wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Significant different between == and equals()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Significant different between == and equals()?" Watch "Significant different between == and equals()?" New topic
Author

Significant different between == and equals()?

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Significant different between == and equals()?
Praveen Kumar Singh
Ranch Hand

Joined: Mar 04, 2009
Posts: 43
"==" 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.


Praveen
SCJP, SCWCD, SOA
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

== checks whether two references point to a same object or not

equals method determine whether they are meaningfully equivalent.


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051




see this code if you not understand then tell i will explain you................
Ans will be
true true false true.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

After analyzing add two line more............



and see the output..............
sharma pankaj
Greenhorn

Joined: Jul 21, 2010
Posts: 6
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

Joined: Aug 06, 2010
Posts: 173
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


Regards,
Avishkar Nikale
Vasif Mustafayev
Greenhorn

Joined: Aug 07, 2010
Posts: 14
there are difference between String object and simple objects
BE CAREFUL abaut that.
do you understand me?


|SCJP 6.0 80%| |SCWCD in progress|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Aug 06, 2010
Posts: 543
    
    4

Campbell Ritchie wrote: Google for Angelika Langer Java equals hashCode.


That was extremely helpful, thanks!


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Significant different between == and equals()?