• 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 assignments

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the same question as sampath had in the below post, but need more explanation and hence am putting it as a new topic..

<<one more doubt:

char c = '\u000d';
char c1 = '\u000a';
char c2 = '\u0000';
char c3 = '\uface' ;
char c4 = '\u00001' ;
char c5 = '\101';
char c6 = 65;
char c7 = '\1001' ;

from the above declarations why c,c1 and c7 are invalid whereas c3 is valid.

Please clarify me.

Thank you
Sampath
>>

The reply to this was
<<
char c = '\u000d';
char c1 = '\u000a';

throws error because of newline charater

char c7 = '\1001' ;
throws error because u is missing
>>

Two questions
1. when I tried to compile even C4 was wrong.
2. also you have mentioned that in case of c7 only u is missing , by putting u shouldn't we get the same new line character error. Basically my question is how does JAVA recogonize that what is after /u is binary and not a string, also isn't '/u000a' a HEX, isn't a to f part of HEX.

Thanks,
JP
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. For me c4 doesn't compile

2. Java programs are written using the Unicode character set.

\uxxxx is an escape sequence for representing, in hexa, an Unicode character.

And we're talking here about each characters that compose java code, not the "char" primitive.

You can write in your code :
\u0069\u006E\u0074 myInt = 0;

It will compile as it's the same as
int myInt=0;

Hope that helps.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, Unicode char values are designated by \u followed by exactly 4 hex digits. This representation must be within single quotes so that it is interpreted as a char literal.

The following are not valid assignments because they introduce an "illegal line end in character literal":
  • '\u000a' is a line feed
  • '\u000d' is a carriage return
  • In the post above, the following assignments are not valid because they do no contain exactly 4 hex digits and/or are missing the u:
  • char c4 = '\u00001'; //5 digits
  • char c5 = '\101'; //3 digits and missing u
  • char c7 = '\1001'; //missing u

  • Ref: http://www.lookuptables.com/
    [ September 30, 2005: Message edited by: marc weber ]
     
    Seb Mathe
    Ranch Hand
    Posts: 225
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Note that \u0000 is the special "NUL" character .

    I'me not sure how it is interpreted in Java.
    Does somebody know ?
     
    Ranch Hand
    Posts: 220
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by marc weber:
    In Java, Unicode char values are designated by \u followed by exactly 4 hex digits. This representation must be within single quotes so that it is interpreted as a char literal.

    The following are not valid assignments because they introduce an "illegal line end in character literal":

  • '\u000a' is a line feed
  • '\u000d' is a carriage return
  • In the post above, the following assignments are not valid because they do no contain exactly 4 hex digits and/or are missing the u:
  • char c4 = '\u00001'; //5 digits
  • char c5 = '\101'; //3 digits and missing u
  • char c7 = '\1001'; //missing u

  • Ref: http://www.lookuptables.com/
    [ September 30, 2005: Message edited by: marc weber ]



    You're wrong about c5='\101' is a valid octal escape, and prints A
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Seb Mathe:
    Note that \u0000 is the special "NUL" character . I'me not sure how it is interpreted in Java...


    Java interprets this as a null char value ('\u0000' by any other name). But how this is actually displayed depends on the context. In a Windows Command Prompt, for example, a null char value just shows as a blank space. You might get other results in different contexts.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Akshay Kiran:
    ... You're wrong about c5='\101' is a valid octal escape, and prints A


    Yes, this is an octal escape. I meant that it is not a valid Unicode representation.
     
    Ram Murthy
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for all the resposne, so can i understand this as...

    1. If \u is present and then next 4 bytes are valid hex char (i.e between 0 to f) it is a valid char. Eg '\uface' or '\u1001'

    2. If \u is not present then it should be a octal value (between 0 thru 7) for it to be valid. Eg '\101' or '\7' etc

    Please confirm or guide me with a better thumb rule to handle such questions.

    Cheers,
    JP
     
    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,

    You are right about the statement..


     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    ...Please confirm or guide me with a better thumb rule to handle such questions.


    Just note that the maximum octal value for a char is '\377'. Values above this will cause a compiler error.
    [ October 01, 2005: Message edited by: marc weber ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic