Why does CHAR reserve 16 bits (2 bytes) in Java!?

exploreIT
2 min readAug 12, 2021
Why does CHAR reserve 16 bits in Java!!

If you are from C/C++ background and currently working with Java you must be familiar with C/C++ Data Types and you must know the size of ‘char’ datatype.

Yes you are right! it reserves only 1 byte in C/C++ , but when you look at Java Data Types you must be wondered seeing it has ‘char’ datatype that reserves 2 bytes (or 16 bits). BUT WHY? What makes the difference?

Hmm… the same question also came in my mind when I first observed this.

Yeah definitely! I’ll share the concept with you guys also. Well, before that we should know why C/C++ ‘char’ datatype takes 8 bits(or 1 byte) only-

The simple answer is C/C++ supports ASCII Code, and the reason Java ‘char’ takes 2 bytes is- it supports Unicode.

Hey hey! what you just said? Can you explain a little bit deeper?

Sure, here it is- You see I mentioned two terms ‘ASCII’ and ‘Unicode’, these are basically two different character encoding standards where in ASCII the character set ranges from 0–255 and on the other hand Unicode have a much wider range where the character set varies from 0–65535. Back in the days there were a lot of such standards, like- ISO/IEC 8859–5, Extended ASCII, Code page, Multinational Character Set and so on. But having such different standards caused some problems, such as-

a) A particular code value corresponds to different letters in the various language standards.

b) The encoding for languages with large character sets have variable length. Some common characters are encoded as single bytes, other require two or more byte.

Now here comes the Unicode that is considered as the universal character encoding standard which is able to handle a very big number of characters (from 0–65535). Hence Java supports Unicode standard that’s why its ‘char’ datatype takes 2 bytes or 16 bits so that it fit in the range 0–65535 and on the other hand C/C++ supports ASCII, so now you know why its ‘char’ takes 1 byte only.

Reference Quora and Others

Hey, did you like the explanation?

If yes then don’t forget to follow exploreIT as I regularly come up with interesting topics related to programming. And of course, if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!

Feet free to connect — https://www.linkedin.com/in/salman-shaikh-82989b1b9/

See you in the next article, Bye Bye!

Related Article(s)

--

--

exploreIT

This is Salman, a Software Developer. Here in exploreIT, I come up with interesting concepts related to programming that you may like. Do check & have fun:)