java: boolean instanceOf Boolean?
Asked Answered
B

4

19

I'm a bit confused: I have a function, that takes an Object as argument. But the compiler does not complain if I just pass a primitive and even recognizes a boolean primitive as Boolean Object. Why is that so?

public String test(Object value)
{
   if (! (value instanceof Boolean) ) return "invalid";
   if (((Boolean) value).booleanValue() == true ) return "yes";
   if (((Boolean) value).booleanValue() == false ) return "no";
   return "dunno";
}

String result = test(true);  // will result in "yes"
Braces answered 30/8, 2010 at 13:18 Comment(1)
Note a boolean is NEVER an instance of Boolean. You can never pass a primitive to the instanceof operator. Your boolean primitive never entered this method, boxing happened when the invocation happened and method invocation conversion occurred!Nacred
F
33

Because primitive 'true' will be Autoboxed to Boolean and which is a Object.

Finley answered 30/8, 2010 at 13:20 Comment(5)
(+1) And here's some documentation to go along with itMontez
Further reading: download.oracle.com/javase/1.5.0/docs/guide/language/… @jpegzz, the code would not compile if you ran with 1.4.xHattie
interesting :) Well, the docs suggest only to use autoboxing if really neccessary so I wont. But it's nice to know that this is not a bug but a feature :)Braces
what is the meaning of ------------ if (object instanceof Boolean ) -----------------then what happends? Means what it things?Airspace
instanceof operator checks if the object is instance of Boolean ClassFinley
C
3

Like previous answers says, it's called autoboxing.

In fact, at compile-time, javac will transform your boolean primitve value into a Boolean object. Notice that typically, reverse transformation may generate very strange NullPointerException due, as an example, to the following code

Boolean b = null;
if(b==true) <<< Exception here !

You can take a look at JDK documentation for more infos.

Cartelize answered 30/8, 2010 at 13:24 Comment(2)
Did you mean Boolean b = null;??Velda
I think you meant Boolean b = null; instead of boolean b = null;. Note the capital.Sisal
U
2

This part of the method:

  if (((Boolean) value).booleanValue() == true ) return "yes";
  if (((Boolean) value).booleanValue() == false ) return "no";
  return "dunno";

Could be replaced with

  if (value == null) return "dunno";
  return value ? "yes" : "no";
Ungodly answered 30/8, 2010 at 13:23 Comment(2)
not the same as OP's code behavior. null would return "invalid" since null is not an instance of Boolean (you're missing that line from OP's code in your first code above); "dunno" would never be returned at all (by original code). Without that line your first code would throw a NPE when value is null.Vanderhoek
@Carlos, I can never remember if instanceof returns true or false with all nulls, so I usually avoid the case by checking for null beforehand.Ungodly
M
1

its called autoboxing - new with java 1.5

http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

Maggs answered 30/8, 2010 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.