File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ch\u0061r a = 'a';

 
kay jee
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ch\u0061r a = 'a';
Is a valid declaration right.
but my IDE doesn't think so. His name is eclipse 3.2. and im using jdk1.5
Please help
This is wat it says:
Got an exception - unexpected char: '\'
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ch\u0061r a = 'a';
No, that doesn't make sense in Java.
 
kay jee
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey campbell,

cut copy paste "ch\u0061r a = 'a';" into your google bar.
The whole world is telling me that its a valid declaration. even my granny.

but my IDE begs to differ.

please help
 
Gavin Tranter
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works fine for me (Eclipse3.2.2 and JDK 1.5):


It works, I think all charactor data in java is unicode, rather the ascii.
Although why you would want to use unicode for the delceration of a variable rathern then its content is beyond me.

G
[ April 03, 2007: Message edited by: Gavin Tranter ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is legal according to the JLS and according to the javac compiler. The '\u0061' is equivalent to 'a', and this replacement occurs before any other lexical parsing. So the line should be equivalent to

char a = 'a';

Evidently the Eclipse developers did not anticipate that anyone would ever try to use unicode escapes in a keyword, of all things. So technically this is a bug in Eclipse. (And it looks like IntelliJ has a similar problem.) But, why does it matter? Why not simply write 'char' as 'char' rather than ch\u0061r'? Seems like a bug with a simple workaround.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
most be fixed in 3.2.2 then

G
 
kay jee
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it cud be a bug. im using eclipse 3.2.1. though its beyond me why is it working on my neighbour's machine when he is using the same version. jim did you try it in your machine.
 
Ben Simon
Greenhorn
Posts: 3
Java MySQL Database PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You probably didnt see it right. CHAR where a = \u0061 which further equals 'a'. So when you are writing ch\u0061r you just writing char and any other value would make it illegal.

Another example:


// just means float as I am replacing a in float with unicode character.
 
Shannon Pitts
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused-- why would you do this at all? Surely this is like reaching around your elbow to scratch your back?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it’s your back you are scratching?
 
Jason Bullers
Ranch Hand
Pie
Posts: 101
8
Clojure IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a thought: is there some sort of file encoding setting in the IDE that may be wrong? Probably the file is not being treated as a unicode file, which is why it doesn't work for you and works for others.

Aside from that, I'm just as confused as everyone else as to why you would want to write it like that.
 
Shannon Pitts
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are you sure it’s your back you are scratching?


Just being polite!
 
Stephan van Hulst
Bartender
Pie
Posts: 4840
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For ascii characters it doesn't make sense. Most programs are written in just ascii anyway.

However, sometimes it can be useful to include non-ascii in a program that's written in an ascii source file. This is normally the case if you want to include a unicode character into a String, but you can also use it for special characters in identifiers. The only situation I can think of where the latter would be useful is when you have to interact with third party libraries written in another language.

The \uxxxx combinations are converted to unicode characters before the parser builds the expression tree, so this is why you can also use it for keywords. You should avoid doing this though.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic