Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abt == and equals() method

 
shweta pande
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
: hiii,
i have some doubts abt == n equals method.Here are some examples given below.
i m not able to understand the answers,can anyone help me out..



if( new Boolean("true") == new Boolean("true"))
2: System.out.println("True");
3: else
4: System.out.println("False");

Ans---False




1: 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");


Ans--False
True

it is quite confusing to differentiat between these two operations.

shweta
 
Rajiv Arora
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "==" operator simply checks for the reference equality. i.e it simply compares whether the reference variables point to the same objects in the memory or not. Whereas .equals() is meant to actually compare the object values rather than comparing their references.
 
Gautam Pandey
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever the operator new is used it creates the object in the memory and there is a reference created (memory address) for that object
So when you use the == operator to compare 2 references it matches the bit pattern of the two operands , it would never be the same unless both the references refer to the same object in memory.

But equals() is overidden in Double ( and all wrapper classes) it actually compares the values wrapped inside the Double.

If equals() is not overidden it will use the Object 's equals() method which just does (if(o1==o2) return true

Hope this helps
[ September 22, 2007: Message edited by: Gautam Pandey ]
 
Kota li
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi swetha see creating object means it will be locatining in heap of the memory.
== means same reference refer to identical object but here is dynamically creating in yhe heap.

Originally posted by Kamal Arora:
The "==" operator simply checks for the reference equality. i.e it simply compares whether the reference variables point to the same objects in the memory or not. Whereas .equals() is meant to actually compare the object values rather than comparing their references.
 
shweta pande
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but still there is a query.
In 1st and 2nd example,every thing is same so why it is giving "False"answer?
shweta
 
Henry Wong
author
Marshal
Pie
Posts: 21218
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shweta pande:
Thanks for the reply but still there is a query.
In 1st and 2nd example,every thing is same so why it is giving "False"answer?
shweta


First example:



You created two different Boolean instances -- see the two calls to the "new" operator.


Second example:



You created two different Double instances -- see the two calls to the "new" operator.


The "==" operator checks to see if they are the same object. How can they be the same object, when they are both created?

Henry
 
shweta pande
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
== operater is used for ref. variables and equals() method is used for objects.Right?
But in the given example == is used for objects rather than ref. variables.
ref. variables means whatever is given in front of class name.Right?
eg.
Double d=new Double();

here "d" is ref variable and new Double is actual Object.Right?
so when we use == for objects,it should give a error rather that giving any answer.(please see the first example)Can you explain this?
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shweta pande:
[QB]Thanks Henry
== operater is used for ref. variables and equals() method is used for objects.Right?
But in the given example == is used for objects rather than ref. variables.
QB]

Reference variables are the ways to get to the objects so == and equals() are both use reference variables to test objects.
In the first example:

you are creating an anonymous object, meaning it has no ref variable, so its ok to use it directly with ==
 
Henry Wong
author
Marshal
Pie
Posts: 21218
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== operater is used for ref. variables and equals() method is used for objects.Right?


No, the "==" operator is used to compare if two objects are the same (in memory). It is just that it is uncommon to refer to an object without a reference -- hence, you are comparing references to see if they are referring to the same object.

The equals() method is basically asking the object if the value is the same as another object. The definition of what exactly does this mean is specific to the object.

Henry
[ September 22, 2007: Message edited by: Henry Wong ]
 
Gautam Pandey
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the line simply means
if(b1==b2)
b1 and b2 are reference variables which has a bit value equals to the memory address of the 2 objects (Boolean("true") and Boolean("true")) created in two different locations.
These 2 objects live in totally different locations even though they have same values inside(i.e, "true" here).

but b1.equals(b2) goes inside these two objects and gets the values and compares against each other. It doesn't care about where the objects reside in the memory location.
 
shweta pande
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for the help.
 
shweta pande
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please give me the example where two ref.variables are compared with == operater and giving true value.
shweta
 
Henry Wong
author
Marshal
Pie
Posts: 21218
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shweta pande:
can you please give me the example where two ref.variables are compared with == operater and giving true value.
shweta


Two reference variables are "==" when they refer to the same instance, so they will return true when you assign them to the same instance, like so...


Double a = new Double(10.0);
Double b = a;


Henry
[ September 24, 2007: Message edited by: Henry Wong ]
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shweta pande:
can you please give me the example where two ref.variables are compared with == operater and giving true value.

Normally every object constructed using "new" will reside in memory in diff location.
but you can refer to one object with more than one ref variable.

prints true, because after the assignment at line 2 both ref variables d and d1 refers to the same object.
 
priti manas duddilla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the K&B book,Page 236, chapter 3,
"In order to save some memory, two instances of the following wrapper classes will be equal when there primitive values are same.

Boolean
Byte
Short and Integer(-128 to 127)
Character (/u0000 to /u007f)

So according to the above notes,shouldn't
Boolean b1 = new Boolean("true");
Boolean b2 = new Boolean("true");
(b1==b2) return true?

Please clarify.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In order to save some memory, two instances of the following wrapper classes will be equal when there primitive values are same.

Boolean
Byte
Short and Integer(-128 to 127)
Character (/u0000 to /u007f)


Thats true but only if these wrappers are created use autoboxing

prints true
 
priti manas duddilla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok...
That answers my question.Thank you!
 
shweta pande
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you explain autoboxing please,i don't know this concept.
shweta
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autoboxing is a new feature available in JDK 1.5 or later.
it makes use of wrapper objects and handles some operations automatically.
for example auto converts ints to Integers and back, so you can pass int to a method declared to take Integer.
consider this code:

remember Integer objects are immutable so when you say i1++; autoboxing handles the operation.
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


ok...so as if the "==" operator is used to compare if two objects are the same, i declared Integer b = a, but changed b's value to 6,
so a.equals(b) returns false as of different values (4 and 6),
but a and b now are two same objects (i.e. by declaring Integer b = a),
how come (a==b) still returns false!?!?!?!?!
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer objects are immutable; assigning new value to one object will refer to diff object.

At line 2 both 'a' and 'b' refer to the same object.
At line 3 'b' now refer to diff object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic