Sunday 27 October 2013

In interview point of view important questions in java:



In interview point of view important questions in java:
In java mostly asked questions are based on the some methods in java.Some important methods are like toString()method,equals()method and hashCode()methods and their differences.Now we are going to see these methods and their importance.

1.toString() method:
ans:
-->generally toString() method is overriden in a class,to read an object's in a text format.
--->when we pass a java class object as parameter to the System.out.println statement internally toString() method of the object will be called.
--->if class doesn't override a toString() method then toString() method of the java.lang.Object class will be called.
--->toString() method of java.lang.Object class will return like the fallowing
       Class name@unsigned hexadecimal format of the object's hashCode.
for example:
     public class Demo
     {
        private  int x;
        public Demo(int x)
         {
           this.x=x;
          }

        public static void main(String args[])
         {
             Demo  d=new Demo(10);
              System.out.println(d):
           }
      }
-->in this above example we can not override the toString() method.So Object class toString() method will be called.we are getting the output as like the fallowing
output:
Demo@3e25a25
-->But we are override the toString()method in our program we can get the correct result.
example:
public class Demo
{
   private int x;
   public void Demo()
    {
      this.x=x;
     }
      public String toString()
      {
        return x="+x;
       }
   public static void main(String args[])
    {
       Demo d=new Demo(10);
     System.out.println(d);
    }
}
output: x=10

2.equals() method:
ans:
---->In java to compare two Objects like the fallowing.
     i) == operator for identical comparison
     ii)equals() method for meaningfully comparison.
--->The operator == returns true then two objects references are referring same object.Other wise it referring two different objects.
--->The operator equals() method returns true ,if the values are meaningfully equal other wise they are not equal.
--->if equals() method is not overriden in a class then the java.lang. Object class equals() method will be called.
-->equals()method  of object class internally calls == operator for comparing two objects.So there is no difference between the ==operator and equals() method of Object class.
example:
Note:For the wrapper classes Boolean,Integer,Short,Byte,Character.if the values of the objects are same then jvm creates only one Object.
example:
Integer i1=120;
Integer i2=120;
 i1==i2 ------>true
i1.equals(i2) ----->true

Integer i1=250;
Integer i2=250;
i1==i2 ---->false
i1.equals(i2) --->true

--->if the wrapper class range is exceeded then  two objects are created other wise it one object is created.
example:
class Main
{
  public static void main(String args[])
   {
     Integer i1=120;
     Integer i2=120;
    if(i1==i2)
   {
     System.out.println("i1 and i2 are identically equal");
    }
    if(i1.equals(i2))
    {
      System.out.println("i1 and i2 are meaningfully equal");
     }
     Integer i3=250;
     Integer i4=250;
     if(i3==i4)
     {
       System.out.println("i3 and i4are identically equal");
       }
      if(i3.equals(i4))
      {
         System.out.println("i3 and i4 are meaning fully equal");
      }
  }
}
out put: 
  i1 and i2 are identically equal
  i1 and i2 are meaning fully equal
 i1 and i2 are meaning fully equal
   

About Admin of the Blog:

Krishna babu is the founder of Alljobupdates .He is a Tech Geek,SEO Expert,Web Designer and a Pro Blogger. Contact Him Here

Follow him @ Facebook

0 comments:

Enjoyed? Share Your View With Us