File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes System.properties() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "System.properties()" Watch "System.properties()" New topic
Author

System.properties()

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Source: MasterExam



Command Line invocation : java -Daaa=ccc propertiestest

Here I selected 2 answers:
The value of aaa is ccc.
The value of bbb is aaa.

But the correct answer is The value of aaa is ccc., the other one is incorrect, why so??
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

How can you have two solutions for only one println ? Are you executing the program twice ?


[My Blog]
All roads lead to JavaRanch
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
The program prints aaa.
Actually the program didnt have any print statement, I inserted it.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Sorry, I still cannot understand why there would be two options. Can you post the exact question and the exact code ?
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


This is the code.
The command line invocation is java -Daaa=ccc propertiestest.

Now we have to choose : Choose the option which will be always true.
The correct answer is The value of property aaa is ccc.
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

For your question about the method System. getProperty(String key, String def), javadoc has the answer

Gets the system property indicated by the specified key.
First, if there is a security manager, its checkPropertyAccess method is called with the key as its argument.
If there is no current set of system properties, a set of system properties is first created and initialized in the same manner as for the getProperties method.


This happens when we pass a key and definition to getProperty method. If you pass only a key like System. getProperty("aaa") without setting the value for aaa initially, it will return null.


SCJP 5.0 and now fighting with ExtJS
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
The value of bbb is aaa, also could be the correct answer?
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Yes.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
But according to Master Exam it is not correct.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

the getProperty method in the example has two parameters. This means that if the value of the property bbb is not set, it will be set to aaa. But it is not given that the value of bbb is set or not. So it is not compulsory that bbb will be set to aaa. If bbb is set in a properties file, then bbb's value will be that value. But since the other property is set as a command line argument, so you are sure that it's value will be the value provided at the command line invocation. This is because if the property is set in a properties file or anywhere else, the value will be overridden...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
ok.Ankit.Now I got it.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Ankit Garg wrote:the getProperty method in the example has two parameters. This means that if the value of the property bbb is not set, it will be set to aaa. But it is not given that the value of bbb is set or not. So it is not compulsory that bbb will be set to aaa. If bbb is set in a properties file, then bbb's value will be that value. But since the other property is set as a command line argument, so you are sure that it's value will be the value provided at the command line invocation. This is because if the property is set in a properties file or anywhere else, the value will be overridden...

Ankit, actually the property does not get set. What getProperty(property, default) does is return default if property is not set, but property is unaffected. I know because I had this doubt myself a while back so I had to check it out in the API.
Try this:

Output is:
aaa
null


All code in my posts, unless a source is explicitly mentioned, is my own.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


Now it is www
aaa.
If property is un-affected then why is it printing aaa?
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Abhi vijay wrote:

Now it is www
aaa.
If property is un-affected then why is it printing aaa?


Because you used setProperty, instead of getProperty
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Oh..thanks...That was a printing mistake.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Hmm. I didn't knew that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.properties()