Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boolean: valueOf() and equality doubt 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 "Boolean: valueOf() and equality doubt" Watch "Boolean: valueOf() and equality doubt" New topic
Author

Boolean: valueOf() and equality doubt

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Output:
true
false
false

WHY THIS ANOMALY! or Do I miss something?


And this is the code straight from the Boolean class source:





PLEASE HELP!!!




Regards,
cmbhatt


cmbhatt
Arad Chear
Ranch Hand

Joined: Jan 05, 2007
Posts: 98
hi


when you type new keyword you make new Object with new reference
when you type :
Boolean b=true;
acutally i remember its will be goes for the constant pool and refer to the true value there ,
so any Boolean object without new keyword its refer to the value in the same pool ,
Boolean b2=true

now b2 refer to the b



with my modification :


Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Eisa, I think you misinterpreted me.
I gave definition of couple of methods from the Boolean (Standard API) class.
My question is, when you write,

Boolean b1 = Boolean.valueOf("true");

static method valueOf() returns wrapper boolean (Boolean). There are two
constants defined in the Boolean class named TRUE and FALSE that refer to
two Boolean objects encapsulated with true and false accordingly.

Inspired from the Boolean class I did the same,
why does my case of comparison give the false result;


PLEASE HELP!
I am in a very dilemmatic situation as "What is going wrong here???"
My question requires serious consideration. (It may prove complete silly too, but I don't know ).


PLEASE CONSIDER THE CASE, described in my previous post!

:roll: :roll: :roll:
WAITING...

cmbhatt
Fernando Espinosa
Greenhorn

Joined: Apr 21, 2007
Posts: 8
Chandra, I think you the source of your confusion might be that you MIGHT be coming from a C# experience... am I wrong?

In Java, the operator == ALWAYS tests for reference equality. ALWAYS, unlike in C# where

(1) you could overload the operator ==
(2) the corresponding Boolean type is a struct (not a class) hence a Value type.

I know, I almost fall into this pitfall since I have been coding in C# for a long period now, and such statements seem so natural to interpret in the way we got used to.

Simple, in Java == ALWAYS, ALWAYS, ALWAYS tests for reference equality.


SCJP 5.0
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

When you do this:
AWrapper a = new AWrapper(arg);
AWrapper b = new AWrapper(arg);

then (a==b) is false, because a and b are distinct and different objects. Doesn't matter if they are equal here.


When you do autoboxing (Java 5) on both wrappers:
AWrapper c = arg; // eg Boolean b = true;
AWrapper d = arg;

then one AWrapper object is created in the wrapper pool. Both variables, c and d, refer that object.
so (c==d) is true

Exception: simply spoken if c and d are bigger than a byte:
Integer exc1 = 1000;
Integer exc2 = 1000;
exc1==exc2 is false, because now there is not one Integer(1000) object in the pool (doesn't fit in) but two distinct objects on the heap.
This "bigger than byte rule" of course does not occur if the wrapper is a String...



When you only use autoboxing in one of them, the other is an object,
AWrapper e = new AWrapper(arg);
AWrapper f = arg;
then e refers to a new constructed object, but f refers to an object in the pool (if arg fits in a byte).
So e==f is false.

valueOf()
When you use valueOf(), it is the same as if you do autoboxing.

A different problem is this:

Here you digged yourself a trap. You shouldn't type this variables identifiers in uppercase letters, because:
TRUE1 and 2 are NO constants. They are objects.
Objects are never constants, only primitives can be.
But they are no primitives, they are two distinct objects, so
(TRUE1==TRUE2) is false.


And by the way, Fernando claimed:
In Java, the operator == ALWAYS tests for reference equality.


This is correct only when both operands are objects.

But if one of them is a primitive, the other is converted into primitives, you know:
Integer i1 = new Integer (-5000);
int i2 = -5000;
(i1==i2) is true.



Yours,
Bu.


all events occur in real time
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Just when I thought I had a handle on this...

public static final Boolean TRUE1 = new Boolean(true);
public static final Boolean TRUE2 = new Boolean(true);

Two different objects are created with identical content.

Boolean b3 = TRUE1;
Boolean b4 = TRUE2;

Question I have is: Are b3 and b4 autoboxed? If so wouldn�t they both refer to the same object since TRUE1 and TRUE2 have identical primitive values �true�. Or is the �new� retained(?) so that both b3 and b4 are actually referencing different objects which would explain b3==b4 resulting in false.


Richard
Fernando Espinosa
Greenhorn

Joined: Apr 21, 2007
Posts: 8
Originally posted by Burkhard Hassel:

This is correct only when both operands are objects.

But if one of them is a primitive, the other is converted into primitives, you know:
Integer i1 = new Integer (-5000);
int i2 = -5000;
(i1==i2) is true.


OMG! didn't I tell you I tend to fall in this C# vs. Java mistakes too often :S you are absolutely right. To my favour is that I mentioned the distinction between C# and Java (value vs. "reference") type. Maybe I wanted to say: for any two "objects" (that is, references to instances of a Class) the operator == ALWAYS tests for reference equality but then again, you are right: in Java, anything that is not a primitive is a reference to an object (in C# the word object is really used more like in C++, anything that is allocated somewhere is an object... great distinction, e.g. int is an alias to type System.Int32)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Fernado & Burkhard,


I passed very hard time to come out of the trap of myself. I was very badly
entrapped in that.

My mistake was, I created two reference variables are treated them as constants. As Burkhard stated, objects are never constants, only primitives can be constants, I recalled fine. Both reference variables referred to two distinct Boolean objects although wrapped with primitive true.

What lesson I got:

1- Two reference variables will result false if they refer to two distinct objects on the heap.
2- Better we use Boolean constants in place of calling constructors to create Boolean object. It saves memory and improves performance.
3- I passed a hard time with the valueOf() method, but finally I got a lot from it.




Thank you very much Burkhard for pointing out my missing things.
Thank you Fernado!



Regards,
cmbhatt
Fernando Espinosa
Greenhorn

Joined: Apr 21, 2007
Posts: 8
Originally posted by Richard Boren:
Just when I thought I had a handle on this...

public static final Boolean TRUE1 = new Boolean(true);
public static final Boolean TRUE2 = new Boolean(true);

Two different objects are created with identical content.

Boolean b3 = TRUE1;
Boolean b4 = TRUE2;

Question I have is: Are b3 and b4 autoboxed? If so wouldn�t they both refer to the same object since TRUE1 and TRUE2 have identical primitive values �true�. Or is the �new� retained(?) so that both b3 and b4 are actually referencing different objects which would explain b3==b4 resulting in false.


Richard


I understand your question, but here I autoboxing is not relevant at all: you are passing the reference "in" TRUE1 (whatever TRUE1 "points" to) to variable b3. Similarly you are passing the reference "in" TRUE2 (whatever TRUE2 "points" to) to variable b4. To begin with:



was false. Therefore, necessarily



evaluates to false. However, both



both are true. Autoboxing only comes if you mixed primitives and objects in assigments. The fact that you are creating a Boolean object passing in boolean values to the constructor is not to be confused here.
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Thanks, Fernando

I got it.

public static final TRUE1 = new Boolean(true) ;
not a constant. there's no primitive. should use Boolean's constance.

Boolean b3 = TRUE1 ;
no autboxing. no primitive invloved. plan ol' reference assignment.

Boolean b1 = true ;
autoboxing. primitive here that will be wrapped in a Boolean object.

I feel like a dog that learned a new trick and forgot all his old ones.

Richard
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean: valueOf() and equality doubt