In this post, we will see how to resolve error int cannot be dereferenced
in java.
Table of Contents
- primitive such as int, long, char etc
- Non primitive such as String, Character etc.
One of the common reasons for this error is calling method on a primitive datatype
int
. As type of int is primitive, it can not dereferenced.
For example:
1234 int i=10;System.out.println(i.toString());To avoid this error, we shouldn’t call any method on
int
directly. You can use wrapper classInteger
instead.
1234 Integer i=10;System.out.println(i.toString());
Dereference
is process of getting the value referred by a reference. Since int is primitive and already have value, int can not be dereferenced.
Read also: Char cannot be dereferenced
Let’s understand this with the help of simple examples:
Example 1 : Calling toString() method on primitive type int
Let’s say you want to copy int array to String array and you have written below code:
1 2 3 4 5 6 7 8 9 10 |
package org.arpit.java2blog; public class CopyIntArrayToString { public static void main(String[] args) { int[] arr=new int[] {1,2,3}; String[] arrStr=new String[arr.length]; for (int i=0;i<arr.length arrstr system.out.println></arr.length> |
When you will compile the code, you will get below error:
CopyIntArrayToString.java:10: error: int cannot be dereferenced
arrStr[i]=arr[i].toString();
^
1 error
Solution 1
We are getting this error because we are calling toString()
method on primitive data type int.
There are 2 ways to fix the issue.
Change the int[] array to Integer[]
We can change int[] array to Integer[] to resolve the issue.
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; import java.util.Arrays; public class CopyIntArrayToString { public static void main(String[] args) { Integer[] arr=new Integer[] {1,2,3}; String[] arrStr=new String[arr.length]; for (int i=0;i<arr.length arrstr system.out.println></arr.length> |
Output:
Cast int to Integer before calling toString() method
We can cast int to Integer to solve int cannot be dereferenced issue.
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; import java.util.Arrays; public class CopyIntArrayToString { public static void main(String[] args) { int[] arr=new int[] {1,2,3}; String[] arrStr=new String[arr.length]; for (int i=0;i<arr.length arrstr system.out.println></arr.length> |
Output:
Use Integer.toString()
We can use Integer.toString()
method to convert int to String.
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; import java.util.Arrays; public class CopyIntArrayToString { public static void main(String[] args) { int[] arr=new int[] {1,2,3}; String[] arrStr=new String[arr.length]; for (int i=0;i<arr.length arrstr system.out.println></arr.length> |
Output:
Example 2 : Calling equals() method on primitive type int
We can get this error while using equals
method rather than ==
to check equality.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class IntEqualityCheckMain { public static void main(String[] args) { int i=10; if(i.equals(10)) { System.out.println("value of i is 10"); } else { System.out.println("value of i is not 10"); } } } |
When you will compile the code, you will get below error:
IntEqualityCheckMain.java:7: error: int cannot be dereferenced
if(i.equals(10))
^
1 error
Solution 2
We can resolve this issue by replacing equals
method with ==
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class IntEqualityCheckMain { public static void main(String[] args) { int i=10; if(i==10) { System.out.println("value of i is 10"); } else { System.out.println("value of i is not 10"); } } } |
Output:
That’s all about how to fix int cannot be dereferenced in java.