Tokens and variables
The smallest individual entities in a java program are called tokens. They are classified as
- Reserved words (keywords)
- Identifiers (classes, methods, variables, objects, packages and interfaces)
- literals (values)
- Operators (+,-,++,–,etc)
- separators ({}, (), [])
Variables
Variable is a named memory location which can hold value and the value can change any no. of times during execution.
- the first character in a variable should not be a digit. It should start with a letter or $ or _.
- A variable name may consist of alphabets, digits, underscore and the dollar character.
- A keyword should not be used as a variable name.
- There should be no space in a variable name
legal:
int _a;
int $c;
int _4;
int a_very_long_variable_name;
Illegal:
int :a;
int -d;
int .f;
int 7g;
int i; //Declaration
i = 10; // Initialization
int i = 10; // both Declaration and Initialization
Note:
- Every statement should end with ;
- If local variable is not initialized when using it, java throws error (local variable).
- If you are not using the variable which is not initialized, it does not throw any error (local variable).
- Whenever a variable is declared within a method, it is a local variable.
- whenever a variable is declared outside a method and within class definition, it is a global variable/member;
- global variables have default values, local variables should be initialized before using.
-
The Initialization can be done at the time of declaration or separately or at the time of usage.
class Demo3{ public static void main(String[] args){ int i; //declaration of local variable i i = 10; // local variable should be initialized before using in one single sentence System.out.println("Main starts"); System.out.println(i); System.out.println("Main ends"); } }
Output: Main starts 10 Main ends
class Demo4{ static int i; // global variable declared public static void main(String[] args){ System.out.println("Main starts"); System.out.println(i); System.out.println("Main ends"); } }
Output: Main starts 0 Main ends
-
If the global variable has to be initialized, then it can be done at global level like example below
example: static int i = 0; //correct static int i; i=10;//compile error
- the global variables are initialized automatically by java compiler at the time of compilation if not initialized.
- The default values for number data types i.e., int, long, float, double is 0(zero) where as String is null and char is ‘‘(empty character)
There are two types of data types
1) Primitive data types 2) non-primitive data types (reference variables)
class Demo5{
static byte b;
static short s;
static int i;
static long l;
static float f;
static double d;
static char c;
static boolean flag;
public static void main(String[] args){
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(c);
System.out.println(flag);
}
}
Output:
0
0
0
0
0.0
0.0
false
class Demo6{
static String str; // string variable
public static void main(String[] args){
System.out.println(str);
}
}
Output:
null