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.
-
a char is a primitive type and a Character is an object, see http://stackoverflow.com/questions/8790809/whats-the-difference-between-primitive-and-reference-types – rje Jul 18 '14 at 11:04
-
[Wrapper class](http://en.wikipedia.org/wiki/Primitive_wrapper_class) vs [Primitive type](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) – Loetn Jul 18 '14 at 11:05
4 Answers
char is a primitive type that represents a single 16 bit Unicode character while Character is a wrapper class that allows us to use char primitive concept in OOP-kind of way.
Example for char,
char ch = 'a';
Example of Character,
Character.toUpperCase(ch);
It converts 'a' to 'A'

- 243
- 1
- 5
-
3Your second example takes a `char` and returns a `char` not a `Character` – Peter Lawrey Jul 18 '14 at 23:21
-
1@PeterLawrey Where does he say it returns `Character`? He's showing an example of how to use the Character class. – sansa Dec 04 '19 at 21:55
-
@sansa no idea what I was thinking, you are right, it is a `char`. – Peter Lawrey Dec 05 '19 at 18:07
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.

- 2,511
- 8
- 38
- 51
Character is an Object - thus contains a number of static methods e.g. valueOf(char),toUpperCase()
where char is a primitive data-type

- 842
- 2
- 11
- 22
char is a primitive type and Character is a class that acts as a wrapper for char.
The point of the Character class is so you can apply a range of methods to your char if needed.
More information here http://docs.oracle.com/javase/tutorial/java/data/characters.html

- 181
- 1
- 1
- 8