• 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

Related to Boxing

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a program like below :

public class Boxing6 {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;
System.out.println(b1 == b2);
System.out.println(b1 == b3);
System.out.println(b3 == b4);
System.out.println(b1 == b4);
}
}

and i am getting the Response as :

false
true
true
false

Could some one explain why is it so ?
Because when i see the statement from SCJP Certification Book it's like this:

In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:
n Boolean
n Byte
n Character from \u0000 to \u007f (7f is 127 in decimal)
n Short and Integer from -128 to 127
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raghavendra

In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:
n Boolean
n Byte
n Character from \u0000 to \u007f (7f is 127 in decimal)
n Short and Integer from -128 to 127



Above statements apply only when you assign primitive values to wrapper types and NOT when we use new operator.

So if you have:
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
Here: b1 == b2 gives FALSE

If you have
Boolean b3 = true;
Boolean b4 = true;
Here b3 == b4 gives TRUE

I hope this clears you question.

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

1 System.out.println(b1 == b2);
b1 and b2 are reference variables of type Boolean and both refer to two different boolean objects in memory and hence b1 == b2 will result in false, However b1.equals(b2) will result in true because equals() is overriden in Bboolean class which compares the values within the objects and as both are true the result is true.

2 System.out.println(b1 == b3);
Here b1 is reference Boolean variable of and b3 is a primitive and hence the JVM will invoke the equals() on b1 and and autoboxed boolean primitive b3 and hence the result is true.

3 System.out.println(b3 == b4);
Same as above.

4 System.out.println(b1 == b4);
Same as first one.

But am not sure why this happens [2nd case]
Thanks
Deepak
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak

You said:

2 System.out.println(b1 == b3);
Here b1 is reference Boolean variable of and b3 is a primitive and hence the JVM will invoke the equals() on b1 and and autoboxed boolean primitive b3 and hence the result is true.



I think this is wrong.
Here b1 is actually unboxed to primitive value and then compared (==) with b3.

When we compare a wrapper type and primitive value, wrapper type will be unboxed before doing comparison.

Murali...
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Murali you have said that



can you explain why this behaviour?
Both are wrapper types ,class is Boolean
than if we instantiate it gives false in first case

and if we dont instantiate it gives true in second case

can you explain why ?actualy i mean to ask in first case
it compares what? so it gives false and second case it compares what..?
so it gives true?
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't understand why is false. As per my undestanding b1 will be unboxed to primitive true ans it should result in ture.

Any rancher can please explain it

Thanks
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dhwani



In this new Boolean objects are returned at both lines 1 and 2. When we do
b1 == b2, it compares reference values of two objects and hence returns false. It is something like this:




Here also when we do o1 == o2 it gives false.

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



In this case, at Line 5 it creates a new Boolean object and assigns it to b3. At line 6, instead of creating new Boolean object it returns previously created object at Line 5 and assigns same address to b4.

So when we do b3 == b4, it compares addresses and which are same as only one object is created and hence returns true.

It is something like this:


Here if we do s1 == s2, it gives true because both s1 and s2 are pointing to same object.

Murali...
 
Polishetty Raghavendra
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Murali, Nice explanation.

Really informative to me.

Regards
Raghavendra
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What a great explanation Murali....



Thanks....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic