Session 2 Java Identifiers
Session 2 Java Identifiers
Session 2 Java Identifiers
Java Literals
⚫ Literals are token that do not change or are constant.
⚫ The different types of literals in Java are:
Integer Literals
Floating-Point Literals
Boolean Literals
Character Literals
String literals
Java Literals
Java Literals: Integer
⚫ Integer literals come in different formats:
Decimal (base 10)
Hexadecimal (base 16)
Octal (base 8)
⚫ Special Notations in using integer literals in our programs:
Decimal
• No special notation
• Example: 17
Hexadecimal
• Precede by 0x or 0X
• Example: 0xD
Octal
• Precede by 0
• Example: 017
/* We are calling the method, that changes the value of myVar. We are displaying myVar again after
* the method call, to demonstrate that the local variable scope is limited to the method itself.
*/
System.out.println("Calling Method");
obj.myMethod();
System.out.println(obj.myVar); If you had not declared the instance variable and only
} declared the local variable inside method then the
} statement System.out.println(obj.myVar); would have
thrown compilation error. As you cannot change and
access local variables outside the method
Primitive and Reference Variables
Primitive Variables
⚫ Variables with primitive data types
⚫ Stores data in the actual memory location of where the variable is.
Reference Variables
⚫ Variables that stores the address in the memory location
⚫ Points to another memory location where the actual data is
⚫ When you declare a variable of a certain class, you are actually declaring a reference
variable to the object with that certain class.
For example, given two variables with data types int and String.
Suppose, the illustration shown below is the actual memory of your computer, wherein you
have the address of the memory cells, the variable name and the data they hold.