ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
public class AboutStrings{
public static void main(String args[]){
String s1="hello";
String s2="hel";
String s3="lo";
String s4=s2+s3;

//to know the hash codes of s1,s4.
System.out.println(s1.hashCode());
System.out.println(s4.hashCode());
// these two s1 and s4 are having same hashcodes.

if(s1==s4){
System.out.println("s1 and s4 are same.");
}else
System.out.println("s1 and s4 are not same.");
}
}

Somebody told me that, == operator compares references of 
the objects.
In the above example even though s1 and s4 are refering to 
same object(having same hash codes),
it is printing s1 and s4 are not same.

Can anybody explain in detail why it is behaving like this?

Thanks in Advance
RavuriVinod
 Question Submitted By :: Ravurivinod
I also faced this Question!!     Rank Answer Posted By  
 
  Re: public class AboutStrings{ public static void main(String args[]){ String s1="hello"; String s2="hel"; String s3="lo"; String s4=s2+s3; //to know the hash codes of s1,s4. System.out.println(s1.hashCode()); System.out.println(s4.hashCode()); // these two s1 and s4 are having same hashcodes. if(s1==s4){ System.out.println("s1 and s4 are same."); }else System.out.println("s1 and s4 are not same."); } } Somebody told me that, == operator compares references of the objects. In the above example even though s1 and s4 are refering to same object(having same hash codes), it is printing s1 and s4 are not same. Can anybody explain in detail why it is behaving like this? Thanks in Advance RavuriVinod
Answer
# 1
because it is a string so it didn,t equate the reference .
for string u have to used s1.equals(s4) for correct 
answer.........
 
Is This Answer Correct ?    1 Yes 1 No
Saket
 
  Re: public class AboutStrings{ public static void main(String args[]){ String s1="hello"; String s2="hel"; String s3="lo"; String s4=s2+s3; //to know the hash codes of s1,s4. System.out.println(s1.hashCode()); System.out.println(s4.hashCode()); // these two s1 and s4 are having same hashcodes. if(s1==s4){ System.out.println("s1 and s4 are same."); }else System.out.println("s1 and s4 are not same."); } } Somebody told me that, == operator compares references of the objects. In the above example even though s1 and s4 are refering to same object(having same hash codes), it is printing s1 and s4 are not same. Can anybody explain in detail why it is behaving like this? Thanks in Advance RavuriVinod
Answer
# 2
it is true that == compares the refernce but in this case 
it is retuning the false because the refernce is not the 
same. 
Strings are final in Java
so s4=s2+s3 will create a new String and s1 and s4 will 
refer to two differnt strings

also hashcode will be equal for the same refernce but it 
may also be equal for differnt refernces hascode is nothing 
but a bucket so there can be multiple object refernce in 
same bucket the only condition is for equals method 
returning true must have same hashcode

so always use .equals methods and not == to compare the 
strings
 
Is This Answer Correct ?    4 Yes 0 No
Vishal Arora
 
 
 
  Re: public class AboutStrings{ public static void main(String args[]){ String s1="hello"; String s2="hel"; String s3="lo"; String s4=s2+s3; //to know the hash codes of s1,s4. System.out.println(s1.hashCode()); System.out.println(s4.hashCode()); // these two s1 and s4 are having same hashcodes. if(s1==s4){ System.out.println("s1 and s4 are same."); }else System.out.println("s1 and s4 are not same."); } } Somebody told me that, == operator compares references of the objects. In the above example even though s1 and s4 are refering to same object(having same hash codes), it is printing s1 and s4 are not same. Can anybody explain in detail why it is behaving like this? Thanks in Advance RavuriVinod
Answer
# 3
in the above example it displays the output s1 and s4 are 
same
 
Is This Answer Correct ?    0 Yes 4 No
Sakthivel(gceb)(n.p)pollachi
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
Can a class inherit the constructors of its superclass? Wipro7
1).Is Object class abstract or not? 2).Is main method(public static void main(String args[])low priority thread or high priority thread? TCS2
Who developed JScript language?  3
What is meant by constructor?  4
How to create two different thread class inside a main function?  1
How two different class threads communicate with each other?. send example code.  4
If your team member writes code with lots of static variables and static methods, will it cause any side effects? KPIT1
How can we find size of the object ? RazorSight1
printstream class method println() is calling using System class and its static object out .how it is explain any one in detail with example ? TCS1
how to convert mm/dd/yy to dd/mm/yy using collections in java. Bosch2
Why we can not force Garbage Collection?  5
What is a layout manager?  1
question on Thread synchronization Huawei1
What is synchronization? How it can be achieved?  2
Different types of Layouts?  5
Why non nested classes in java are not having marked as protected access specifier Google1
when asub class inherits a super class and overrides a public method of super class in sub class(public method in super class). why these methods needs to be public in sub class. (otherwise compile time error).  2
What is unicode?  2
when a servlet sends request for first time it uses the follwing methods a)init b)doget() c)dopost() d)service Accenture2
What is Servlet?  8
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com