• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Differene between == and equals() method

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


when this code executes i prints nothing.

What i would like to know is what is the difference between == operator and
equals() method.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== returns true when two reference variables are equal i.e. they refer to same objects while equals returns what public boolean equals(Object){}
for that class returns(which is overriden for some classes like String).
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am quite confused as well.
t1 and t2 obviously are same type of object and reference type,
at least using equals() would return true,
but WHY NOT??!!!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by adam lui:
i am quite confused as well.
t1 and t2 obviously are same type of object and reference type,
at least using equals() would return true,
but WHY NOT??!!!


Because the equals() method is not overridden in class test2, the equals() method of the superclass is called, which is class Object. The equals() method of class Object compares the two objects with ==. Since t1 and t2 refer to different test2 objects, it will return false.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this Post for a longer discussion.
 
Rubayat Islam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Result: s1 and s2 are same.

The == for t1 and t2 surely fails as they refer to two different Test objects. s1 and s2 also refer to two different String objects (whose values are same). Shouldn't the (s1==s2) test return false?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi...

Strings in java, are immutable...

so for code :

String a = "hello";
String b = "hello";

Both a and b will have same reference..if string variable is not exclusively assigned memory using "new" , then JVM tries to look for string object with same value and hence b will also have same reference as a !

If the code was

String a = new String("Hello");
String b = new String("Hello");

then a == b will return false...
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic