17

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"
Gopi
  • 10,073
  • 4
  • 31
  • 45
epegzz
  • 837
  • 2
  • 10
  • 21
  • 4
    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! – Mishax Mar 19 '13 at 06:10

4 Answers4

33

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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 3
    (+1) And here's some [documentation to go along with it](http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html) – Tim Stone Aug 30 '10 at 13:22
  • Further reading: http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html @jpegzz, the code would not compile if you ran with 1.4.x – NG. Aug 30 '10 at 13:22
  • 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 :) – epegzz Aug 30 '10 at 13:27
  • what is the meaning of ------------ if (object instanceof Boolean ) -----------------then what happends? Means what it things? – alishaik786 Nov 09 '11 at 13:57
  • `instanceof` operator checks if the object is instance of Boolean Class – jmj Nov 09 '11 at 14:48
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.

Jozef Dúc
  • 965
  • 2
  • 18
  • 29
Riduidel
  • 22,052
  • 14
  • 85
  • 185
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";
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • 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`. – user85421 Aug 30 '10 at 13:33
  • @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. – Paul Tomblin Aug 30 '10 at 14:07
1

its called autoboxing - new with java 1.5

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

Adam Butler
  • 3,023
  • 5
  • 35
  • 40