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 uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- How to use HttpURLConnection POST data to web server?
- How to add BASIC Authentication into HttpURLConnection?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- What are class variables in Java?
- What are local variables in Java?
- How to Use Updatable ResultSet in JDBC?