Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolean.class?

Tags:

java

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 :\

like image 601
Dave Avatar asked Jun 19 '09 17:06

Dave


4 Answers

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
like image 86
glmxndr Avatar answered Sep 30 '22 00:09

glmxndr


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.

like image 30
Tom Hawtin - tackline Avatar answered Sep 30 '22 00:09

Tom Hawtin - tackline


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.

like image 21
Bill the Lizard Avatar answered Sep 29 '22 23:09

Bill the Lizard


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

like image 43
jitter Avatar answered Sep 29 '22 22:09

jitter