What is Variable in Java Programming Langauge?

  What is Variable in Java Programming Langauge?

  • //Variables:-

    • //In java Programming Language Variable is piece of memory used to store the information.
    • //One Variable can store One Information at time.
    • //Variable also used in information Reuseability.
    • //Variable assigned With Datatype.
    • //to utilize variable in java programming language we need to follow bellow step:
      • //1.Variable Declaration--> Allocating/Reserving Memory.
        • //ex. 
        • String name; 
        • int rollNum; 
        • float weight; 
        • char gender;
      • //2.Variable Initialization-->Assigning/inserting value.
        • //ex. 
        • name="Sambhaji"; 
        • rollNumber=22; 
        • weight=52.52f; 
        • gender='M';
      • //3.usage(Printing/any other Operation)
        •  //ex. 
        • System.out.println("My Name is "+name);
        • System.out.println("My Roll Number is "+rollNumber);
        • System.out.println("My Weight is "+weight);
        • System.out.println("My Gender is "+gender);
    • //Syntax of Variable is---> Datatypes VariableName=Value;
    • //for creating variable we need information
    • //i.e.information may contain character, digit, mixture of both etc.
    • //to store the information we can not declare the variable, we have to declare datatype for it.

                    

  • //Types of Variable:-

    • //1.Global Variable:

      • //It is Declared inside the classbody, but outside the Method/Block/constructor.
      • //Scope of Global Variable throughout the class.
      • //It is also called Permanent Variable / Instance Variable.
      • //This variable can not accessed in static method,but can be accessed in Non static Method within class.
      • //Global variable have a default value. 
        • for int-->0 , 
        • String-->"Null", 
        • Boolean="False".

    • //2.Local Variable:

      • //The Variable which is Declared inside the method/block/constructor is called Local variable
      • //The scope of Local Variable Remains Only within the method/block/constructor.
      • //It is also called Temporary variable.
      • //Local Variable cannot defined with Static Keyword.
      • //We can not declare more than one local variable of same Name within the Method.
      • //Local variable does not have default value in java. we need to provide the value otherwise it shows error.

    • //3.Static Variable:

      • //A Variable declared with static keyword is called static variable
      • //It is also called class variable.
      • //It is declared inside class of body and outside the method body.
      • //Static variable can be accessed in both static and non static method.
      • //static variable has a default value.
        • int-->0 , 
        • String-->"Null", 
        • Boolean="False".

 

  • //Calling Global Variable

    • //same class:-

      • //Static Variable-->System.out.println("My Sum is "+variableName);
      • //non Static Variable-->System.out.println("My Sum is "+ObjectName.variableName);

    • //Differnt class:-

      • //Static Variable-->System.out.println("My Sum is "+anotherclassName.variableName);
      • //non Static Variable-->System.out.println("My Sum is "+ObjectName.variableName);

====================================================================

//Program I:-

package studyOfProject; 

public class StudyOfVariables

{             

    int a=26; // Global Variable

    static int b=55; //static variable

  public static void main(String[] args)

    {

              int x=123; // Local Variable

    }

}

====================================================================

Post a Comment

0 Comments