Is there a difference in size between a String "a" and a Char 'a'? Should you use a Char when you need to save single letters or numbers?
-
those are totally dif. objects that coincidentally print the same value... – ΦXocę 웃 Пepeúpa ツ Apr 03 '17 at 15:10
-
So when should I use them and whats the main difference between them? – mangaluli Apr 03 '17 at 15:12
-
primitive `char` (or `Char` if you want object) ensures that the value is single character only. – Dmitry Bychenko Apr 03 '17 at 15:12
-
maybe duplicate? http://stackoverflow.com/questions/5078314/isnt-the-size-of-character-in-java-2-bytes or at least it may help – Ispas Claudiu Apr 03 '17 at 15:12
3 Answers
Note there are actually 3 cases
String "a"
Character 'a'
char 'a'
of these, char 'a' will take up the least amount of space (2 bytes), whereas Char 'a' and String "a" are both objects and so because of the memory overhead associated with an Object, both will be approximately the same size

- 33,923
- 10
- 53
- 80
Let's get something vaguely scientific going on here. I created the following program which creates an array of 100 million characters or strings:
public class NewClass {
private static final int SIZE = 100000000;
public static void main(String... args)
{
char[] a = new char[SIZE];
for (int i = 0; i < SIZE; ++i)
{
a[i] = 'a';
}
}
}
I profiled it once using char[]
, once using String[]
, and once using Character[]
.
The amount of used heap space:
char[] : 267 million bytes
String[] : 442 million bytes
Character[] : 437 million bytes
So char
is roughly 60% of the size of a 1 character String
, which is comparable in size to a Character
(on my JVM).

- 41,989
- 11
- 82
- 128
There is no size difference between a String
of one letter and a char
of one letter (referring to the backing char
array of the String
, that is). Currently, they both require 2 bytes as per UTF-16 encoding, but non UTF-16 characters will only require 1 byte once Java 9 is released in July.
The String
will obviously use up more memory than the primitive, but it sounds like you're prematurely optimizing.

- 28,856
- 5
- 62
- 116