What is Difference between int.class and Integer.TYPE?
Class literals have been around since Java 1.1. Everyone knows you can use Integer.class to get the Class object for java.lang.Integer. Under the covers the compiler caches a results of Class.forName("java.lang.Integer") in your class. The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
There is no different between int.class and Integer.TYPE. The Integer.TYPE and int.class point to the same class object. The Integer.TYPE is the Class instance representing the primitive type int.
public class Program {
public static void main(String[] args) {
System.out.println(int.class == Integer.TYPE);
}
}
The output is "true".
Class literals are a way to access the instances of the class Class. A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a `.' and the token class. (Please read Using Class Literals and 15.8.2 Class Literals).
Most Recent java Faqs
- How to avoid an java.util.ConcurrentModificationException with ArrayList?
- How to convert a given array to a list in Java?
- How to make Java objects eligible for garbage collection?
- What are local variables in Java?
- What are instance variables in Java?
- How many backslashes?
- What are class variables in Java?
Most Viewed java Faqs
- How to use HttpURLConnection POST data to web server?(24746)
- What is runtime polymorphism in Java?(18328)
- How to add BASIC Authentication into HttpURLConnection?(16088)
- What is String literal pool?(14754)
- Can the run() method be called directly to start a thread?(13991)
- What does Class.forname method do?(10593)
- Can transient variables be declared as 'final' or 'static'?(10446)