aspose file tools
The moose likes Java in General and the fly likes Variables names in Hex and Octal form Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Variables names in Hex and Octal form" Watch "Variables names in Hex and Octal form" New topic
Author

Variables names in Hex and Octal form

Mohamed Alla Pitchai
Greenhorn

Joined: Oct 18, 2007
Posts: 12
Hello,
Can we write a variable name with Hex or Octal notation ?

I tried the following.

int \u00C1 = 10;
int \u0041 = 15;
System.out.println("number is " + \u00C1);
System.out.println("number is " + \u0041);

and it works fine.
But when i tried to include the following statment the compiler throws me error.
int \101 = 10; //octal value for 'A'
Not sure why it is throwing error ? Does it mean we can write the variable name only through the octal representation ?

Thanks,
Mohamed
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16482
    
    2

int \101 = 10; //octal value for 'A'
Well, no, that's the octal value for the number 10. Only numbers have octal representations in Java, and variable names can't be numbers. If you experiment further, you'll find that not all Unicode escapes can be variable names either. Only the ones that produce valid variable names work. For example you'll find that \u002e isn't a valid variable name... can you see why?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[Paul C]: Well, no, that's the octal value for the number 10. Only numbers have octal representations in Java, and variable names can't be numbers.

It looks to me like that's not an octal number, but an octal escape for character and String literals. Basically, things like 01, 053, 06235424 (0 followed by one or more octal digits) are octal integer literals, and represent numbers. Things like \123, \101, \012 are octal escapes for character and string literals, and can only appear inside quote marks, either single or double. Neither of these representations is allowed for other contexts such as identifiers.
[ January 28, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 852
Originally posted by Mohamed Alla Pitchai:
I tried the following.

int \u00C1 = 10;
int \u0041 = 15;
System.out.println("number is " + \u00C1);
System.out.println("number is " + \u0041);

and it works fine.
But when i tried to include the following statment the compiler throws me error.
int \101 = 10; //octal value for 'A'
Not sure why it is throwing error ?


You seem to be confused that unicode escapes and octal character escapes are treated differently in Java.

Well, don't be. If you check the JLS you'll see that they are indeed treated differently.

Unicode escapes are processed at an early stage and may occur anywhere in the source file. ("This translation step allows any program to be expressed using only ASCII characters." Unicode characters are allowed in Java comments, identifiers, character literals, and string literals.)

Octal character escapes are not processed early and may occur only in character and string literals.


bitguru blog
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16482
    
    2

Octal escapes... learn something new every day! Pity today's something-learned is so useless.
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Perhaps worth making the point that naming variables like that, whether possible or not, is generally a very bad idea, because it makes the code much less readable.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 852
Originally posted by David Payne:
Perhaps worth making the point that naming variables like that, whether possible or not, is generally a very bad idea, because it makes the code much less readable.


Ok, but let's be a little more precise. There's nothing wrong (case aside) with naming a variable A, and \u0041 is the same as A.



So in this case it's not the name of the variable that you would quibble with, but the style in which it was declared.

Now it's dumb to use \u0041 (since it's the same as A) but I see nothing wrong with naming a variable 中 if you speak Chinese and your editor can handle unicode: int 中 = 8;

If your editor can't handle unicode, then it would be dumb to write int \u4E2D = 8. You might as well just write int middle = 8 unless you need to do something like refer to a variable named in someone else's code.
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Sorry Brian, you're absolutely right, of course - I should have thought more carefully about what I was objecting to

I did write a bleating comment about not using 中 as a variable name, or any other character that I'm too stupid to understand, but realised that my argument boiled down to just that - me being too stupid, so I've changed my mind
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Variables names in Hex and Octal form
 
Similar Threads
Java2 Sun Certified Programmer & Developer by Sierra and Bates
hexadecimal?
Sun Cirtification
assigning octal to int var
number converstion between(decimal, octal, Hex)