Skip to main content

Variables in Java

Java Variables

A Java variable is a piece of memory that can contain a data value. A variable thus has a data type. Data types are covered in more detail in the text on Java data types.
Variables are typically used to store information which your Java program needs to do its job. This can be any kind of information ranging from texts, codes (e.g. country codes, currency codes etc.) to numbers, temporary results of multi step calculations etc.


Simple Program :

In the code example below, the main() method contains the declaration of a single integer variable named number. The value of the integer variable is first set to 10, and then 20 is added to the variable afterwards.


  public class MyClass {
    public static void main(String[] args) {

        int number = 10;

        number = number + 20;
    }

}

Types Of Variables


1. Java Local Variable
2.Java Instance Variable.

1.Java Local-Variable



public class MyClass {
    public static void main(String[] args) {

        int number;
         float b;
        String c;
    }

}

*.local memory is allocated in Stack Segement.
*. Memory is deallocated when control leaves the method.

2.Java Instance-Variable


public class Launch {
        int number;
         float b;
        String c;
    }

}public class MyClass {
    public static void main(String[] args) {

        Launch l= new Launch(); System.out.Println(l.number);
    System.out.Println(l.b);}
}
o/p: 0,  0
   
*.local memory is allocated in Heap Segment.

*.Memory is deallocated when object will become garbage,, and garbage collector will collect it.

Comments