The “primitive” and “non-primitive” correspond to the two data types in Java. The latter types involve referencing objects and therefore, are also termed as “Reference types”. However, it is not the case with the former ones and so the “int cannot be dereferenced” limitation is faced usually by the noob programmers since this error is a basic one.
This guide will demonstrate the scenarios where the “int cannot be dereferenced” error is faced and the methodologies to fix it.
What is the Java “int cannot be dereferenced” Limitation?
The “int cannot be dereferenced” corresponds to the limitation in Java that is faced upon invoking a method on a primitive data type. The calling of the method can be carried out to convert an integer (primitive)variable to a String or compare it with other variables of primitive types etc. As “int” is primitive, it can’t be dereferenced and so the limitation is encountered.
Note: The “primitive” data types include “int”, “char”, “double” etc. However, the “String”, “Arrays”, “Integer” etc correspond to the “non-primitive” data types.
How to Resolve the Java “int cannot be dereferenced” Error?
This particular error can be fixed/resolved by utilizing the below-stated approaches:
- Applying the “equals()” Method With “int” Value.
- Applying the “compareTo()” Method with “int” Value.
- Applying the “toString()” Method With an “int[ ]” Array.
These above error scenarios can be resolved via the following solutions, respectively:
- Replacing “equals()” Method With the Equality Operator “==”.
- Using the “Comparison Operator” Instead of the “compareTo() Method.
- Casting int to Integer, utilizing the “Integer.toString()” Method, or changing “int[ ]” to “Integer[ ]”.
Encountered Error Case 1: Applying the “equals()” Method With “int” Value
The “equals()” method compares two strings, and gives true if the strings are equal.
Syntax
boolean equals(Object x)
Here, “x” is an object, referring to the other string to be compared.
In this case, calling the “equals()” method on the primitive data type “int” yields the discussed error:
public class DereferencedError {
public static void main(String[] args) {
int x = 45;
System.out.println(x.equals(54));
}}
According to these code lines, initialize an integer and compare it with another integer using the “equals()” method.
Output

Solution: Replacing “equals()” Method With the Equality Operator “==”
To resolve the error in the above case, simply, replace the applied method with the equality “==” operator and store it in a “Boolean” type variable to generate the outcome accordingly(as a boolean):
public class DereferencedError {
public static void main(String[] args) {
int x = 45;
Boolean y = (x == 54);
System.out.println("The comparison becomes -> "+y);
}}
Output

Encountered Error Case 2: Applying the “compareTo()” Method With “int” Value
The “compareTo()” method compares the two provided strings. The “int cannot be dereferenced” limitation can also be faced likewise upon invoking a method upon the “int” type to compare the integers.
Syntax
compareTo(x)
In the above syntax, “x” points to the string to be compared with the other string.
Return Values
0: If both the strings become equal.
< 0: If a string is less than the associated string.
> 0: if a string is greater than the associated string.
Now, consider the below-given scenario of the faced error:
public class DereferencedError {
public static void main(String[] args) {
int x = 45;
int y = 54;
System.out.println(x.compareTo(y));
}}
In this code, initialize the provided integers via the “int” type and apply the “compareTo()” method to compare both integers.
Output

Solution: Use the Comparison Operator Instead of the “compareTo() Method
The solution to the error in this scenario can be to utilize the comparison operator “<” instead of the invoked method upon the discussed data type for comparison:
public class DereferencedError {
public static void main(String[] args) {
int x = 45;
int y = 54;
boolean z = x < y;
System.out.println("The comparison becomes -> "+z);
}}
Output

Encountered Error Case 3: Applying the “toString()” Method With an “int[ ]” Array
In this scenario, the dereferenced limitation can be encountered upon accessing the “toString()” method to convert each element of the integer array into a string:
public class DereferencedError {
public static void main(String args[]) {
int[] intArray = {1, 2, 3};
for(int i=0; i< intArray.length; i++){
String result = intArray[i].toString();
System.out.print(result+ " ");
}
}}
Based on this snippet of code, initialize an integer array via the “int” type. Now, apply the “for” loop to iterate the array and transform each integer element in the array to a string via the applied “toString()” method.
The limitation in this situation can be coped with via the below-stated approaches:
- Casting/Changing “int” to “Integer”.
- Utilizing the “Integer.toString()” Method.
- Changing “int[ ]” to “Integer[ ]”.
Solution 1: Casting int to Integer(non-primitive)
In this case, cast “int” to the non-primitive type “Integer” to retrieve the appropriate outcome, demonstrated below:

Solution 2: Applying the “Integer.toString()” Method
In another case, this particular method can also be applied to retrieve the string object of the corresponding integer instead by containing the array as the method’s argument:

Solution 3: Changing int[ ] to Integer[ ]
In this scenario, using “Integer[ ]” instead of “int[ ]” can also resolve the limitation since the former is the non-primitive type:

Conclusion
The “int cannot be dereferenced” corresponds to the limitation in Java that is faced upon invoking a method on a primitive data type. It can be resolved by utilizing an operator instead of calling a method on the primitive data types. This write-up has presented a detailed guide on how to resolve the dereferencing limitation in Java.