Search code examples
javatypescharcharacterprimitive

Java:What's reason to define a character like this?


Ok, just 2 lines of code. I understand the first line. What's the meaning of the second line and when and why do i have to use this line?

char c = 'x';
Character C = new Character(c);

Please answer to all of the questions..(What's,when,why)


Solution

  • char is a primitive type. Character is the wrapper to the primitive type, as you can see in the Java documentation

    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.