Difference between "char" and "String" in Java
Solution 1:
char
is one character. String
is zero or more characters.
char
is a primitive type. String
is a class.
char c = 'a';
String s = "Hi!";
Note the single quotes for char
, and double quotes for String
.
Solution 2:
char
means single character. In java it is UTF-16 character.
String
can be thought as an array of chars.
So, imagine "Android" string. It consists of 'A', 'n', 'd', 'r', 'o', 'i'
and again 'd'
characters.
char
is a primitive type in java and String
is a class, which encapsulates array of chars
.
Solution 3:
In layman's term, char
is a letter, while String
is a collection of letter (or a word). The distinction of '
and "
is important, as 'Test'
is illegal in Java.
char
is a primitive type, String
is a class