How many bytes in memory a string occupy in Java language?

Question by T K: How many bytes in memory a string occupy in Java language?
I am reposting this question.
How many bytes in memory a string occupy in Java language?
Suppose i write:
String name=”Bill Gates”;
So, how much memory it requires? Whether it can store a UNICODE character in the string. How internally it is implemented, using a 1 byte char array or 2 byte unicode char array? I read somewhere that Java’s char data type is 16 bit wide.

Thanks!!!

Best answer:

Answer by tbrminsanity
It depends on your computer actually. A string can be almost any length, but it is best to keep it under 256 characters. Each character in a string takes up 2 bytes (will have a value from 00 to FF). The CHAR data type technically doesn’t use the first bit of its 2 byte code and as such only 14 bits of the 16 bits allocated have information.

Add your own answer in the comments!

Add a Comment

Your email address will not be published. Required fields are marked *