• 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

.equals()- How it works

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

Please find the code snippets below:

class Value {
int i;
}

public class TestClass {
public static void main(String[] args) {
Value v1 = new Value() ;
Value v2 = new Value() ;
v1.i=v2.i=10;
if ( v1.equals(v2) )
System.out.println("Its here") ;
else
System.out.println("Its in else part");
}
}

The Output of the above program is "Its in else part"

Now consider the following code snippet:

public class TestClass {
public static void main(String[] args) {
Value v1 = new Value(10) ;
Value v2 = new Value() ;
v1.i=v2.i=10;
if ( v1.equals(v2) )
System.out.println("Its here") ;
else
System.out.println("Its in else part");
}
}

Now conisder the following code snippet:

lass Value{
Value( int a) {
int i=a;
}

}
public class StringEquals {
public static void main(String[] args){
Value v1 = new Value(10) ;
Value v2 = new Value(10) ;
// v1.i=v2.i=10;
if ( v1.equals(v2) )
System.out.println("Its here") ;
else
System.out.println("its here");

}
}
Output:The output is "Its here".

I am confused with the way .equals works.Can any body please explain this behaviour.

Advance thanks!
Pradeep.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that's the output in the last code?

By default, .equals will test whether or not the caller and parameter are references to the same object.

You can override equals to define what it means for two objects of type Value to be equal.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are learning how to implement the equals methods, be sure to check out Joshua Bloch's "Effective Java", chapter 3, Methods common to all objects:

http://java.sun.com/docs/books/effective/chapters.html
 
pradeep chellappan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith/Jeff,
Thank you for the Quick Reply!

Keith- In both the cases the caller and the parameter are the references.And i agree with you that .equals compares references rather than value of the objects.
I believe that in the first snippet,value of the object is considered,whereas in the second case refences are compared.If i am right then,then i am not sure how this is happening.
I really want to understand this.I am just a beginner!

Jeff-Thanks for the link.I will go through when i will get time.

Lets carry on this thread!

Thanks!
Pradeep.
 
pradeep chellappan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,

I am sorry,the output for the second snippet will be "its here"(Else part)
 
pradeep chellappan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Please ignore my previous threads,conisder this as fresh one....Sorry for the incovinence...

Snippet1
--------

public class StringEquals {
public static void main(String[] args){
Integer v1 = new Integer(10) ;
Integer v2 = new Integer(10) ;
// v1.i=v2.i=10;
if ( v1.equals(v2) )
System.out.println("Its here") ;
else
System.out.println("its hereeee");

}
}
Output:Its here.

Snippet2
--------
class Value{
Value( int a) {
int i=a;
}

}
/*class Value {
int i;
}*/
public class StringEquals {
public static void main(String[] args){
Value v1 = new Value(10) ;
Value v2 = new Value(10) ;
// v1.i=v2.i=10;
if ( v1.equals(v2) )
System.out.println("Its here") ;
else
System.out.println("its here");

}
}
Output:its here
cAN ANY BODY Explain this different scenarios.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, let me suggest you write your test code like this -- it avoids your questionable if/else/out code:

Second, if the following is your class Value, are you aware that i is a local variable, and not an instance variable?


Third, Integer and Value have different implementations of equals. If you look at the source code for Integer, you will see the following.
 
We should throw him a surprise party. It will cheer him up. We can use 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