• 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

char literals

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

I am trying to assign value to char variable. It is accepting the integer literal with '\' preceding. ex:'\61' is accepted as char literal. where as '\81' is not accepted. Compile time error msg "unclosed character literal" is given.

Any body please tell me how to recognize which integers are allowed in quotes with '\'. And also I have doubt whether this type of questions will araise in SCJP?

Regards,
Swathi.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use the form '\xxx' for a character literal, I think xxx is an octal number.

Henry
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Henry is right. \xxx is an octal number. Hence you can use numbers only till 7.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,





Why does the compilation error occurs if the above line is uncommented ??


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

As per the Java Language Specification the escape char '\' can only be followed by any one of the following char/ values.

\b, \t,\n,\f,\r,\',\", Octal Escape.

The Octal Escape in turn can have any octal vaue < 377(Octal) i.e., decimal equivalent of 255.

As others have explained only octals can take part of a escape sequence.

To the original post as 81 is not an octal u r getting the Error.

And Mr.kumar, as ur variable d is not assigned an octal value ( x doesnot come in as octal) ur getting the compiler error.

P.S;- This is my first post into this forum so if u have any feedbacks please reply to me.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manoj Kumar Sikhakolli:
Hello all,

As per the Java Language Specification the escape char '\' can only be followed by any one of the following char/ values.

\b, \t,\n,\f,\r,\',\", Octal Escape.

The Octal Escape in turn can have any octal vaue < 377(Octal) i.e., decimal equivalent of 255.



and what about
char letterN = '\u004E'; // compiles !
character 'u' is after '\' ?
 
Manoj Kumar Sikhakolli
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi andrew I am sorry for leaving a question in your mind.

Actually, \b is the equivalent of '\u0008'

\u is for Unicode Characters.

For Octal Escape in the escape seq '\u0000' to '\u00ff' is accepted ( which is HExa equivalent of 255)
 
Swathi Sree
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thank you very much for the great explanation.

Regards,
Swathi.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic