[Solved] int cannot be dereferenced in java

In this post, I will be sharing how to solve int cannot be dereferenced in java. If you are familiar with java, then you might know that there are two datatypes in java:
a. Primitive data type
b. Non-primitive data type

Primitive data types are int, char, float, double, boolean, byte, and short.

Read Also: java.lang.ClassNotFoundException with Example

Non-primitive data types are String, Arrays, Integer, Character etc. They are also called reference types because they refer to objects.

This error is mostly faced by java beginners and one of the most common reason is calling a method on primitive data type. Since int is primitive, it can not be dereferenced.

We will understand the error with the help of some examples. First we will produce the error before moving onto the solution.

Example 1: Producing the error by calling equals() method



If we call equals() method on primitive type int instead of ==(equality operator) to check equality.


public class JavaHungry {
    public static void main(String args[]) {
    int var = 100;
if(var.equals(100))
{ System.out.println(var); } } }

Output:
/JavaHungry.java:4: error: int cannot be dereferenced
if(var.equals(100))
^
1 error



Solution:


The above compilation error can be resolved by replacing equals() method with == equality operator.


public class JavaHungry {
    public static void main(String args[]) {
    int var = 100;
if(var == 100)
{ System.out.println(var); } } }

Output:
100



Example 2: Producing the error by calling toString() method



If we want to convert each element of the given int array to String using toString() method then we will get the int cannot be dereferenced error in java.

public class JavaHungry {
    public static void main(String args[]) {
    int[] arr = {20, 56, 70, 89};
    String output = null;
    for( int i=0; i< arr.length; i++)
        {
output = arr[i].toString();
System.out.print(output+" "); } } }


Output:
/JavaHungry.java:7: error: int cannot be dereferenced
output = arr[i].toString();
^
1 error



Solution:



The above compilation error can be resolved by three ways:
1. By casting int to Integer before calling toString() method
2. By using Integer.toString() method
3. By changing the int[] to Integer[]


1. Casting int to Integer before calling toString() method

public class JavaHungry {
    public static void main(String args[]) {
    int[] arr = {20, 56, 70, 89};
    String output = null;
    for( int i=0; i< arr.length; i++)
        {
output = ((Integer)arr[i]).toString();
System.out.print(output+" "); } } }


Output:
20 56 70 89



2. Using Integer.toString() method

public class JavaHungry {
    public static void main(String args[]) {
    int[] arr = {20, 56, 70, 89};
    String output = null;
    for( int i=0; i< arr.length; i++)
        {
output = Integer.toString(arr[i]);
System.out.print(output+" "); } } }


Output:
20 56 70 89



3. Changing the int[] to Integer[]

public class JavaHungry {
    public static void main(String args[]) {
Integer[] arr = {20, 56, 70, 89};
String output = null; for( int i=0; i< arr.length; i++) { output = arr[i].toString(); System.out.print(output+" "); } } }


Output:
20 56 70 89


That's all for today, please mention in comments in case you have any questions related to int can not be dereferenced in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry