This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Java Character Declaration Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Character Declaration Format" Watch "Java Character Declaration Format" New topic
Author

Java Character Declaration Format

sur manickam
Greenhorn

Joined: Dec 27, 2008
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19674
    
  18

A char is stored in two bytes, and stores the unicode value. You can confirm this with the following code:

Output:
61
1234


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sur manickam
Greenhorn

Joined: Dec 27, 2008
Posts: 6
Thank you Rob
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
As Rob has told you, it is a 16-bit unsigned integer (whole number).
sur manickam
Greenhorn

Joined: Dec 27, 2008
Posts: 6
Out of 16 bits, how many bits are needed to store one character ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

16 bits.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

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

Joined: Dec 27, 2008
Posts: 6
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

Joined: May 03, 2008
Posts: 4530
    
    5

Hmm...
http://forums.sun.com/thread.jspa?threadID=5357602


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Character Declaration Format