• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Integer" = variable name?

 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got this question taking Jxam mock Exam:
Choose all valid variable names:
1) _int
2) %bob
3) $str
4) Integer
5) 2num
I put (1) and (3), but it also said (4) was correct.
Now I'm sure I'm sounding dumb asking this, but I thought that Integer was already a "taken" name.
Integer i = new Integer();
int i = Integer.parseInt("32");
If I'm wrong, it won't be the first time, but I'd like to please have this clarified asap.
All help is appreciated.
Thanks again,
Travis B.
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right, only 1 and 3 seem to be the right answers.
HTH,
- Manish
 
Graeme Brown
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exam is correct, try compiling this:

It will compile OK, but now you have the headache of trying to work out when you are referring to the class java.lang.Integer or to your local variable called Integer. Just because legally you can do something doesn't make it good practise to do so. That is why coding guidelines are so important.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class names are valid names for identifiers...
try this:

This code compiles and runs fine... Nothing prevents you to use class names for identifiers...
If you want more details, you can check out:
JLS 6.2 Names and Identifiers
JLS 3.8 Identifiers
[ February 26, 2002: Message edited by: Valentin Crettaz ]
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What mock exam has this? I don't believe this is good practice and should not be posed as good practice!
-Dale
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dale,
you are right this is not a good practice to code like that, but this question is only to show that such coding practices are possible. You could use such a technique in a code obfuscator for instance which would replace identifiers with completely unrelated class names. That way it would be kind of difficult to figure out what the code does... Obfuscating the code is not a secure enough technique to protect your source code, though.
[ February 26, 2002: Message edited by: Valentin Crettaz ]
 
Uday Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We should be able to use any word as identifier which does not figure in "keyword" and "reserve word" list in Java.
 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ranchers!
I'm glad that I'm not alone thinking that this was a bit questionable.
Thanks for the clarification,
Travis B.
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Travis Benning:
Thanks ranchers!
I'm glad that I'm not alone thinking that this was a bit questionable.
Thanks for the clarification,
Travis B.

I second that
- Manish
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic