File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes char  primitive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "char  primitive " Watch "char  primitive " New topic

char primitive

salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
"... the char Java primitive is actually an unsigned integer type.. "
i want to understand this phrase. can you tell me what is the meaning of this ? can you illustrate with an example
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

A char is 16 bits long. Every character corresponds to a single 16-bit non-negative number. For example, 'A' is 65. The smallest value is 0, and the largest is 65535.
You can convert between int and char using a cast. For example
int i = (int) 'A';
The variable "i" gets the value 65. Likewise,
System.out.println((char) 65);
prints "A".
Not sure what else to tell you.

[Jess in Action][AskingGoodQuestions]
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
I'l ltry to expound on what Ernest posted. A char is a 16 bit integer number. If the char variable contains the bit pattern 0000000001000001, that is the base 10 number 65. That's all it is. We can place this number in an int variable as well. The int variable would look the same, but with a lot more "0"s on the left - 16 more to be exact.
As Ernest pointed out, we can accomplish this by the following:

The compiler will let us do this because the 16 bit char value fits within the 32 bit int value without problems. This is called a widening conversion because we are putting a narrow (16 bits wide) value into a wider container (the int).
The compiler will "complain" if we try to do the opposite - put a int value into a char. Ernest pointed out that a char can hold number up to 65535. ints can hold numbers up to 2147483647 (we'll ignore negative numbers for now). What happens if I try to put the number 10000000000000000 into a char (that number is 65536 by the way)? The char can only hold the 16 zeros, but not the 1 so the char's value would be 0 and not 65536!
This type of conversion is called a narrowing conversion because we are trying to put a 32 bit wide number into a 16 bit wide container. This can cause errors as shown above, but if the int contained a value less than 65535, then it would work without problems. Since it can work sometimes, and sometimes there are valid reasons for doing these narrowing conversions, the Java language designers created a way to tell the compiler that we know the conversion can cause problems, but we are sure it is okay.
We tell the compiler to allow these narrowing conversions by "casting":

The "(char)" tells the comiler that we really, really want to convert the wide int value into a narrow char value. This same technique works for converting ints into other narrower integer types - short, byte, and char. There are comparable casts for floating point numbers as well.
Notice that I've been talking about chars as numbers and have not mentioned anything about the letter 'A'? That because to the computer everything is a number! ints, shorts, chars, and bytes are all just numbers. Letters come into play when we Humans decide that 72 69 76 76 79 is to complicated to say HELLO. We created a conversion chart that says sometimes we want to treat 65 as a alphabetic character we can recognize easier.
Actually we've created MANY conversion charts. The first was EBCDIC, then ASCII - American Standard Code for Information Interchange, then many different Unicode standards (what Java uses). These
charts map mumbers to visual characters. Certain operations on the computer, like printing to the screen, or reading from the keyboard, convert numbers to letters, or letters to numbers according to these charts. That's why we don't have to press the 65 key on the keyboard to type the letter 'A' in a document. The document contains the number 65, but when we ask the computer to display the document on the screen, it converts the 65 back to the character 'A' and places it on the monitor.
That was the long explanation - the short explanation is that chars contain numbers just like ints. The phrase "unsigned" in your question just means that chars only store positive numbers - not negative ones.
Hope this helps,

Tom Blough
[ March 24, 2004: Message edited by: Tom Blough ]

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
Tom Blough
thanks Tom....its really a good explanation. you clarified nicely and very simple helped me to understand . thanks
I agree. Here's the link:
subject: char primitive
It's not a secret anymore!