In this article, we are going to shed light on how to fix int cannot be dereferenced error in Java.

First, we will explain in detail what dereferencing is in Java. Then, we will illustrate, using practical examples, how to reproduce the error and how to fix it.

Dereferencing in Java

Typically, dereferencing refers to the process of accessing the state or the behavior of an object using its reference.

In Java, objects are always accessed through a reference, and the reference itself is never directly manipulated or dereferenced.

When we declare an object, what we are really creating is a reference to that object. We can then use this reference to access the object’s fields and methods using the dot (.) operator.

For example, if we have a reference variable named person that refers to an instance of the class Person, we can dereference it like this:

    
        Person person = new Person();
        person.myMethod();  // dereferencing to call a method
        int myVariable = person.myField;  // dereferencing to access a field
    

As we can see, the dot operator is used to dereference the person reference.

Solving Int Cannot Be Dereferenced Error

In Java, primitive types such as int are not objects, and therefore they cannot be dereferenced.

So, if we try to call a method or access a property using an int variable, we will get int cannot be dereferenced error.

Now that we know what causes Java to fail with the error, let’s see how to reproduce it and fix it in practice.

Calling equals()

Basically, one of the common causes of the error is comparing int values using the equals() method.

For example, let’s consider the following code:

    
        public static void main(String[] args) {
            int number = 20;
            System.out.println(number.equals(20));
        }
    

The compilation of the above code will fail because we are trying to call the equals() method on an int value.

To solve this error, we need to use the == operator instead:

    
        System.out.println(number == 20);  // true
    

Another solution would be using autoboxing, which is a feature that automatically converts primitive types to their corresponding wrapper classes.

So, let’s convert our variable number to an Integer object:

    
        Integer numberWrapper = number;
        System.out.println(numberWrapper.equals(20)); // true
    

Overall, the key point here is to convert the primitive type to an object type before calling any method on it.

Alternatively, we can use Integer.compare() to check equality of int values without worring about the dereferencing error:

    
        System.out.println(Integer.compare(number, 20));
    

Calling toString()

Another way to generate the error is invoking toString() on primitive data type int:

    
        public static void main(String[] args) {
            int num = 30;
            System.out.println(num.toString());  // compile error
        }
    

So, to avoid the compilation error when converting int to String, we can use the toString() method of the Integer class.

Here’s an example:

    
        String numStr = Integer.toString(num);
    

Alternatively, we can use concatenation with an empty string to convert an int value to a String object:

    
        String numStr = "" + num;
    

This works because when we concatenate a string with any primitive type, Java automatically invokes toString() of the corresponding wrapper class.

Calling compareTo()

The error “int cannot be dereferenced” usually occurs when we try to compare int variables using the compareTo() method.

So, to fix this error, we need to make sure that we are comparing wrapper objects and not primitive types.

Here’s an example of how to use compareTo() with Integer objects:

    
        public static void main(String[] args) {
            Integer number1 = 5;
            Integer number2 = 6;
            int result = number1.compareTo(number2);
        }
    

In case we want to compare primitive types, we can use the standard comparison operators instead of compareTo().

For example:

    
       number1 < number2; // returns true because number1 is less than number2
       number1 > number2; // returns false because number2 is greater than number1
    

Conclusion

To sum it up, we explored different ways of fixing “int cannot be dereferenced” error in Java.

Along the way, we explained the main reasons behind the error. Then, we saw how to solve it in practice.