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.