• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Significant different between == and equals()?

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Significant different between == and equals()?
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After analyzing add two line more............



and see the output..............
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



That was extremely helpful, thanks!
 
You've gotta fight it! Don't give in! Read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic