This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

.equals()- How it works  RSS feed

 
pradeep chellappan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!