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.
Bala
SCJP 5.0
Gonna hunt down SCWCD soon..
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
posted
0
The value of bbb is aaa, also could be the correct answer?
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 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
posted
0
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
posted
0
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