• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

== Vs equals()

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code will give

1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");

A) Compilation error, toString() is not avialable for Byte.
B) Prints "True".
C) Prints "False".

But can anyone explain this why the answer is C in this case though object is only one.


and if the reference thing is correct then:

Double a = new Double(Double.NaN);
2: Double b = new Double(Double.NaN);
3:
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
8:
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");

A) True
True

B) True
False

C) False
True

D) False
False
why is the answer C and why over here equals() is true. while in the last it was not.

Sorry for asking too many queries but all of them are related.
I'll appreciate your effort in explaining. Without you it must have been difficult.
Thanks again!

Uzma


Just for referance my old query which is nicely answered as follows:
What is the output of the following code?

1: class MyClass
2: {
3: static int maxElements;
4:
5: MyClass(int maxElements)
6: {
7: this.maxElements = maxElements;
8: }
9:
10: }
11:
12: public class Q19
13: {
14: public static void main(String[] args)
15: {
16:
17: MyClass a = new MyClass(100);
18: MyClass b = new MyClass(100);
19:
20: if(a.equals(b))
21: System.out.println("Objects have the same values");
22: else
23: System.out.println("Objects have different values");
24: }
25: }

A) Compilation error at line 20. equals() method was not defined.
B) Compiles fine, runtime exception at line 20.
C) Prints "Objects have the same values".
D) Prints "Objects have different values".

Answer is D
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai Akbar,
Whenever u call toString() method on Byte class object ,it creates a new new String object.(Read the API).

For example: if u have Byte b1=new Byte("127");
if u call toString() method on b1, lets say 2 times,then it will create 2 new String objects.


As we know == operator compares the references,

b1.toString()==b1.toString() //returns false,two different references.

when u call equals() method on Byte class object, it compares the contents of the both objects.it returns true if they contain same values else returns false.(read API plz).

thats why, b1.equals(b1) // returns true,compares the contents!





regarding ur second doubt:-


remember one thing Double.NaN does not equal to anything including Double.NaN.

that is, Double.NaN==Anything //always returns false;
Double.NaN!=Anything //always returns true;

Hope this is corredt, any doubts .....
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checking the API...

Under the equals method for Double:

If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.


In general, NaN is non-ordinal, so relational "value" comparisons involving NaN always result in false. The only exception is NaN != NaN, which returns true.

Under the toString() method for Byte:

The value is converted to signed decimal representation and returned as a string, exactly as if the byte value were given as an argument to the toString(byte) method.


And under toString(byte b):

Returns a new String object representing the specified byte.


In other words, each time toString is called, a new String object is created. Therefore, a value comparison (==) will return false, although the equals method would return true (in this case).
 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Marc Weber I agree with you
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hay Akbar,

The confusion is due to "ref1 == ref2" & ref1.equals(ref2).

First of all in java " == " will compare the both side reference, whether they are of reference of same Object or not.
so,
if(b1.toString() == b1.toString()) ......this will be false.


As, ashok reddy devaram explained b1.toString() will create as many string object as many times they are called


Now,
Come to ref1.equals(ref2),...this is interesting. In java.lang.Object class this method is defined same as " == " . Means this will return only true if ref1 & ref2 are references of the same Object.

But in java.lang.String class as well as wrapper classes ( java.lang.Byte, Integer, Double,..etc) this equals() method is been overidden.And they are defined such as that they will compare the contents also.
so,
-----------------------------------
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);
if( a.equals(b) )....this is true.

-----------------------------------

As you know in java Super class of all class is java.lang.Object.
So if you make any class super class of that class is Object class & that class will have all the methods of Object class.
Now if you don't overide the equals() method of the Object class in your own class then it will behave same as the Object class.
so,
----------------------

MyClass a = new MyClass(100);
MyClass b = new MyClass(100);
if(a.equals(b)) .....false

I hope this will help you.
[ November 22, 2005: Message edited by: Purujit Saha ]
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much as my concept about this is cleared like a crystal

Thank you all for such a great input.

Uzma
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic