• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Character Declaration Format

 
sur manickam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we declare and store a character in Java, in which format the character is stored internally ascii or unicode or utf8?
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A char is stored in two bytes, and stores the unicode value. You can confirm this with the following code:

Output:
61
1234
 
sur manickam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Rob has told you, it is a 16-bit unsigned integer (whole number).
 
sur manickam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of 16 bits, how many bits are needed to store one character ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessarily. Only 8 are needed for ASCII characters. However, all 16 are reserved for all characters, regardless of how many are needed.
 
sur manickam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob. I also agree with you. Out of 16 bits 8 bits are needed for ascii. So in the remaining 8 bits, is it possible to add some other 8 bits? If so, how to do it?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...
http://forums.sun.com/thread.jspa?threadID=5357602
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other 8 bits are 0, and if you want it to be the same character you should keep them 0.

If you want to merge two ASCII characters into one char variable that will create a completely different character. In other words, don't do that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic