1. Introduction
There are instances when we need to work with primitive types and their corresponding wrapper classes in Java. When dealing with the int type and its wrapper class Integer, we may come across three different representations. These representations are Integer.class, Integer.TYPE, and int.class. Although they might seem similar at first glance, there are subtle differences between them.
In this tutorial, we’ll explore the differences between these terms and understand their significance in Java programming.
2. Integer.class
Integer.class represents the class object associated with the Integer wrapper class.
We can use it when we need to obtain information about the class itself, such as its name, superclass, interfaces, methods, and fields.
Furthermore, we can use it for reflection-related operations, like creating new instances dynamically or invoking methods at runtime.
Here’s an example demonstrating the usage of Integer.class:
@Test
public void givenIntegerClass_whenGetName_thenVerifyClassName() {
Class<Integer> integerClass = Integer.class;
Assertions.assertEquals("java.lang.Integer", integerClass.getName());
Assertions.assertEquals(Number.class, integerClass.getSuperclass());
Assertions.assertFalse(integerClass.isPrimitive());
}
Here, we obtain the class object for Integer using Integer.class, allowing us to perform various operations on it.
For example, calling getName() on the class returns “java.lang.Integer”. Another useful method is getSuperclass(), which allows us to obtain the superclass of a class. This helps to identify the inheritance relationship with the Number class.
3. Integer.TYPE
Integer.TYPE is a special constant in Java that represents the primitive type int. We can use Integer.TYPE to distinguish between primitive types and their corresponding wrapper classes. This distinction becomes particularly important in scenarios involving method overloading.
Let’s illustrate the usage of Integer.TYPE with an example:
int sum(int a, int b) {
return a + b;
}
int sum(Integer a, Integer b) {
return a + b;
}
int sum(int a, Integer b) {
return a + b;
}
In the above code, we created three overloaded sum() methods. The first sum() method takes two int primitive values and returns their sum. The second sum() method takes two Integer wrapper objects and returns their sum. The third sum method takes an int primitive value and an Integer wrapper object and returns their sum.
Let’s verify the sum of values when dealing with int and Integer types.
@Test
public void givenIntAndInteger_whenAddingValues_thenVerifySum() {
int primitiveValue = 10;
Integer wrapperValue = Integer.valueOf(primitiveValue);
Assertions.assertEquals(20, sum(primitiveValue, primitiveValue));
Assertions.assertEquals(20, sum(wrapperValue, wrapperValue));
Assertions.assertEquals(20, sum(primitiveValue, wrapperValue));
Assertions.assertEquals(Integer.TYPE.getName(), int.class.getName());
}
The main usage of the given test case is to distinguish between int primitive types and their corresponding Integer wrapper classes, which is crucial in scenarios involving method overloading.
The provided code snippet contains three assertions that verify the correct functioning of different sum() methods. The first assertion checks if the sum() method correctly adds two int primitive values, ensuring that the expected sum of 20 is returned. Similarly, the second assertion tests the sum() method with two Integer wrapper objects, expecting a sum of 20. Lastly, the third assertion validates the sum() method that takes both an int primitive value and an Integer wrapper object, confirming that it correctly computes the sum and returns the expected result of 20.
These assertions collectively ensure the proper distinction between the int primitive type and the Integer wrapper class in the overloaded sum() methods.
The code also compares the class name of int.class (which represents the int primitive type) with the class name of Integer.TYPE. The assertEquals() method asserts that both class names are equal.
4. int.class
int.class represents the class object associated with the int primitive type. To be specific, it can be used when we need to refer to the primitive type itself explicitly.
This is particularly useful in scenarios where we want to check the type of a variable or parameter. Furthermore, it allows us to perform type-specific operations directly on the primitive type.
Let’s see the usage of int.class with an example:
@Test
public void givenIntClass_whenUsingIntClass_thenVerifyIntClassProperties() {
Class<?> intClass = int.class;
Assertions.assertEquals("int", intClass.getName());
Assertions.assertTrue(intClass.isPrimitive());
Assertions.assertEquals(int.class, intClass);
}
In this example, we retrieve the class object for intValue using the getClass() method. Although intValue is a primitive int, the getClass() method returns the corresponding wrapper class java.lang.Integer. To compare it with the primitive type int, we use int.class and verify that they match.
5. Differences Between Each Type
The following table summarizes the key distinctions between Integer.class, Integer.TYPE, and int.class:
Integer.class
Integer.TYPE
int.class
Represents
The class object associated with the Integer wrapper class
int primitive type associated with the Integer wrapper class
The class object associated with the int primitive type
Usage
Reflection-related operations, obtaining class information, creating instances dynamically
Distinguishing between method overloading with Integer and int parameters
Type-specific operations directly on the int primitive
Example Usage
Integer.class.getName() returns java.lang.Integer
Integer.TYPE.getName() returns int
int.class.getSimpleName() returns int
Reflection
Used to access information about the class, such as name, superclass, methods, and fields
Not applicable, as it represents the primitive type, not a class
Can be used to access information about the class through several methods such as getName(), getModifiers(), getSuperclass(), and so on
Type Checking
Integer.class.isPrimitive() returns false
Not applicable, as it represents the primitive type, not a class
int.class.isInstance() can be used to perform type checks to determine if an object is of the int type.
Method Overloading
Not directly related to method overloading
Used to distinguish between Integer and int parameters in overloaded methods
Not directly related to method overloading
Performance
Involves the overhead of working with wrapper objects
No overhead, as it represents the primitive type directly
No overhead, as it represents the primitive type directly
6. Conclusion
In this article, we’ve explored the concepts of Integer.class, Integer.TYPE, and int.class in Java. Understanding these concepts empowers us to work efficiently with primitive types and their wrapper classes.
As always, the complete code samples for this article can be found over on GitHub.