| Author |
"Integer" = variable name?
|
Travis Benning
Ranch Hand
Joined: Jan 24, 2002
Posts: 74
|
|
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.
|
Sun Certified Programmer for Java 2 Platform
|
 |
Manish Hatwalne
Ranch Hand
Joined: Sep 22, 2001
Posts: 2573
|
|
I think you are right, only 1 and 3 seem to be the right answers. HTH, - Manish
|
 |
Graeme Brown
Ranch Hand
Joined: Oct 13, 2000
Posts: 193
|
|
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
Joined: Aug 26, 2001
Posts: 7610
|
|
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 ]
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Dale DeMott
Ranch Hand
Joined: Nov 02, 2000
Posts: 514
|
|
What mock exam has this? I don't believe this is good practice and should not be posed as good practice! -Dale
|
By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
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
Joined: Jan 22, 2002
Posts: 24
|
|
|
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
Joined: Jan 24, 2002
Posts: 74
|
|
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
Joined: Sep 22, 2001
Posts: 2573
|
|
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
|
 |
 |
|
|
subject: "Integer" = variable name?
|
|
|