File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equality Comparison Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equality Comparison Operators" Watch "Equality Comparison Operators" New topic
Author

Equality Comparison Operators

sumathi krishnan
Greenhorn

Joined: May 21, 2001
Posts: 16
public class MyClass {
public static void main(String args[]) {
String str1 = "Test One";
String str2 = new String("Test One");
if ( str1.equals( str2) ) {
System.out.println("Both are equal");
}
boolean b = true;
boolean b1 = false;
if ( b.equals(b1) ) {
System.out.println("true");
}
}
}
public class MyClass {
public static void main(String args[]) {
String s1 = new String("Test One");
String s2 = new String("Test One");
if ( s1== s2 ) {
System.out.println("Both are equal");
}
Boolean b = new Boolean(true);
Boolean b1 = new Boolean(false);
if ( b.equals(b1) ) {
System.out.println("These wrappers are equal");
}
}
}
What is the output from this code I need explanation .I mean equality comparison equals() rules.
Maitham H
Greenhorn

Joined: Jan 07, 2001
Posts: 29
sumathi,
in your first class, you can't do the following:
-------------------------
boolean b = true;
boolean b1 = false;
if ( b.equals(b1) ) {
System.out.println("true");
}
-------------------------
both b and b1 are primitive types and not Objects , and the equals method operate only on Objects.
I think the first class will not even compile.
ok , so that is out of the way, let us now talk about the difference between == and equals
the == operator evaluate both sides, and check if they are the same or in another way if the have "the same content"
for example, if you have 2 variables
int i1 = 9;
int i2 = 9;
if we use the == operator on them like this (i1==i2) we are trying to compare their values.
however you can't do (i1.equals(i2)) because as I have said, the equals only operate on Object and i1 and i2 are not objects.
now the equals operator test NOT the values of the operands, BUT THE OBJECT REFERENCE ITSELF , (i.e. it test if both operands are of the same object)
lets take an example
say we have 2 objects
String s1 = "one";
String s2 = "one";
now if you know something about the JVM, you will know that since both s1 and s2 has the value "one", then both of them refer to the same location in memory (which will contain the value "one"), therefore they both refer to the same object
so if we say:
s1.equals(s2) , this will return true.
at the same time , if we say s1==s2 , this will return true.
BUT
if we have
String s1 = "one";
String s2 = new String("one");
now s1 and s2 points to different location in memory (because we forced s2 to be a new string ) , therefore if we say
s1.equals(s2) this will return false
BUT
if we say s1==s2 , this will return true since we are comparing not the object reference, we are comparing the real value that the objects are holding, in both s1 and s2 the have "one".
and finally
if we have
String s1 = "one";
String s2 = new String("two");
and say : s1.equals(s2) , this will return false (because they are 2 different OBJECTS )
and if we say (s1==s2) , this will return false, (because they have different values)
I hope that this is clear now
Maitham

A Software life cycle can be greater than its Developer's
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Sumathi,
Please see my comments on your post below:

The bottom line is, equals method of Object compares the address of the objects being compared , so does the "==" operator. Most classes override the "eqauls" method, which compares the "deep" comparison based on element by element comparison, but in case they dont override the Objects equals method then the "equals" method and "==" operator give the same result, i.e., they compares the address location.
Hope this clears your doubt.
Ravindra Mohan

[This message has been edited by Ravindra Mohan (edited May 29, 2001).]
sumathi krishnan
Greenhorn

Joined: May 21, 2001
Posts: 16
HI!
Maithem and Ravindra,still i'm stucking with output.
According to second ex.,you are telling first block will not be executed.and second one will execute then what is the final result will print.
System.out.println("Both are equal"); // so this will not be executed..
.
.
.
.

System.out.println("These wrappers are equal"); // so this line will be executed.
Maitham H
Greenhorn

Joined: Jan 07, 2001
Posts: 29
sumathi,
lets take the second class
-------------------------
public class MyClass {
public static void main(String args[]) {
String s1 = new String("Test One");
String s2 = new String("Test One");
if ( s1== s2 ) {
System.out.println("Both are equal"); //line 1
}
Boolean b = new Boolean(true);
Boolean b1 = new Boolean(false);
if ( b.equals(b1) ) {
System.out.println("These wrappers are equal");// line 2
}
}
}
-------------------------
if you compile and run this class you will get no output
why??
lets see why.
in line 1 , this will not get execuated, because s1 and s2
are two different objects.
and we know that == test the equality of the objects, not
the equality of the value of the object.
in line 2, this will not get execauted, because b and b1 have
different values inside them , b is false, and b1 is true,
and we know that the equals() test the values of the objects.
if we want both line 1 and line 2 to get executed, we must do
the following changes to the code
-------------------------------
public class MyClass {
public static void main(String args[]) {
String s1 = new String("Test One");
String s2 = new String("Test One");
if ( s1.equals(s2) ) { // here we are using the equals() not ==
System.out.println("Both are equal"); //line 1
}
Boolean b = new Boolean(false); // here we changed b to be false
Boolean b1 = new Boolean(false);
if ( b.equals(b1) ) {
System.out.println("These wrappers are equal");// line 2
}
}
}
-------------------------------
now line 1 will get executed, becase we are using the equals(),
and this tells us to test the values of the two objects s1 and s2
and since both have the value "Test One" then the if statement
will return true, and line 1 will get executed.
line 2 will also get executed, becase now both b and b1 have
the same value (false) , and we know the the equlas(), comapre
the values of the objects, then line 2 will get execuated.
I hope this helps
Maitham
sumathi krishnan
Greenhorn

Joined: May 21, 2001
Posts: 16
Thanks guys!
I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equality Comparison Operators