FAQ

Java

JSP

Servlet


Advertisement



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).


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |