You are on page 1of 11

Java Identifiers

⚫ Are tokens that represent names of variables, methods, classes, etc.


Examples of identifiers are: Hello, Sample, main, System, out.
⚫ Java identifiers are case-sensitive
This means that the identifier Hello is not the same as hello.
⚫ Identifiers must begin with either a letter, an underscore “_”, or a dollar
sign “$”. Subsequent characters may use numbers 0 to 9.
⚫ Identifiers cannot use Java keywords like class, public, void, etc.

Java Identifiers Coding Guidelines


⚫ For names of classes, capitalize the first letter of the class name. For
example:
ThisIsAnExampleOfClassName
⚫ For names of methods and variables, the first letter of the word should
start with a small letter. For example:
thisIsAnExampleOfMethodName
⚫ In case of multi-word identifiers, use capital letters to indicate the start of
the word except the first word. For example:
charArray, fileNumber, className
⚫ Avoid using underscores at the start of the identifier such as _read or
_write.
Java Identifiers
Java Keywords
⚫ are predefined identifiers reserved by Java for a specific
purpose.
⚫ You cannot use keywords as names for your variables,
classes, methods… etc.

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

Java Literals: Floating Point


⚫ Represents decimal with fractional parts.
Example: 3.1416
⚫ Can be expressed in standard or scientific notation
Example: 583.45 (standard); 5.834e2 (scientific)
Java Literals
Java Literals: Boolean
⚫ Boolean literals have only two values, true or false.

Java Literals: Character


⚫ Character Literals represent single Unicode characters.
⚫ Unicode character
A 16-bit character set that replaces the 8-bit ASCII character set.
Unicode allows the inclusion of symbols and special characters from other
languages.
⚫ To use a character literal, enclose the character in single quote
delimiters.
⚫ For example
The letter a, is represented as ‘a’.
Special characters such as a newline character, a backslash is used followed
by the character code. For example, ‘\n’ for the new line character, ‘\r’ for
the carriage return, ‘\t’ for tab.

Java Literals: String


⚫ It is not a primitive data type, it is a class.
⚫ String literals represent multiple characters and are enclosed by
double quotes.
⚫ For example: “Hello World”.
Variables in Java
What is a variable?
⚫ A variable is an item of data used to store the state of
objects.
⚫ A variable has a:
data type which indicates the type of value that the variable can
hold.
Name that must follow rules for identifiers.
⚫ A variable is a name which is associated with a value that
can be changed. For example, int i = 10;

How to declare a variable in Java


⚫ To declare a variable follow this syntax:
<data type> <name> [ = initial value];
⚫ Note: Values enclosed in <> are required values, while those
in [] are optional.
⚫ For example:
int num;
int num = 100;
char ch = ‘A’;
char ch;
Variables in Java
Declaring and initializing variables: Sample Program
public class VariableSamples
{
public static void main (String[] args)
{
//declare a data type with variable name result and Boolean
//data type
Boolean result;
//declare a data type with variable name option and char
//data type
Char option;
option = ‘C’; //assign ‘C’ to option
//declare a data type with variable name grade,
//double data type and initialized to 0.0
Double grade = 0.0;
program codes/statements


}
}
Declaring and Initializing Variables
Coding Guidelines
⚫ It is always good to initialize your variables as you declare them.
⚫ Use descriptive names for your variables.
⚫ Declare one variable per line of code.
⚫ Variables naming cannot contain white spaces, for example:
int num ber = 100;
⚫ Variable name can begin with special characters such as $ and _
⚫ As per the java coding standards the variable name should begin with a lower
case letter, for example int number; For lengthy variables names that has
more than one word do it like this: int smallNumber; int bigNumber; (start
the second word with capital letter).
⚫ Variable names are case sensitive in Java.

Types of Variables in Java


⚫ Local variables
⚫ Static (or class) variables
⚫ Instance variables
⚫ Primitive variables
⚫ Reference variables

Static (or class) Variable


⚫ For example, If I create three objects of a class and access this static variable,
it would be common for all, the changes made to the variable using one of the
object would reflect when you access it through other objects.
Static (or class) Variables
Example of static variable:
public class StaticVarExample
{
public static String myClassVar=“Class or static variable";

public static void main(String args[])


{ Output:
StaticVarExample obj = new StaticVarExample(); Class or static variable
StaticVarExample obj2 = new StaticVarExample(); Class or static variable
StaticVarExample obj3 = new StaticVarExample(); Class or static variable
Changed Text
//All three will display “Class or static variable" Changed Text
System.out.println(obj.myClassVar);
System.out.println(obj2.myClassVar);
Changed Text
System.out.println(obj3.myClassVar);

//changing the value of static variable using obj2


obj2.myClassVar = "Changed Text"; As you can see all three statements displayed
the same output irrespective of the instance
//All three will display "Changed Text" through which it is being accessed. That is why
System.out.println(obj.myClassVar); we can access the static variables without
System.out.println(obj2.myClassVar); using the objects like this:
System.out.println(obj3.myClassVar); System.out.println(myClassVar);
}
Do note that only static variables can be
} accessed like this. This doesn’t apply for
instance and local variables.
Instance Variables
Example of Instance variable
public class InstanceVarExample
{
String myInstanceVar="instance variable";
public static void main(String args[])
{
InstanceVarExample obj = new InstanceVarExample();
InstanceVarExample obj2 = new InstanceVarExample();
InstanceVarExample obj3 = new InstanceVarExample();
System.out.println(obj.myInstanceVar);
System.out.println(obj2.myInstanceVar); Output:
instance variable
System.out.println(obj3.myInstanceVar);
instance variable
obj2.myInstanceVar = "Changed Text"; instance variable
instance variable
System.out.println(obj.myInstanceVar); Changed Text
System.out.println(obj2.myInstanceVar); instance variable
System.out.println(obj3.myInstanceVar);
}
}
Local Variables
⚫ These variables are declared inside method of the class and their scope is limited to the
method.
⚫ In the example, instance variable is declared with the same name as local variable, this is
to demonstrate the scope of local variables.
Example of Local variable
public class VariableExample
{
// instance variable
public String myVar="instance variable";
public void myMethod()
{
// local variable
String myVar = "Inside Method";
System.out.println(myVar); Output:
} Calling Method
public static void main(String args[])
Inside Method
{
// Creating object instance variable
VariableExample obj = new VariableExample();

/* 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.

int num = 10;


String name = “Hello”;

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.

Memory Variable Data


Address Name
1001 num 10
: :
1563 name Address(2000)
: :
: :
2000 “Hello”
As you can see, for the primitive variable num, the data is on the actual location of where the
variable is. For the reference variable name, the variable just holds the address of where the
actual data is.

You might also like