Java Variables|Local Variable| Instance Variable| Static Variable

What are Variables in Java?

Java Variables: A variable is a location in memory storage area to hold data. To indicate the storage area, each variable should be given a unique name It is a name of storage space which is used to store data, Its value may be changed. It always contains the last value stored to it. It is always declared with data type.

int studentid=100;

Here, studentid is a variable of int data type, and we have assigned value 100 to it. The int data type suggests that the variable can only hold integers. we have assigned value to the variable during declaration. However, it's not mandatory. You can declare variables and assign variables separately. 

int studentid;


studentid=1001

Note: Java is a statically-typed language. It means that all variables must be declared before they can be used.

Java Variables|Local  Variable| Instalce Variable| Static Variable
Java Variables|Local Variable| Instance Variable| Static Variable

    Variable declaration in java

    int student_id;

    float student_marks;

    char student_grade;

    Here, student_id is a variable of type int, student_marks is a variable of type float and student_grade is a variable of type char.

    java variables initialization declaration

    We can initialize a variable at the time of declaration of the variable or after declaration.

    int student_id=2023;

    float student_marks=85.6;

    char student_grade='A';

    Here 2023 is the value of student_id,85.6 is the value of student_marks and A is the value of student_grade. Character value is always written in single quotes.

    What are the 5 rules to declare variable name?

    • The first letter of a variable should be alphabet or underscore(_).
    • The first letter of the variable should not be a digit.
    • After the first character, it may be a combination of alphabets and digits.
    • Blank spaces are not allowed in variable name.
    • Variable name should not be a keyword.

    What are the four types of variables in Java?

    Java programming language has its own set of rules and conventions for naming variables. Here's what you need to know:

    1. Instance Variables (Non-Static Fields)

    2. Class Variables (Static Fields)

    3. Local Variables

    4. Parameters

    How many there are types of variables in Java?

    1. Local Variable
    2. Instance Variable
    3. Static Variable

    What is local variable in Java with example?

    Local Variable: A variable declared inside the body of the method or constructor or block is called local variable. A local variable can be used only inside that method/function in which it is declared. A local variable can be a static variable.

    class Learncoding

    {

     public void data()

     {

     int x,y=100,z=200;//declaring local variable

     x=y+z;

     System.out.println("Addition:"+x);

     }

     public static void main(String[] args) 

     {

      Learncoding obj=new Learncoding();

      obj.data();       

     }

    }


    **********OUTPUT************

    Addition:300

    How do you declare an instance variable in Java?

    Instance Variable: A variable which is declared inside a class but outside the body of the method or constructor or block is called an instance variable. Instance variable can be used anywhere in the program.

    class Learncoding 

    {

     int x=300,y=100,z;

     public void add()

     {

     z=x+y;//accessing instance variable

     System.out.println("Addition:"+z);

     }

     public void sub()

     {

     z=x-y;//accessing instance variable

     System.out.println("Subtraction:"+z);

     }

     public static void main(String[] args) 

     {

      Learncoding obj=new Learncoding ();

      obj.add();  

      obj.sub();

     }

    }


    **********OUTPUT************

    Addition:30

    Subtraction:10

    What is static variable in Java with example?

    Static Variable: A variable which is declared with static keyword, inside a class but outside the body of the method or constructor or block is called static variable. 

    Static variable is stored in the static memory. Static variables are created when the program starts and destroyed when the program stops. Static variable can be called by class name directly.

    class Learncoding

    {

     //declaring static variable

     static String text="Welcome To Learn Coding Website.";

     public static void main(String[] args) 

     {

         System.out.println(Learncoding.text);

     }

    }

    *********OUTPUT*********

    Welcome To Learn Coding Website.

    Java literals: Literals are data used for representing fixed values. They can be used directly in the code. 

    int a =1;

    float b = 2.5;

    char c = 'F';

    1. Boolean Literals: In Java, boolean literals are used to initialize boolean data types. They can store two values: true and false.

    boolean flag1 = false;

    boolean flag2 = true;

    2. Integer Literals: An integer literal is a numeric value (associated with numbers) without any fractional or exponential part. 

    There are 4 types of integer literals in Java

    1. binary (base 2)

    2. decimal (base 10)

    3. octal (base 8)

    4. hexadecimal (base 16)

    3. Floating Point: A floating-point literal is a numeric literal that has either a fractional form or an exponential form. 

    class Learn {


      public static void main(String[] args) {

        

        double datable = 3.4;


        float dataFloat = 3.4F;


        double myDoubleScientific = 3.445e2;


        System.out.println(datable);  


        System.out.println(dataFloat);   


        System.out.println(myDoubleScientific); 


     }


    }



    Note: The floating-point literals are used to initialize float and double type variables.

    4. Character Literals: Character Literals are Unicode character enclosed inside single quotes. 


    5. String literals: A string literal is a sequence of characters enclosed inside double-quotes. 

    String str1 = "Java Programming Language" ;


    String str2 = "Learn Coding" ;


    FAQ Java Variables

    Q1. What is variable data type?
    Ans: Variable is a hold of the value. Variable is a container and store any value. it is very, the most important part of any programming language.

    Q2. Is variable a keyword in Java?
    Ans: One of the 50 reserved phrases in the Java programming language, a Java keyword is a term with a specific use and definition.

    Q3. Is array a variable in Java?
    Ans: it is a collection of data of same data types. It is used to store group of data simultaneously.

    Q4. Is array An variable?
    Ans: Because it may hold several values, an array is a unique kind of variable. An index and an array name make up an array. The element in the array that is addressed is identified by the index. Declaring arrays, array elements, or the number of elements in an array is not required.

    Q5. Is String a variable?
    Ans: A type of value that can be kept in a variable is a string. Characters, which can be letters, sentences, phrases, or symbols, make up a string. Strings are used to store collections of characters, such as words or phrases.

    Conclusion

    Hello, Java Programmer in this post you can learn java variables like local variable, instance variable and static variable.
    variable is most part of any programming language.


    Post a Comment

    0 Comments