aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Character Literals Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Character Literals Question" Watch "Character Literals Question" New topic
Author

Character Literals Question

Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
Hi guys,
I'm reading the SCJP 6 Study Guide book (the Kathy Sierra and Bert Bates one) and I've come to Chapter 3. I like to type out all the code in the book onto my PC for practice, and I came to the bit of Character Literals (pg 189) In the book, I quote:

Remember, characters are just 16-bit unsigned integers under the hood. That means you can assign a number literal, assuming it will fit into the unsigned 16-bit range (65535 or less). For example, the following are all legal:

char a = 0x892; //hex literal
char b = 982; //int literal
char c = (char)70000; //The cast is required; 70000 is out of char range

I tried to write a program to see if this works, but when I tried to print the value of char a, it simply printed a question mark. Same for, b, and c.
Why is this?

I have a second question, but it's not as important as the first. What is an unsigned 16-bit integer anyway?

Thanks in advance for all your help,
Mike


It is a mistake to think that you can solve any major problem with just potatoes.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The question mark is just because you don't have the correct character set installed. It could for instance be a Chinese character.
An unsigned integer is a 16 bit integer that does not have a sign (-/+). The range of a unsigned integer vs a signed integer is 2 times
bigger because you don't need a bit to indicate the sign.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
Wouter Oet wrote:The question mark is just because you don't have the correct character set installed. It could for instance be a Chinese character.
An unsigned integer is a 16 bit integer that does not have a sign (-/+). The range of a unsigned integer vs a signed integer is 2 times
bigger because you don't need a bit to indicate the sign.


I see, thanks for the quick reply...but then, how do I get the correct character set?
Mike Vella Zarb
Greenhorn

Joined: Dec 28, 2009
Posts: 21
Oh I see what you mean now....I lowered the value of char b to 1 and it gave me a smiley, a character! So basically, there was no character association for that big number, right?

And the unsigned integer thing...does this mean that the range is from -65535 to +65535?

Thanks, Mike
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Not sure but you don't really need it. Java comes with all the characters you need.
And it's not on the exam.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

No unsigned means that is has no sign. It can only be positive.
So from 0 to 2^16-1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Character Literals Question
 
Similar Threads
Some questions for newcomer as ME.Pls help ME.
Char literal assignment Question
Sun Cirtification
char cast
chars