Friday, April 20, 2012

Java Related

Why JVM
1) Platform Independent (It includes hardware and Software)
    Eg : Linux runs on 16 bit,32 bit,64 bit and the application developed in 16 bit cannot run in other bits but the application developed by java will run on 16 bit,32 bit and 64 bit also

2) Easy to install ( copy & paste)
3) Memory Management (Eg : Garbage Collector)
4) Improved Security for clients
    Before java we have security for server but we dont have security for clients.Once a client is connected to a server the server has all permissions to do on client and the server can format the disk also ,so with java we have got client side security so that server needs to authenticate with the client

 JVM is present under rt.jar

Pre-emptive multitask : Multiple threads will be loaded but only one thread will run.Threads will toggle between the themself

Muti-core Processor: This will  process multi threads at same time

Thread : Queue of executable instructions waiting for CPU time

Different Ways of creating Objects in java
a) Using new Keyword
      MyObject obj=new MyObject();

b) Using Class.forName()
      If we know the name of the class and if it has public default constructor we can create an object
      MyObject obj=(MyObject)Class.forName("Vinay.MyObject").newInstance();
      Vinay.MyObject is class name

c) Using clone
      MyObject anotherObj = new Object();
      MyObject obj = anotherObj.clone();

d) Using Deserialization
       ObjectInputStream instream=new ObjectInputStream(new FileInputStream("filename"));
       MyObject obj=(MyObject)instream.readObject();

e) Using class Loader
     this.getClass().getClassLoader().loadclass("pavan.myObject).newInstance();

What is Thread safe  ?
         Global variables are not thread safe
  
Why Java Enterprise Edition is required ?
In  standalone Java application we cannot communicate between to process.For Example If we have RAM of 2 GB and 1 GB is used by OS and 150 MB used by Notepad,250 MB used by Word and 500 MB used by Excel. Now Excel needs another  200 MB then it goes to OS and asks for it if has any free space it allocates or it will inform no memory.(if we have land of 5000 sq feet and it was divided into two as 2000 sq ft and 3000 sq ft and the owner belonging to 3000 sq ft has started constructing it and if he needs any more land he cannot take from 2000 sq ft like was if excel needs any additional memory it cannot take from other process). So in java enterprise edition we have this facility to use the process of other service so we can achieve code reuse  for invoking thse process we use RMI,EJB,JMS and WebServices.
RMI,EJB,JMS use TCP/IP so this can be used in LAN.
 To learn RMI we should know the below
   Naming Service,JNDI,JRMP,Server/Broker,URI,URL,Proxy,Dispatch,Singleton,Marshalling/UnMarshalling)
To learn WebService
 XML,XML Parsing,JAXP,JAXB,SOAP,WSDL

Creation of class
1. new
2. Class.forName
3. clone
4. newInstance
5. classLoader
6. Serilization



If we need jar while compilation from command prompt we can place the jar in current jre/lib/ext.

UpCast and DownCast

class Parent {}
class Child extend Parent {}


Parent pobj=new Child(); // Upcasting

Child cobj = (child)pobj; // DownCasting

Child chdObj= new Parent(); //Invalid Compilation Time Error
Child chdObj=pobj; //Invalid Compilation Time Error
Child chdObj=(Child)new Parent(); //Error class Cast Exception is raised

To avoid ClassCastException we can use instanceOf to identify type of object

Why we use interface in development
To develop dynamic polymorphism and Loosely coupling

Difference between new String("abc") and String str="abc";
When we use String str="abc" then the value is stored in String Constant Pooling(SCP) and this value is used for another String str1="abc" and this is the reason why String is made immutable.

String str=new String("abc") then the value is created in heap and String str1=new String("abc") will also be created in different heap.







No comments:

Post a Comment