Identifiers represent names which can be assigned to variables, methods, and classes to uniquely identify them. In java you can be creative in naming identifiers, but there are some limitations such as, all Java identifiers are case-sensitive and must begin with a letter, an underscore (_), or a dollar sign ($). Letters include both upper and lowercase letters, identifier characters can include the numbers from 0 to 9 and finally we cannot use Java defined keywords as identifiers.
For e.g. _shashi7, $r4remployee, programmer are some valid identifiers.
Literals are the constant values that are used in a program. These can include integers, floating-point numbers, characters, boolean and string values. Following are the examples of literals :
- Integer Literals: 2, 3, -4, 5
- Floating point Literals: 3.45, .0987, 943.45
- Character Literals: 'h', 'k','1', '2'
- Boolean Literals: true, false
- String Literals: "shashi", "r4r", "12343"
Variable in Java
A variable may be defined as a storage location in memory for storing different data types. Thus, every variable has a type so we can declare a variable as :
- int sum;
- double sum;
- char name;
A variable name is decalared with a letter at begining and then can be a sequence of letters and digits.
Symbols like + or @ cannot be used inside a variable name and also there can't be any space in variable name.
All characters in the variable are case sensitive and length is essentially unlimited.
You cannot use java keywords as variable names.
After you declare a variable you must initialize it explicitly, as uninitialized varibale can never be used.
int number; // this is declaration number=10; // this is assignment
In java you can declare a variable and assigned it value upon declaration, i.e. intialization of variable
int number=10; // this is initialization
You cannot declare two varibles of same name in the same scope.