In this post, we will see how to fix error char cannot be dereferenced
in java.
Table of Contents
As we know that there are two data types in java
- 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 char
. As type of char is primitive, it can not dereferenced.
Dereference
is process of getting the value referred by a reference. Since char is primitive and already have value, char can not be dereferenced.
Let’s understand this with the help of simple examples:
Example 1 : Calling equals() method on primitive type char
We can get this error while using equals
method rather than ==
to check equality for char.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class CharEqualityCheckMain { public static void main(String[] args) { char c='A'; if(c.equals('A')) { System.out.println("value of c is A"); } else { System.out.println("value of c is not A"); } } } |
When you will compile the code, you will get below error:
CharEqualityCheckMain.java:7: error: char cannot be dereferenced
if(c.equals(‘A’))
^
1 error
Solution 1
Replace equals with ==
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 CharEqualityCheckMain { public static void main(String[] args) { char c='A'; if(c=='A') { System.out.println("value of c is A"); } else { System.out.println("value of c is not A"); } } } |
Output:
Using Character.toString() method to convert char to String
We can resolve this issue by calling Character.toString()
method to convert char to String and then calling equals
method for equality check.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class CharEqualityCheckMain { public static void main(String[] args) { char c='A'; if(Character.toString(c).equals('A')) { System.out.println("value of c is A"); } else { System.out.println("value of c is not A"); } } } |
Output:
Example 2 : Calling isLetter() method on primitive type char
You may also get this error when you are trying to call isLetter() method directly on primitive data type char.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; public class CharIsLetterMain { public static void main(String[] args) { char c ='#'; if(c.isLetter()) { System.out.println("char c is letter"); } else { System.out.println("char c is not letter"); } } } |
When you will compile the code, you will get below error:
CharIsLetterMain.java:8: error: char cannot be dereferenced
if(c.isLetter())
^
1 error
Solution 2
We are getting this error because we are calling isLetter()
method on primitive data type char rather than using `Character.isLetter() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; public class CharIsLetterMain { public static void main(String[] args) { char c ='#'; if(Character.isLetter(c)) { System.out.println("char c is letter"); } else { System.out.println("char c is not letter"); } } } |
Output:
That’s all about how to fix char cannot be dereferenced in java.