Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.properties()

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you have two solutions for only one println ? Are you executing the program twice ?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program prints aaa.
Actually the program didnt have any print statement, I inserted it.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of bbb is aaa, also could be the correct answer?
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But according to Master Exam it is not correct.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.Ankit.Now I got it.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now it is www
aaa.
If property is un-affected then why is it printing aaa?
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh..thanks...That was a printing mistake.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. I didn't knew that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic