Types and Variables in Java

Types and variables:

There are three types of variables in Java :
1.Local- A variable declared inside the method is called local variable.
2.Instance- A variable declared inside inside a class and outside of methods is called instance variable
3.Static- A variable that is declared as static is called static variable. It cannot be local.

Example:

public class TypesAndVariables {

static int staticVariable=10;// Static variable
int globalvariable=25;//Global Variable/instance variable

public static void main(String[] args) {
// TODO Auto-generated method stub
int localvariable=15;// Local variable inside a method.
System.out.println(staticVariable+” “);

}

}

All Data in Java can be divided into two types:
1.Primitive
2.Non primitive

Primitive data types- There are eight primitive data type in Java:
Data Type

Data Type Default Value Default size
boolean false 1 bit
char ‘\u0000’ 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

Non primitive data types are: Array, Strings etc(will be discussed later in detail).

Examples:
Adding two variables:

public class VariablesExamles {

public static void main(String[] args) {

int a=5;
int b=6;
System.out.println(“sum of a &b =”+(a+b));
}

}

output: sum of a &b =11.

We can convert one datatype into another considering Typecasting Rules.