• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Character Literals

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls look at the following code
class D {
public static void main (String[] args) {
char a=\u0030;
char b=\u0031;
char c=\u0032;
char y=\u0039;
/* the values from \u0030 to \u0039 are working fine when assigned char variable without the necessary '' ( i mean '\u0031').However ,usage of \u0001 to \u0029 and \u0040 and greater give a compile time error.
what is the reason? shouldnt the assignment be illegal??? */
System.out.println(""+a+b+c+y);
}
}
Pls clarify !
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Substitution of unnicode characters happens before the compiler sees the code, so when your code is compiled, it looks like this:

Because 0x30... 0x39 are the ASCII for the ten decimal digits. But if you used (for example) \u0020, that's the ASCII space character, and you can see how you'd end up with a statement like

which obviously won't compile!
Anyway, the effect of "char a = \u0030;" and "char a = '0';" are quite different; in the first case, you get a char whose numeric value is zero, while in the second case, you get one whose numeric value is 48 (30 hex) but whose character value is the digit 0. Understand? In the general case, you want to use the quotes!
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, you could write your entire program in unicode characters if you wished. Would make for interesting and fun debugging sessions.
 
Eric Lidell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys for the replies.They solved my problem.
Thanks ,Ernest.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic