• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

System.properties()

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But according to Master Exam it is not correct.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.Ankit.Now I got it.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now it is www
aaa.
If property is un-affected then why is it printing aaa?
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh..thanks...That was a printing mistake.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. I didn't knew that
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic