As the name suggests, the values stored in these variables can change depending on the need of the program. Through the following video, Aishwarya will show you how the variables names can be updated.
To update the value of a variable in Java, you follow the following format:
<Variable Name> = <Variable Name> + <Update Value>;
For example:
marks = marks+10;
You can also add another variable as the update value. For example, consider that there is another variable called marks1 which needs to be added to marks. This would be accomplished as follows(assume all variables are int type):
marks = marks+marks1;
So, the addition of marks and marks1 shall be assigned to the variable, marks.
One can assign values to variables, update or change those values, but one may also want to print the values in the variables.
You already know how to print statements using Java code. In the following video, you will see how you can print the values of variables to the console.
Through the video above, you learned about one way to print variables in Java:
System.out.println(<Variable Name>);
For example:
System.out.println(marks);
Note: This will move the cursor in a new line after printing the variable.
If you wish to not start printing at a new line then you should use:
System.out.print(<variable name>);
In the next segment, we will see all our code in action. Also, we will learn about some best practices to be followed while writing code in Java.