Wednesday, December 29, 2010

Java Memory Management

Where local variables in Java Store?
Ans: Local variables store inside the Stack of JVM
All class instances and Arrays are allocates in Heap Memory.
Question : What is output of the below program?
Ans :

     String s1 = "MY Name is Chakra";
     String s2 = "MY Name is Chakra";
     if(s2  == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
     }else{
System.out.println(" s1 != s2 ");
     }
     if(s1.equals(s2)){
System.out.println(" s1 equals s2 ");
     }else{
System.out.println(" s1 not equals s2");
     }
o/ps1 == s2 
      s1 equals s2 
Question 2 :

     String s1 = "MY Name";
     s1 = s1 + " is Chakra";
     String s2 = "MY Name is Chakra";
     if(s1  == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
     }else{
System.out.println(" s1 != s2 ");
     }
o/p : 
 s1 != s2 

       s1 equals s2 
Question 3 : 
     String s1 = new String("MY Name is Chakra"); 
     if(s1  == "MY Name is Chakra"){
System.out.println(" s1 == s2 ");
     }else{
System.out.println(" s1 != s2 ");
     }

o/p: s1 != s2 
     s1 equals s2 
Question : What are the interfaces a string class implements?
Ans: Serializable , Comparable, charSequence.


Question : If your class overrides the hashCode method , how will you get the 
int value of default hashCode() .
Ans : Use System.identityHashCode(obj1);


Question: how can you make your class immutable ?
Ans: Declare all variables as private, and  do not provide setters and getters.

No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...