Sunday, April 16, 2017

JAVA

1. Difference between JDK, JRE and JVM

JDK = JRE + Development Kit
JRE = JVM + Library classes

To develop a JAVA Application JDK is needed.

To run JAVA Application in client machine JRE is needed

2. Explain JVM:


https://www.youtube.com/watch?v=dncpVFP1JeQ
https://www.youtube.com/watch?v=ZBJ0u9MaKtM

1. Converts Byte code to machine code
2. JVM is a specification. Really don't exists JRE contains JVM.
3. JRE for Windows will install code required for windows. JRE for Linux will will install code required for Linux.
4. JAVA is Write once and run many places and JVM is the one that does the job in running the application on different platforms. JAVA is platform independent, however JRE is tightly platform dependent.
5. JVM reads class file and converts to the format that the platform understands
6. JVM instance exists in computer until the program runs. If 3 Java programs simultaneously runs then 3 JVM instances runs.


JDK contains JRE and JRE contains JVM


Class Loaders

Load
Bootstrap Class Loader - Loads JAVA Internal class. Inside rt.jar. Framework classes

Extension Class Loader - Loads additional application JARS present in JRE/LIB/Ext

Application Class Loader - Loads classes from CLASSPATH Environmnet variables. if -cp command is given, even those would be loaded.

Linker:
Verify
 Prepare
Resolve:

Initialize

Memory Unit

Methods Area,Heap, Stack, PC Registers, Native Method Stack


3. What is Block?
Java has a concept called block that is enclosed between the { and } characters, called curly braces. A block executed as a single statetement, and can be used where a single statetement is accepted. After a block is executed all local variables defined inside the block is discarded, go out of scope.

4. Final Keyword?


The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:



1. variable: If you make any variable as final, you cannot change the value of final variable(It will be constant).

class Bike9{  
 final int speedlimit=90;//final variable  
 void run(){  
  speedlimit=400;                       ////Output:Compile Time Error
 }  
 public static void main(String args[]){  
 Bike9 obj=new  Bike9();  
 obj.run();  
 }  
}//end of class 

Blank uninitialized Final variable. It can be initialized only in constructor.


class Student{  

int id;  

String name;  

final String PAN_CARD_NUMBER;  

...  

}  

2. method: Cannot override Final method. It can only be inherited.


class Bike{  

  final void run(){System.out.println("running");}  

}  

     

class Honda extends Bike{  

   void run(){System.out.println("running safely with 100kmph");}   
                                              //////Output:Compile Time Error////////////////////
   public static void main(String args[]){  
   Honda honda= new Honda();  
   honda.run();  
   }  
}  


3. class: Cannot extend Final class

final class Bike{}  
  
class Honda1 extends Bike{          //////Output:Compile Time Error////////////////////
  void run(){System.out.println("running safely with 100kmph");}  
    
  public static void main(String args[]){  
  Honda1 honda= new Honda();  
  honda.run();  
  }  

}  


5. Association, Aggregation and Composition.

Association is a relationship between 2 objects. One-to-one, on-to-many, many-to-one, many-to-many

In aggregation is a special case of association, its a has- a relation, where the contained object can exist  out side container object.

In case of composition, the contained object cannot exist outside container object.


6. Covariant return types
Java 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return type should be sub-type of parent’s return type. Overriding method becomes variant with respect to return type.
// Java program to demonstrate that we can have
// different return types if return type in
// overridden method is sub-type
// Two classes used for return types.

class A {}
class B extends A {}    //HERE B extends A and is Subclass of B
class Base
{
    A fun()
    {
        System.out.println("Base fun()");
        return new A();
    }
}
class Derived extends Base
{
    B fun()
    {
        System.out.println("Derived fun()");
        return new B();
    }
}
public class Main
{
    public static void main(String args[])
    {
       Base base = new Base();
       base.fun();
       Derived derived = new Derived();
       derived.fun();
    }
}

Output:
Base fun()
Derived fun()


7. Collection Framework.

See Collection framework by Durga https://www.youtube.com/watch?v=v9zg9g_FbJY
2 Categories Items and maps.

Items 2 Categories:
List Interface: Duplicates allowed and Set Interface: No Duplicates

List Interface: 
Array List - Contiguous Allocation, Quick Insertion and Retrieval. Bad for Insertion in between. Only Iterator to be used for traversing. Traverse only front and not back.
Linked List -  Double Linked list Algorithm. Not Contiguous. Best for insertion in the middle. List Iterator can be used to traverse back and Front.

Set Interface:
Hash Set - Linked Hash Set has no Order. Insertion Order is the sequence of storage.
Sorted Set - Tree Set: Sorts in ascending Order

Queue: To store elements in FIFO prior to processing.
Vectors and Stack are Legacy types and can be traversed via Enumerator.

Maps 2 Categories: NO Duplicates
Hash Map: No Sorting Order. Insertion Order is the sequence of storage.
Tree Map: Ascending Order
Dictionary is Legacy.

The NavigableSet interface inherits from the SortedSet interface. It behaves like a SortedSet with the exception that we have navigation methods available in addition to the sorting mechanisms of the SortedSet. For example, NavigableSet interface can navigate the set in reverse order compared to the order defined in SortedSet.


To upgrade JAVA follow,

https://geekflare.com/how-to-upgrade-jdk-1-6-to-1-7-on-linux-or-centos/

How to call the method runMeInstance() of non-static and Static class which are inside the Wrapper class Outer?
========================
class Outer {
 class Inner {
  void runMeInstance() {
   System.out.println("Inside Outer-Inner-runMeInstance");
  }
 }

 static class InnerStatic {
  void runMeInstance() {
   System.out.println("Inside Outer-InnerStatic-runMeInstance");
  }
 }
}
class Invoker {
 public static void main(String[] args) throws Exception {
  // invoke runMeInstance( ) methods from both the classes here....
 }
}
Solution:
//////////////For non-Static Class///////////
Outer.Inner oi = new Outer().new Inner();
oi.runMeInstance()
//////////////For Static Class///////////
Outer.InnerStatic staticObject = new Outer.InnerStatic();
staticObject.runMeInstance()


No comments:

Post a Comment