• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Character Literals Question

 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Vella Zarb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No unsigned means that is has no sign. It can only be positive.
So from 0 to 2^16-1
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic