Skip to main content

Variables

Variables

Variables acts as a container for the user data. It is a named area that is allocated by the user and space will be allocated according to its datatype. Variable needs to be declared before using them.
Value of variable can be changed any number of times during the execution of program.

 For example:
                 int a;   //declaring an integer type variable.
                 a = 5;  //assigning value to a variable.




  • Local Variables : Variables that are declared inside a members are called Local Variables. Local variables are necessary to initialized.Their value is varied from object to object.

          Only Final modifier is used with local keywords.
          * They can be initialized in static and instance block.
          * Stored inside stack memory.
          * Scope of these variables is the block in which we declared them.
          * Created when block in which declared is executed.
          * Destroyed when block in which declared is completed.
          * JVM doesn't provide any default value, thus it is mandatory to initialized them for accessing.
          * Local variables are thread safe as for every thread a separate copy is created.
  • Instance Variables : Variables that are defined directly inside a class without static keyword are known as Instance variables.These variables can have difference values across different objects.
          * A separate copy of variable will be created for every object
          * Created at the time of object creation and destroyed at the time of object distraction.
          * Stored in Heap memory as a part of object.
          * Can.t access directly from static context, but we can access them by using object reference.
          * JVM provides default values for instance variables.
          * Instance variables are not thread safe because multi-thread can be accessed simultaneously
  • Static Variables :  Variables that defined directly inside a class with a Static keyword are known as Static variables(also known as Class variables).These variables can only have one value.
          * A single copy will be created and shared by every object of class.
          * Created at the time of class loading and destroyed at the time of class unloading.
          * Stored in memory ares.
          * Scope is same as of .class file
          * Can be access either with object reference or class name directly.
          * Can access directly from instance as well as static context.
          * JVM provide default value for static variables.
          * Static variable are also not thread safe as the can access multi-thread simultaneously.

    For Example :
               class abc
                 {
                    int a;             // Instance variable.
                    static int b;   //Static variable.
                    public static void main(String arg[])
                    {
                        System.out.println(a);
                        int c;        //Local variable.
                        c = 5;
                        System.out.println(c);
                    }
                 }

    Ways to deal with Variables -
    • Declaring a variable in one statement:          
                   Syntax
                         [modifiers] <Datatype> <Variable>;

                  Example
                         int i;
                         byte b;   
                         char c;
                         double d;
                         float f;
                         String s;  
                         Hello h;  etc.


    • Initializing single variable:
                    Syntax
                          <Variable_Name> = <value / expression>;

                    Example
                           i = 5;
                           c = 'A';
                           b = 4 + 5;
                           d = 59.64;


    • Declaring and Initializing a variable in a single statement: 
                    Syntax
                          [modifier] <Datatype> <Variable_Name> = <value/expression>;

                    Example
                          int a = 9;
                          float f = 45.11;
                          int x = 44 + 16;
                          boolean b = true;


    • Declaring multiple variables in a single statement:
                    Syntax
                          <Datatype> <variable1>, <variable2> , <variable3>.... ;

                    Example
                           int a,b,c;
                           float p,q,r,s;
                           String s1,s2,s3;


    • Initialize multiple variables with same value:
                  Syntax
                        <variable1> = <variable2> = <variable3>... = <value/expression>;

                  Example
                         a = b = c = 25;
                         p = q = r = s = 66.23;
                         str1 = str2 = str3 = "Hello";


    • Declaring and Initializing multiple variables in single statement:
                 Syntax
                      [modifier] <Datatype> <variable1> = <value1>, <variable2> = <value2>, <variable3> = <value3>.... ;

                 Example
                       int a = 1, b = 4, c = 6, d = 59;
                       String str1 = "Welcome", str2 = "To", str3 = "My_Blog";
                       boolean x = true, y = false;

    Key Points To Remember About Variables:
    1. Local variables value must be provided by user.
    2. Static and Instance variables are initialized by JVM.
    3. Static variables can be directly accessed from static content.
    4. Default value for static variable is 0.
    5. Same name variables can also be declared but in different scope of same class.

    Comments

    1. Wow,what a skimmed content about the Variables.
      Could you recommend us a genuine book for further reading?

      ReplyDelete

    Post a Comment

    Popular posts from this blog

    Data Types

     Data Types     There are different types of data used by programmers in java like Numbers, Alphabets, Images etc. Computer does not know the difference between values like number or alphabets.Each type of data will be categorized into different types which are known as Datatypes in java. Thus, Datatype is a classification of data according to its type that an object or a variable can hold. It is a kind of data storage that contains specific type or range of value. Whenever variable is created a storage is assigned to it according to the datatype. Two main concepts of Datatypes are - Type of data. Size allocated to it. It is classified into two main categories i.e Primitives and Referenced .  There are 8 Primitive Type Datatypes and 4 Referenced Datatypes i.e Primitives Datatypes - These are built-in or predefined by the language and their definition cannot be changed. byte short char int long float double boolean Referenced Dat...

    Polymorphism

    Polymorphism Polymorphism means " Many Forms " - One Object with many behaviors. In other words, ability to appear more than one i.e. to do different things with single object. It can be implemented by method overloading and method overriding in java. For eg. : When you press a device it will turn on, when you press it again it will turn off. Similarly in java "+" Plus Operator has polymorphic behavior: It can perform Arithmetic Operation as well as Concatenation Operation Types of Polymorphism Static Polymorphism Dynamic Polymorphism Static Polymorphism also represented as Compile Time  Polymorphism and Early Binding  Polymorphism. Static Polymorphism is just a method overloading. Thus, it is a process of defining more than one functionalities with same name in same class but having different parameter lists (i.e. different no. of parameters list or different parameters type) Here compiler will figure out which method to call by...

    Literals

    Literals Literal is the value that we pass in the variables. In other words, Literals are syntactic representation of boolean,numeric,character and string type. These values can be fixed or constant and are stored in the variable. Generally literal are categorized into two types i.e. Numeric Literals and Non Numeric Literals. Integer Literal : They includes - int, short, long, byte. These are the value without any decimal. Int is default type of integer. if any value is out of range in integer literal then compiler will consider that value as int and generates an error. To represent any value in long L/l will be used at the end. There are 4 integer Literals type i.e. Decimal, Octal, Hexadecimal, Binary(added in java 7)  Decimal Literals has base 10 i.e from 0 to 9.          They must not be started with 0. Example :          int a = 111;          it will print 111 Octal...