An identifier is a name given to a variable, class, or method. Identifiers have the following characteristics:
Can start with a Unicode letter, underscore (_), or dollar sign ($) Are case-sensitive and have no maximum length.
The valid identifiers are:
A variable is a container that holds values that are used in a Java program. Every variable must be declared to use a data type. For example, a variable could be declared to use one of the eight primitive data types: byte, short, int, long, float, double, char or boolean. And, every variable must be given an initial value before it can be used.
Local variables: Variables defined inside a method are called local variables, also referred to as automatic, temporary, or stack variables.
Instance variables: These are variables that are used to store the state of an object (for example, id). Every object created from a class definition would have its own copy of the variable. They are declared without using the static keyword. It is valid for and occupies storage for as long as the corresponding object is in memory.
Class variables: These variables are explicitly defined within the class-level scope with a static modifier. No other variables can have a static modifier attached to them. Because these variables are defined with the static modifier, there would always be a single copy of these variables no matter how many times the class has been instantiated. They live as long as the class is loaded in memory.
Parameters or Arguments: These are variables passed into a method signature. Recall the usage of the args variable in the main method. They are not attached to modifiers (i.e. public, private, protected) and they can be used everywhere in the method. They are in memory during the execution of the method and can't be used after the method returns.
datatype variablename;
Example
int area;
float avg;
char grade;
int area = 10;
float avg =12.3f ;
char grade = 'A';
boolean flag = true ;
It is necessary to initialize the variables before using then in the program.
Java forces the compiler to assign a value for local variables.
Java forces the complier to assign the default values for primitive and reference instance variables
The following figure displays the default values of primitive types.
class VariableExample { int x; // instance variable static int y; //static or class variable public static void main(String[] args) //parameter variable or local { x=10; // wrong instance variable can't be used inside static method } public void display() { int z; //local variable } }