Java Lesson 2
Java Lesson 2
• Java Comments
➢ Single-line Comments
➢ Java Multi-line Comments
• Java Variables
➢ Declaring (Creating) Variables
➢ Final Variables
➢ Java Print Variables
➢ Display Variables
➢ Java Declare Multiple Variables
➢ One Value to Multiple Variables
➢ Java Identifiers
Java Output / Print
Print Text
You learned from the previous chapter that you can use the println() method to output values or print
text in Java:
Example
System.out.println("Hello World!");
Double Quotes
When you are working with text, it must be wrapped inside double quotations marks "".
If you forget the double quotes, an error occurs:
Example
System.out.println("This sentence will work!");
System.out.println(This sentence will produce an error);
Note that we add an extra space (after "Hello World!" in the example above), for better readability.
In this tutorial, we will only use println() as it makes it easier to read the output of code.
Example Example
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be executed).
This example uses a single-line comment before a line of code:
Example
// This is a comment
System.out.println("Hello World");
Example
System.out.println("Hello World"); // This is a comment
It is up to you which you want to use. Normally, we use // for short comments, and /* */ for longer.
Exercise:
To create a variable that should store text, To create a variable that should store a number,
look at the following example: look at the following
Example Example
Create a variable called name of Create a variable called myNum of
type String and assign it the value "John": type int and assign it the value 15
int myNum = 15;
String name = "John"; System.out.println(myNum);
System.out.println(name);
You can also declare a variable without assigning the value, and assign the value later:
Example
int myNum;
myNum = 15;
System.out.println(myNum);
Note that if you assign a new value to an existing variable, it will overwrite the previous value:
Example
Change the value of myNum from 15 to 20:
int myNum = 15;
myNum = 20; // myNum is now 20
System.out.println(myNum);
Final Variables
If you don't want others (or yourself) to overwrite existing values, use the final keyword (this will
declare the variable as "final" or "constant", which means unchangeable and read-only):
Example
final int myNum = 15;
myNum = 20; // will generate an error: cannot assign a value to a final
variable
Other Types
A demonstration of how to declare variables of other types:
Example
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
Exercise:
Create a variable named carName and assign the value Volvo to it.
= ;
For numeric values, the + character works as a mathematical operator (notice that we use int (integer)
variables here):
Example
int x = 5;
int y = 6;
System.out.println(x + y); // Print the value of x + y
Java Identifiers
Identifiers
All Java variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and maintainable
code:
Example
// Good
int minutesPerHour = 60;