COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMS Computer Science with AIML ConcentrationMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International Management
For College Students

Working With Variables In Java - Explained in Simple Way

$$/$$

Let us now take a look at how all this can be done on the IntelliJ IDE and a few errors that one can make.

 

You can download the following java file which will be very useful to you while watching the video. Right click on the java file and open it with IntelliJ.

$$/$$

Video Transcript

 

Now let us practically see as to how we can declare new variables in Java. So here you can see is that I have made a file here, a Java class file with the name variables and I have declared a public static void main function here. Inside this I am declaring two integer variables. So as we discussed earlier, any new new variable must contain a name as well as it must be specified as to what is going to be contained inside that variable. Since my marks one and marks two will be storing student marks and those marks will be of the form of an integer. So I am writing int here. Now in these two statements we have successfully declared our marks one and marks two variables. Now here we are assigning values to these variables and initializing them. So I say marks one is equal to 72 and marks two is equal to 85. Now once this is done, it's time to declare the names of two students. So I say string student one is equal to Adam. Remember that whenever you are declaring any word or textual information or a character you must enclose it within double quotes. Moreover then we decay string student two is equal to Lucy within double quotes. So now we have successfully declared and initialized two variables for marks and two name variables. Let us try to print the marks variable and the student to variable and see what comes out. So for running it, let us press this green button here. When you press the green button then you can click on run variables main. 

 

Once this is done, you will see that on this console 1st 72 is printed. So basically we did a system out print ln of marks one and here marks one was 72. So consequently 72 was printed on the console. Moreover, when we say system out print ln student two. So the program tries to find where the student two variable is located. In the program it sees that student two is declared here and has the value Lucy inside it. And so the value Lucy here is printed on the console. So in this way we have successfully declared and initialized our variables. Let us now take a simple example as to how we can update those variables. So consider that there are two people, Adam and Lucy who are running a race. Both of them start from zero. So therefore let us maintain and declare two variables, one for Adam and one for Lucy which is going to store the location of Adam and Lucy at any given point of time. So now we say we declare and initialize two variables in one line itself. So we say int distance atom is equal to zero, comma distance Lucy is equal to zero. So in this way we were able to declare and initialize both of the variables in one line itself. So now we'll print the initial values of these variables. We say system. Print. Ln distance. Atom and system out. Print. Ln distance. Lucy? So as you expect, these two print statements are going to print zero since the initial values of Distance Adam and Distance Lucy is zero. Now one way to update these variables, as we saw, was to say that the new value here on the left hand side is equal to the old value here on the right hand side plus the change which we want it to reflect. So we say Distance Atom is equal to distance Atom plus 20. Another way to update any given variable was to simply overwrite it. So we say Distance Lucy is equal to 15 and then we are finally printing out the updated values. So the new value should print out 20 and 15 for Adam and Lucy respectively. Let us test out this file and see if it works. So we run it like this. 

 

So yes, you can see here on the console that first two zeros are printed. These two zeros are printed because in the beginning we are printing the initial values of Distance Atom and Distance Lucy. And lastly 20 and 15 are printed. This 20 and 15 are printed which are the updated values of Distance Adam and Distance Lucy. So basically we have seen that this update statement and this update statement have been successfully executed and therefore our values of the variables have been updated. So in this way we are now finally done with learning how to clear initialize and update variables in Java. 

 

Video Recap

 

  • The instructor creates a Java class file and declares a public static void main function.

  • Two integer variables are declared to store student marks.

  • The variables are initialized with values 72 and 85.

  • Two string variables are declared to store the names of two students - Adam and Lucy.

  • The console prints the values of the marks variable and the student variable.

  • Two variables for Distance Adam and Distance Lucy are declared and initialized to 0.

  • The instructor shows two ways to update variables - one by adding a new value to the old value and the other by overwriting it.

  • The console prints the updated values of Distance Adam and Distance Lucy - 20 and 15 respectively.

 

So, we discussed the following things in the video:

  • How to declare and initialise variables — eg. int distance = 0;

  • How to update variable values — <Variable Name> = <Variable Name> + <Update Value>

  • How to print the value stored inside a variable — System.out.println(<Variable Name>)

$$/$$

Now there are a few rules in Java that govern the names given to variables. You can name variables under these rules only. Let’s learn what these rules are. As far as the formatting of your code is concerned, these are the guidelines you should follow. We advise you to have a look at them to learn them as a good practice. A few of the important guidelines from the same document are as follows:

 

Braces follow the Kernighan and Ritchie style for nonempty blocks and block-like constructs:

  • No line break before the opening brace.

  • Line break after the opening brace.

  • Line break before the closing brace.

  • Line break after the closing brace, only if that brace terminates a statement or terminates the body of a method, constructor, or named class. For example, there is no line break after the brace if it is followed by else or a comma.

  • Each time a new block or block-like construct is opened, the indent increases by two spaces. When the block ends, the indent returns to the previous indent level. The indent level applies to both the code and comments throughout the block.

 

Let us now take a look at certain variables naming rules and conventions to be followed in Java.

$$/$$

So we discussed the following rules that should be kept in mind while naming variables:

  • It’s a convention to follow lower camel case.

  • Variable names cannot start with numbers or special characters.

  • There should not be any spaces in variable names.

  • Variable names are case-sensitive.

  • You cannot use reserved keywords as variable names. You will find the list of reserved keywords if you follow the link given here.

Note: The lower camel case and upper camel case conventions are not just specific to Java and are followed while writing code in any other programming language too.