What is the difference between char and Character in Java?

前端 未结 4 910
失恋的感觉
失恋的感觉 2021-02-01 07:31

I need to know what is the difference between char and Character in Java because when I was making a java program, the char worked while the Character didn\'t work.

4条回答
  •  渐次进展
    2021-02-01 07:49

    From the JavaDoc:

    The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char. In addition, this class provides several methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

    Character information is based on the Unicode Standard, version 6.0.0.

    So, char is a primitive type while Character is a class. You can use the Character to wrap char from static methods like Character.toUpperCase(char c) to use in a more "OOP way".

    I imagine in your program there was an 'OOP' mistake(like init of a Character) rather than char vs Character mistake.

提交回复
热议问题