Type Types in Java

At the risk of asking a question that has already been asked, but

Is there an analog character in Java for the Type type available in C #?

What I want to do is fill the array with elements that reflect several primitive types such as int, byte, etc.

In C #, this will be the following code:

 Type[] types = new Type[] { typeof(int), typeof(byte), typeof(short) }; 
+6
source share
2 answers

Yes, they are available through their TYPE wrappers:

 Class[] types = new Class[] {Integer.TYPE, Byte.TYPE, ...}; 

You can also use the int.class syntax, which was not available in earlier versions of the language:

 Class[] types = new Class[] {int.class, byte.class, ...}; // Lowercase is important 
+10
source

You speak:

 Class[] aClass = {Integer.class, Short.class, Byte.class}; 

However, to emphasize the difference with Integer.TYPE and Integer.class : Integer.TYPE is actually a type of Class<Integer> and: Integer.TYPE equivalent to int.class

 System.out.println(Integer.class == Integer.TYPE); // false System.out.println(Integer.TYPE == int.class); // true 
+3
source

All Articles