• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java 1.5 and char initialization

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am preparing for java 1.5 SCJP exam and reading Complete Java 2 Certification book from Phil Heller ans Simon Roberts.

I have doubt about one review question:

char c = 0x1234;

I made test program and this code works for me.
By the book this statement is illegal and legal one is
char c = '0x1234';

Do you have any idea, why
char c = 0x1234; should be wrong?

Thanks
Jan
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you able to compile code with statement ?
char c = '0x1234';

It should give compilation error. Because 0x1234 will be converted to integer value and you can not assign '123' or so to char type. Though you can assign single digit 0-9 to char (c='1' ;) .

Now the statement char c = 0x1234 is legal as long as the literal value of 0x1234 within a range of type char (max is 2^16-1)


[disabled smily]
[ November 24, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a mistake in the book. However, notice that the int hexadecimal literal is within the range of a char (0 to 0xFFFF). If you used 0x10000 it needs a cast.

In fact char x = '0x1234' is the illegal one, it should be '\u1234'.

See the Java Language Specification, 3rd Ed
[ November 24, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the 1.5 test last week and there were no such questions on the test. It could be that I just didn't get any, or they are no longer asking about that. But either way it will most likely not be a major point on the test if it is even on there at all.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic