| Author |
question about properties and command line arguements
|
Brent W Farrell
Greenhorn
Joined: Apr 10, 2008
Posts: 17
|
|
Hello all, I am taking a sun practice exam for the scjp 5. Please help me with the following question: 1. class x { 2. public static void main(String [] args) { 3. String p = System.getProperty("x"); 4. if(p.equals(args[1])) 5. System.out.println("found"); 6. } 7. } Which command-line invocation will produce the output found? A java -Dx=y x y z B java -Px=y x y z C java -Dx=y x x y z D java -Px=y x x y z E java x x y z -Dx=y F java x x y z -Px=y I thought since we are comparing the property "x" to args[1] the correct answer is A, both would be y, however the exam says the answer is c. Please provide guidance.
|
 |
Ivan Ivanic
Ranch Hand
Joined: Oct 31, 2007
Posts: 100
|
|
in answer A, y is args[0] and z is args[1] args is array, thus zero indexed...
|
<a href="http://faq.javaranch.com/java/UseRealWords" target="_blank" rel="nofollow">Use Real Words</a> <a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="http://faq.javaranch.com/java/SayThanks" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14669
|
|
Command line flags are not part of the main method arguments. You could see that by looping through the args parameter. If you type "java" at the prompt, you'll some help, looking like that : Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) This [-options] like -D are not part of the command line arguments. The [args...] are. A java [-Dx=y] x [y z] : property x = y. args[1] = z B java [-Px=y] x [y z] : -P ?? C java [-Dx=y] x [x y z] : property x = y. args[1] = y D java [-Px=y] x x y z : -P ?? E java x [x y z -Dx=y] : property x does not exist. args[1] = y F java x [x y z -Px=y] : property x does not exist. args[1] = y
|
[My Blog]
All roads lead to JavaRanch
|
 |
 |
|
|
subject: question about properties and command line arguements
|
|
|