• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Equality Comparison Operators

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
I got it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic