Java Variables

A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.

Variable is a name of memory location. There are three types of variables in java: local, instance and static.

There are two types of data types in Java: primitive and non-primitive.


Variable

A variable is the name of a reserved area allocated in memory. In other words, it is a name of the memory location. It is a combination of “vary + able” which means its value can be changed.

  1. int data=50;//Here data is variable

Types of Variables

There are three types of variables in Java:

  • local variable
  • instance variable
  • static variable

1) Local Variable

A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren’t even aware that the variable exists.

A local variable cannot be defined with “static” keyword.

2) Instance Variable

A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static.

It is called an instance variable because its value is instance-specific and is not shared among instances.

3) Static variable

A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory.

Example to understand the types of variables in java

  1. public class A
  2. {
  3.     static int m=100;//static variable
  4.     void method()
  5.     {
  6.         int n=90;//local variable  
  7.     }
  8.     public static void main(String args[])
  9.     {
  10.         int data=50;//instance variable  
  11.     }
  12. }//end of class 

Java Variable Example: Add Two Numbers

  1. public class Simple{
  2. public static void main(String[] args){
  3. int a=10;
  4. int b=10;
  5. int c=a+b;
  6. System.out.println(c);
  7. }
  8. }

Output:

20

Java Variable Example: Widening

  1. public class Simple{
  2. public static void main(String[] args){
  3. int a=10;
  4. float f=a;
  5. System.out.println(a);
  6. System.out.println(f);
  7. }}

Output:

10
10.0

Java Variable Example: Narrowing (Typecasting)

  1. public class Simple{
  2. public static void main(String[] args){
  3. float f=10.5f;
  4. //int a=f;//Compile time error
  5. int a=(int)f;
  6. System.out.println(f);
  7. System.out.println(a);
  8. }}

Output:

10.5
10

Java Variable Example: Overflow

  1. class Simple{
  2. public static void main(String[] args){
  3. //Overflow
  4. int a=130;
  5. byte b=(byte)a;
  6. System.out.println(a);
  7. System.out.println(b);
  8. }}

Output:

130
-126

Java Variable Example: Adding Lower Type

  1. class Simple{
  2. public static void main(String[] args){
  3. byte a=10;
  4. byte b=10;
  5. //byte c=a+b;//Compile Time Error: because a+b=20 will be int
  6. byte c=(byte)(a+b);
  7. System.out.println(c);
  8. }}

Output:

20

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *