2013年12月15日星期日

doubt

java.system.getProperty (String key, String def) method
 This post last edited by fulijung on 2013-12-15 01:53:13
getProperty (String key, String def) Gets the system property with the specified key description ( in the configuration file to read key corresponding value from the system environment or *. properties , etc., when the key is NULL when the return value def ; when key value is not NULL , the return value for key ) .

but the value obtained following code outputs Why do not the same

public class MyClass
{
public static void main (String [] args)
{
String s1 = System.getProperty ("os.name", "My default");
/ / Notice that the property key is misspelled here:
String s2 = System.getProperty ("os-name", "My default");

/ / Notice that the output of the following statement can be
/ / different for different operating systems:
System.out.println ("The OS name is:" + s1);

/ / The following statement displays the default property name:
System.out.println ("The OS name is:" + s2);
}
}

/ *
Here are the printed results , why String s2 and String s1 definition statement as how the output is not the same?
Output:
The OS name is: Windows XP
The OS name is: My default
* /

looked api documentation is explained below , but still do not understand , I hope you use the easy-to- understand words to explain , in the end how is it ?
getProperty
public static String getProperty (String key,
String def) Gets the system property with the specified key descriptions.
First, if there is a security manager , use the key as a parameter called checkPropertyAccess methods.

If there is no current set of system properties that will be used in the same way with the getProperties method first creates and initializes the system attributes collection.


parameters:
key - the name of a system property .
def - the default value.
Returns:
string value system property , if no attribute with this key , then return to the default values.
Throws:
SecurityException - if a security manager exists and its checkPropertyAccess method does not allow access to the specified system property.
NullPointerException - if key is null.
IllegalArgumentException - if key is empty.


------ Solution ------------------------------------ --------
If the key is null, will throw NullPointerException
If the key value is not in the system properties set, return def
If the key attribute in the collection system , the return value of the property

os-name attribute is not set, of course, the return def

------ Solution ------------------------------------ --------
landlord can getProperties () and getProperty (key, def) combined to see , getProperties () Gets the collection , if not in the collection through getProperty ( key, def) when you take , you get the default value of the def .
getProperties
public static Properties getProperties () to determine the current system properties.
First, if there is a security manager, its checkPropertiesAccess not directly call methods with parameters . This may result in a security exception.
The getProperty (String) method uses the current system as a set of attributes to return Properties object . If there is no current set of system properties , you first create and initialize a set of system properties .

没有评论:

发表评论