0

In all the low level languages size of char is 1 byte . So why java support 2 byte size for char type.. It might be easy but I'm very new in java that's why I request you guys to support .

Thanks

Piyush Ranjan
  • 29
  • 1
  • 8
  • Because with Unicode it's possible to encode practically every single character (that exists in every language) – dsp_user Mar 09 '17 at 19:01
  • The size of a character depends on the encoding format that you use. Some formats use even more than 2 bytes. If you use only 1 byte, you are practically limited to the old ASCII. Newer schemes support more characters, and more importantly more languages. – Loduwijk Mar 09 '17 at 19:02
  • C++ has several character datatypes and only one of them is defined as "at least 8 bits". Your assertion is simply incorrect. – v010dya Mar 09 '17 at 19:10

2 Answers2

2

In Java 'char' type represents an UNICODE character which is two bytes long

Luci
  • 1,278
  • 9
  • 17
  • 4
    Well, *was* 2 bytes long. Then they realized it's not enough and it got [even more complicated](http://stackoverflow.com/questions/5290182/how-many-bytes-does-one-unicode-character-take). – Kayaman Mar 09 '17 at 19:07
  • Yes you are right about UNICODE characters, but to preserve the compatibility of Java program the 'char' data type will remain 16 bits long. https://docs.oracle.com/javase/tutorial/i18n/text/unicode.html – Luci Mar 09 '17 at 19:18
0

char is a data structure that is 16 bits. Thus 2 bytes (8-bits each) is 16 bits.

JahKnows
  • 2,618
  • 3
  • 22
  • 37
  • 1
    Yes, but the question wants to know why. In other languages, characters are 1-byte long (well... usually; that's probably changing as unicode is gaining ground on ascii). See Luci's answer. – nasukkin Mar 09 '17 at 19:00
  • 1
    `char` is a primitive that is _always_ 16 bits long in Java, just like `int` is _always_ 32 bits long. Not "by default" because there's no other option. Furthermore, `char` is a numeric primitive, and is a "data structure" in the same way that `int` is. – Lew Bloch Mar 09 '17 at 22:04
  • I corrected the use of the English language to appease you. But, I stand by the fact that char is a data structure, much like int is as well. – JahKnows Mar 09 '17 at 22:06