File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Significant different between == and equals()?

 
André Asantos
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Significant different between == and equals()?
 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"==" is just check the refrence i.e. two object point to same memory location or not

equal, not only check '==' its also check the content of object

more about equals, open the source code of String class.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== checks whether two references point to a same object or not

equals method determine whether they are meaningfully equivalent.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



see this code if you not understand then tell i will explain you................
Ans will be
true true false true.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After analyzing add two line more............



and see the output..............
 
sharma pankaj
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
buddy check out this code..

String s1=new String("testing").intern();
String s2=new String("testing").intern();

System.out.println(s1==s2); //it will print TRUE

Hence in case of intern (first the string is searched for the corresponding string in the pool of string.. if the match is found then the corresponding reference is given to the object.. otherwise new object is created.. if i dont write intern there...
then it wont search in the pool. and will create a new object..
thats the wastage of memory.

if you write String s="testing" // then it will automatically call intern method (implicit call)

but not in the case of String s=new String("testing"); // you have to explicitly provide the intern method
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pankaj sharma newbie wrote:buddy check out this code..

String s1=new String("testing").intern();
String s2=new String("testing").intern();

System.out.println(s1==s2); //it will print TRUE

Hence in case of intern (first the string is searched for the corresponding string in the pool of string.. if the match is found then the corresponding reference is given to the object.. otherwise new object is created.. if i dont write intern there...
then it wont search in the pool. and will create a new object..
thats the wastage of memory.

if you write String s="testing" // then it will automatically call intern method (implicit call)

but not in the case of String s=new String("testing"); // you have to explicitly provide the intern method


Nice one Pankaj
 
Vasif Mustafayev
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are difference between String object and simple objects
BE CAREFUL abaut that.
do you understand me?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vasif Mustafayev wrote:there are difference between String object and simple objects
BE CAREFUL abaut that.
do you understand me?



YES..you are right
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where have you read about the equals() method? You ought to look in the API documentation, in Bloch's Effective Java™ (you may be able to find a "sample chapter" from the 1st edition on the internet, which describes equals()), or Google for Angelika Langer Java equals hashCode. There is a lot of useful information there.
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: Google for Angelika Langer Java equals hashCode.


That was extremely helpful, thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic