JAVA Object

         Class is a blueprint of an object. Class contains state and behaviour of an object.

          State means variables and behaviour means methods.


          All the objects are stored in heap memory in JVM in runtime which is called garbage collectable.


Variables:

          Variables are of 2 types : Primitive Variables and reference Variables.


   Primitive Variables:


             Instance variables means variables of an object which vary from object to object. Which are defined by type of variable and name of variable like below . Variables have different sizes like cups in Starbucks when you ask a cup of capuchin they will ask small or medium or large. Similarly java primitive variables have different sizes like               byte << short << int << long 

                     float << double

                     char

                     boolean



                            int x = 0;

          In above example we declared a variable of type int and named the variable as x and assign a value 0.

 So in heap memory this primitive  reference will be created and assign bits equivalent to 0 .


        We can assign values or we can create an variable of compatible type like  below

                      int x = 0;

                      int  y = x;

                In above example we created a variable called x and assigned a value 0 and created another variable called y and assigned x .Now in variable y the copy of x will assign so if we change anything in x will not affect the y as it not directly referred to x location.


       Here compatible means byte is compatible to all types which are bigger than byte like below

                byte x = 1;

                int y = x; 

     But if we want to do in opposite way then compiler will not allow as it will be a spill over which will not fit like the Starbucks will not able to give the large coffee in small cup which will spill the extra coffee.  But they can give small coffee in large cup where some extra space will be left in the cup.



    We can also force the complier to fit larger type variables into smaller using casting then compilers spills the extra bits like below


           long I = 20;

           int  j = (int ) l;

   


        boolean is un castable type in java.


   Reference Variables:


          Reference variables are variables of which they store the reference  of another object like below.


         Student st = new Student();

          Reference variable declare with reference variable type and assign an object reference using new keyword.

Only Object reference which is bits of data will store in the variable not whole object . For example  TV is a object and we access all the TV controls using TV Remote so here remote contains complete reference of the TV which can able to control the TV .Similarly reference variables just have the control of an object not whole object itself.

     

         Here if assign a ref variable to another variable of same object then both variables are pointing to same object that means same TV we can control with 2 remotes. If any change we did on one variable it will reflect in the another variable  which is pointing to same object.


Methods : 


          Methods define the behaviour of an object and Methods us instance variables to change the behaviour of an object. Methods have parameters(Its not mandatory to have parameters for a method) which are defined with type and name  and a return type if the method is not returning anything then we can just mention void like below 


     You can send things to a method and you get things back from a method.


            void sum(int a, int b);


           These methods can be called by the reference variable which are created to an object and we can pass arguments to the method like below.

             Calculate cal = new Calculate();

             cal.sum(2,3);

         We should pass same type of arguments which we defend in the method.

         When we pass any arguments to the methods then we are just passing copy of variables not same variables which are pointing.


         The variables which are define inside the method are local variables.

Local variables must be initialised before use but instance variables have default values based on type of variable.


Java is pass-by-value that means pass-by-copy.

       Lifting more weights using methods. That means doing more tasks using methods. We can do many things inside the function like operators, loops ., and Java provided functions etc…

 

    Enhanced for loops means for each element in the collection  assign a variable of given type like below

       for(int i :  array){
        }

In above example the colon implies that IN,


  For each element in the collection  assign the element to the I variable and run the body of the loop.


Short Circuit Operators:

                  && and || are called short circuit operators because && means all the conditions which are given in expression should be true that’s why JVM will check if first one is false then JVM stops right there and will not enter into the loop or if . Similarly || means any of the condition can be true that’s why JVM will check if one condition is true then JVM makes whole expression as true and ignore remaining.


Non-circuit Operators : 

               When used in boolean expressions , the & and | operators act like their && and || counterparts ., expect that they force JVM to always check both sides of the expression. Typically, & and | are used in another context, for manipulating bits.


Memory Management in Java:

                JVM will maintain all the things in 2 memories one is Heap and another is Stack. All Objects will be in Heap and instance variables will available inside Object . And all the methods calls will be in stack.  Stack follows first in last out ., So when the method is inserted into stack it will stay until the end bracket of method and all local variables will stay inside the method stack ., Here where reference variable declared inside the method will go as we discussed all the objects will goes into the Heap all means all So., when the method declared an reference variable only the reference means the remote control will stay inside the method stack but the remote which its refereeing to a TV will stay inside the Heap . If the method is removed from the stack then the connection between reference variable and Object will lost then GC play a crucial role to manage the memory efficiently. 

    Scope of an variable means the block where variable can access.



Life and Death of an Object : 


                In JAVA Life of an object begins with new Keyword followed by class name with braces . If we declare new keyword the JVM will allocate a memory to the object of that type. So all the super classes of the object which are creating will stay inside the object so its object inside object. When we call new keyword object will call constructor of the class. Constructor is defined same name as class name with no return type and we can pass args. If we don’t pass any args then that constructor is called no-arg constructor. By default JVM will create no-arg constructor for us . But if we write any arg constructor then JVM will remove the no-arg constructor that’s why we have to define the no-arg constructor again.  Constructor helps object to do initial setup before object is created.              An object is assigned to a reference variable then the object is in active state . If object is not assigned to any reference the object is eligible for Garbage collectable . Means The Garbage collector will remove these objects to free up space. We can use finalize method for object clean up activities.

          We can use super() keyword to call the constructor of super class.. super()  should in first line. If we want to refer same object then use this keyword. this is should be first statement in constructor. That’s why we can’t use both statements in same constructor.       


Static in JAVA:

                     Static is like class level things means it will define one per class and all the objects will share this. We can make variables static and method static. Mostly static variables used for constants. We cannot use instance variables and call non static methods in static methods because  static blocks will be at class level then which object variable will class use? We have to initialise static variable while declaration or in static block. static block will first execute when class is compiled.  Static block will execute before constructor also.

We can use static variable to know the no of objects created for this class as this value will not override.

We should mark constructor as static.


Numbers in JAVA:

                 All the primitives in java have wrappers like int have Integer as wrapper . We can use many wrapper class methods to do operations in numbers. JAVA will take care AutoBox primitives for us.

We can use formatted in java to format numbers. The format specifier is like below :

      %[argument number][flags][Width][.precision] type


            All [] are optional and % and type is mandatory.


      Example : format(“%,6.1f”,42.0000);     will give 4,2.0

In above example , is flags. 6 is width and for precision we have to mention . And precision value and type f means floating point ., similarly d means decimal.x Manas hexadecimal. And c means character

Comments

Popular posts from this blog

JAVA Collections

JAVA Annotations