tags:

views:

427

answers:

4
+3  Q: 

boolean.class?

I noticed the other day that I can call boolean.class, but not integer.class (or on other primitives). What makes boolean so special?

Note: I'm talking about boolean.class, not Boolean.class (which would make sense).

Duh: I tried integer.class, not int.class. Don't I feel dumb :\

+5  A: 

You can do int.class. It gives the same as Integer.TYPE.

int.class.isPrimitive(), boolean.class.isPrimitive(), void.class.isPrimitive(), etc., will give a value of true. Integer.class.isPrimitive(), Boolean.class.isPrimitive(), etc., will give a value of false.

Tom Hawtin - tackline
+3  A: 

Well you can do something like int.class as well

System.out.println(int.class);

The .class keyword was introduced with Java 1.1 to have a consistent way to get the class object for class types and primitive data types.

class: Java Glossary

jitter
+3  A: 

boolean isn't special. You can call

int.class

for example. All of the primitive types have this literal. From Sun's Tutorial:

Finally, there's also a special kind of literal called a class literal, formed by taking a type name and appending ".class"; for example, String.class. This refers to the object (of type Class) that represents the type itself.

Bill the Lizard
+5  A: 

Not integer.class but int.class. Yes you can. JRE 6 :

public class TestTypeDotClass{
 public static void main(String[] args) {
  System.out.println(boolean.class.getCanonicalName());
  System.out.println(int.class.getCanonicalName());
  System.out.println(float.class.getCanonicalName());
  System.out.println(Boolean.class.getCanonicalName());
 }
}

outputs

boolean
int
float
java.lang.Boolean
subtenante