What does the dereferenced compilation error mean in Java?

Asked

Viewed 755 times

4

In a test code (in Java), whose goal was to convert a string in one another string between upper and lower case letters, I received the following error at the time of compilation:

error: char cannot be dereferenced

result = Letter.toLowerCase();

Following the example code:

public void solve(String text) {
    int len = text.length();
    StringBuffer buffer = new StringBuffer(len);
    
    for(int i=0; i<len; i++){
        char letter = text.charAt(i);
        char result;
        
        if (i % 2 == 0)
            result = letter.toUpperCase(); //error: compile time
        else
            result = letter.toLowerCase(); //error: compile time
            
        buffer.append(result);
    }
    
    System.out.println(buffer.toString());
}

solve("Teste");

// Resultado esperado:
// TeStE

Why the mistake happens and what it means?

2 answers

7


The variable letter is a char, which is a primitive type of data (a 16-bit integer without a signal). It is not an object. If it were an object (such as an String or a Character) you could call methods on it as letter.toUpperCase(). But it’s not an object, so that’s not possible.

If you work with static class utility methods Character it is easier to make the conversion to uppercase or lowercase as you want:

char letter = text.charAt(i);
char result;

if (i % 2 == 0)
    result = Character.toUpperCase(letter);
else
    result = Character.toLowerCase(letter);

buffer.append(result);
  • In that case you’re not using Character as a wrapper, simply using static class methods Character. If I were to use as wrapper, in many cases the autobox / autounbox would be in charge of the conversion (although I don’t think this applies to calling methods). However, the class Character only have these methods that matter as static anyway, so your approach is correct.

  • @mgibsonbr You’re right. I corrected the answer.

4

You’re using a primitive type. The syntax used should take a reference in the variable used and primitive types have no reference, they keep the direct value without any reference. So there was a problem of non-reference (dereferenced). You have to call the static method that does not work with references and pass the character as the normal parameter. Directly use the class Character for this.

class Main {
    public static void main(String[] args)  {
        solve("Teste");
    }
    public static void solve(String text) {
        int len = text.length();
        StringBuffer buffer = new StringBuffer(len);
        for (int i = 0; i < len; i++){
            char letter = text.charAt(i);
            buffer.append(i % 2 == 0 ? Character.toUpperCase(letter) : Character.toLowerCase(letter));
        }
        System.out.println(buffer);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.