Gadgets

Thursday, 18 February 2016

java.lang package interview questions

java.lang package is most commonly required package for every java class, most of the commonly required classes and interface are grouped into java.lang package. This package (java.lang) is not required to import explicitly, because it is by default available to every java program.
1) What is the purpose of java.lang.Object class?
Object is a super class of every java class directly or indirectly, most of the common required methods are available in java.lang.Object class.

2) Whenever we are trying to print object reference, which method will be executed?
Whenever we are trying to print object reference then internally toString() method will be executed.
class Test
{      
            public static void main(String[] args)
            {
                          Test t=new Test();
                        System.out.println(t);
            }
}

/*
Output: 
D:\Java>javac Test.java 
D:\Java>java Test
Test@139a55
*/

3) How the toString() method is implemented in Object class?
The toString() method available in java.lang.Object class. It is implemented in Object class as shown in the below  
public String toString()
{
       return getClass().getName() +“@”+ Integer.toHexString(hashCode());
               // class name                 @      hashcode
}
 
4) What is the purpose of toString() method?
The main purpose of toString() method is overridden in all collection classes and wrapper classes because to return meaningful string representation. 

5) What is the hashCode of an object? Where it will be usefull?
Every Object contains a unique number generated by JVM, which is called hashcode. Objects are save based on hashcode, it is useful to searching operation.

6) Is it possible to override hashCode() method in our class?
hashCode() method is available in Object class, we can override hashCode() method in our class. 
class Test {   
     int number;
     public int hashCode()  {
          return number;
     }
}

7) What is the relation between toString() and hashCode()?
toString() and hashCode() methods are available in java.lang.Object class, when toString() is overridden then hashCode() method is not execute.
If we are trying to print object reference then internally toString() method will be executed then hashCode() method also executed.

8) Does hashCode represent address of an object?
Yes, hashcode is representing the address of an Object.

9) What is the purpose of equals() method?
equals() method is available in java.lang.Object class, the main purpose of equals() method is to check the equality of two Objects.

10) Is it possible to override equals() method?
Yes, we can override equals() method.

11) What are the difference between == operator and equals() method?
== operator
equals() method

== operator is for reference comparison
equals() method is for content comparison

If both references pointing same Object then it == operator returns “true”.
If both Objects are same content and different references then return “false”
if both Objects are have same content then equals() method returns “true” and if content is different then return “false”

String s1=new String("hello");
String s2=s1;
String s3=new String("hello");
System.out.println(s1==s2); // true
System.out.println(s1==s3); // false

String s1=new String("hello");
String s2=new String("hello");
String s3=new String("hi");
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false


12) What is contract between equals() method and hashCode() method?
If two Objects are equal by equals() method then that corresponding object hashcode also same.
class Test {
            public static void main(String[] args)    {
                          String s1=new String("hello");
                          String s2=new String("hello");
                          System.out.println(s1.equals(s2)); // true
                          System.out.println(s1.hashCode()); //99162322
                          System.out.println(s2.hashCode()); //99162322
            }
}

 
13) Whenever we are overriding equals() which method we have to override?
If we override equals() method then hashCode() method also override.

14) What is cloning? How we can perform cloning?
Cloning is to maintain a backup copy of object for the future purpose, by using clone() method we can perform cloning operation.
Test t1=new Test();
Test t2=(Test)t1.clone();
 
15) What is Cloneable interface?
Cloneable is interface, it doesn’t contain any methods and it is a marker interface. We can perform cloning operation by implementing Cloneable interface.

16) What is the difference between deep cloning and shallow cloning?
There are two types of cloning: deep cloning and shallow cloning
In deep cloning if we perform any changes to the object those changes won’t be reflect to the duplicates. Deep cloning is best choice for reference variables.
In shallow cloning if we perform any changes to the object those changes will be reflect to the duplicates. Shallow cloning is best choice for primitive variables.

17) What are the various methods present in java.lang.Object?
Methods
Description
public final Class getClass()
Returns the Class class object
public int hashCode()
Return the hash code number
public boolean equals(Object obj)
Compare the given objects
protected Object clone()
Create the exact copy of object
public String toString()
Return meaningful string representation
public final void notify()
Wakes up single thread
public final void notifyAll()
Wakes up all the threads
public final void wait(long timeout)

Current thread is wait for specific time milli sec
public final void wait(long timeout, int nanos)
current thread is wait for specific time milli sec, nano sec
public final void wait()
Current thread is wait for specific time
protected void finalize()

This method is invoked by the Garbage collector to perform cleanup activities just before destroying the object
18) What is the difference between String and StringBuffer? 
String is Immutable (non changeable) and StringBuffer is Mutable (changeable).
Once we create a String Object then we can’t perform any changes on that Object.
Once we create a StringBuffer Object then we can perform any changes on that Object.

19) What is the meaning of immutable and mutable?
Immutable mean that can be non changeable and Mutable is changeable.

20) With respect to equals() method what is the difference between String and Stringbuffer?
In String class .equals() method is for content comparison, if two different objects have same content then it returns “true”.
In StringBuffer class .equals() method is for content comparison, if two different objects have same content then it returns “false”.
class Test {
            public static void main(String[] args)     {
                          String s1=new String("hello");
                          String s2=new String("hello");
                          System.out.println(s1.equals(s2)); // true 
                          StringBuffer sb1=new StringBuffer("hello"); 
                          StringBuffer sb2=new StringBuffer("hello"); 
                          System.out.println(sb1.equals(sb2)); // false
            }
}
 
21) What is the StringConstantPool (SCP)?
Object creation in SCP is optional, first JVM will check is there any existed object in SCP with required content then that existing object will reuse.
Objects will be destroying automatically in SCP when system restarts.

22) Is it possible to create our own immutable class?
We can create our own immutable class by declaring class as final and final variables.

23) Which classes are immutable in java?
All the wrapper classes and String class is immutable.
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc.

24) What is the trim?
We can eliminate the space between two strings by using trim() method.

25) What is the difference between StringBuffer and StringBuilder?
StringBuffer synchronized i.e. thread safe. That mean two threads can’t perform operations on StringBuffer simultaneously.
StringBuilder is same like StringBuffer, but the difference is StringBuilder is non- synchronized i.e, not thread safe. Two threads can perform operations on StringBuilder simultaneously.

26) When we use String, StringBuffer and StringBuilder?
If our text is not changeable then we use String class.
If our text is changeable frequently then we use StringBuffer class and access only single thread at a time.
If our text is changeable frequently then we use StringBuilder class and access many thread simultaneously.

27) Explain the purpose of wrapper classes?
The main purpose of wrapper classes is wrapping the primitive types into object form, so that we can handle primitive types as object type.
28) What are the various wrapper classes present in java?

  • Byte
  • Short
  • Integer
  • Long
  • Float
  • Double
  • Character
  • Boolean 
28) How to create a wrapper class object?
We can create wrapper class object by using valueOf() method.
public static wrapper valueOf(String s)
{
} 
Example:-
Integer i=Integer.valueOf("10");
Double d=Double.valueOf("10.5");
Boolean b=Boolean.valueOf("Hello");

29) What is the purpose of the valueOf()?
The main purpose of the valueOf() method is to create wrapper class object.

30) What is the purpose of the xxxValue()?
xxxValue() method is to find the primitive types for the given wrapper object.
public int intValue()
public double doubleValue() 
Example:
Integer i=Integer.valueOf("10");
i.intValue();        //10
i.doubleValue(); //10.0

31) What is the purpose of the parseXxx()?
The purpose of the parseXxx() method is to convert string to primitive types.
public static primitive parseXxx(String s)
{
} 
Example:
int i=Integer.parseInt("10");
 
32) What is the purpose of the toString()?
toString() method is used to convert string for the given primitive types or wrapper object.
public String toString()
{
}
Example:
Integer i=new Integer(10);
i.toString();   //10
 
33) What are the AutoBoxing and AutoUnboxing?
There is no choice to perform conversion operation primitive to wrapper, wrapper to primitive until 1.4 versions. From 1.5 version we can perform those conversions.
AutoBoxing: Automatic conversion of primitive type to wrapper object by the java compiler.
AutoUnBoxing: Automatic conversion of wrapper object to primitive type by the java compiler.

34) Which objects are immutable?
All the wrapper classes are immutable. If we change it, then new object will be created.
35) What are the Child classes and wrapper classes of java.lang.Object?

No comments:

Post a Comment