wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes ch\u0061r a = 'a'; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ch\u0061r a = Watch "ch\u0061r a = New topic
Author

ch\u0061r a = 'a';

kay jee
Greenhorn

Joined: Sep 17, 2006
Posts: 21
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

Joined: Oct 13, 2005
Posts: 38363
    
  23
ch\u0061r a = 'a';
No, that doesn't make sense in Java.
kay jee
Greenhorn

Joined: Sep 17, 2006
Posts: 21
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

Joined: Jan 01, 2007
Posts: 333
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
most be fixed in 3.2.2 then

G
kay jee
Greenhorn

Joined: Sep 17, 2006
Posts: 21
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

Joined: Feb 01, 2011
Posts: 3



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

Joined: Dec 30, 2011
Posts: 13
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

Joined: Oct 13, 2005
Posts: 38363
    
  23
Are you sure it’s your back you are scratching?
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

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

Joined: Dec 30, 2011
Posts: 13
Campbell Ritchie wrote:Are you sure it’s your back you are scratching?


Just being polite!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

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
 
subject: ch\u0061r a = 'a';