Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Char cannot be dereferenced" error

I'm trying to use the char method isLetter(), which is supposed to return boolean value corresponding to whether the character is a letter. But when I call the method, I get an error stating that "char cannot be dereferenced." I don't know what it means to dereference a char or how to fix the error. the statement in question is:

if (ch.isLetter()) 
{
....
....
}

Any help? What does it mean to dereference a char and how do I avoid doing so?

like image 278
user658168 Avatar asked Apr 03 '11 02:04

user658168


People also ask

Why does it say char Cannot be dereferenced?

Dereferencing is the process of accessing the value referred to by a reference. Since a char is already a value (not a reference), it can not be dereferenced.

What is dereferenced in Java?

In Java, a reference is an address to some object/variable. Dereferencing means the action of accessing an object's features through a reference. Performing any dereferencing on a primitive will result in the error “X cannot be dereferenced”, where X is a primitive type.

Which type of variable Cannot be dereferenced?

As we can see in the example mentioned above is an integer(int), which is a primitive type, and hence it cannot be dereferenced.

What does double Cannot be dereferenced mean?

double cannot be dereferenced is the error some Java compilers give when you try to call a method on a primitive.


2 Answers

The type char is a primitive -- not an object -- so it cannot be dereferenced

Dereferencing is the process of accessing the value referred to by a reference. Since a char is already a value (not a reference), it can not be dereferenced.

use Character class:

if(Character.isLetter(c)) {
like image 132
manji Avatar answered Oct 10 '22 19:10

manji


A char doesn't have any methods - it's a Java primitive. You're looking for the Character wrapper class.

The usage would be:

if(Character.isLetter(ch)) { //... }
like image 43
no.good.at.coding Avatar answered Oct 10 '22 21:10

no.good.at.coding