• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boolean: valueOf() and equality doubt

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic