Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Differene between == and equals() method

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
See this Post for a longer discussion.
 
Rubayat Islam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic