Monday, March 9, 2009

System class

The System and Runtime classes provide a means for your programs to access system and runtime environment resources. Like the Math class, the System class is final and is entirely composed of static variables and methods. The System class basically provides a system-independent programming interface to system resources. Examples of system resources include the standard input and output streams, System.in and System.out, which typically model the keyboard and monitor.
The System class contains a set of properties as key-value pairs that define traits or attributes of the current working environment. When the runtime system first starts up, the system properties are initialized to contain information about the runtime environment. including information about the current user, the current version of the Java runtime, and even the character used to separate components of a filename.

Below is a complete list of the system properties you get when the runtime system first starts up and what they mean:
Key Meaning
------------------- ------------------------------
file.separator --------- File separator (e.g., "/")
java.class.path--------- Java classpath
java.class.version ------Java class version number
java.home -------------Java installation directory
java.vendor ------------Java vendor-specific string
java.vendor.url ---------Java vendor URL
java.version ------------ Java version number
line.separator -----------Line separator
os.arch -----------------Operating system architecture
os.name --------------- Operating system name
path.separator --------Path separator (e.g., ":")
user.dir ---------------User's current working directory
user.home -------------User home directory
user.name--------------User account name

The Runtime class provides direct access to the runtime environment. An example of a run-time routine is the freeMemory method, which returns the amount of free system memory available.


System class contains the three constant fields err, in and out.
public static final PrintStream err
public static final InputStream in
public static final PrintStream out

we are frequently used one is out ,This is standard output stream. This outputstream is already open and ready to accept output data.

System properties:

The System class has two different versions of getProperty() which retrieve the value of the property named in the argument list. The most simple of the two getProperty() methods takes a single argument: the key for the property you want to search for. For example, to get the value of path.separator, use the following statement:

System.getProperty("path.separator");

The getProperty() method returns a string containing the value of the property. If the property does not exist, this version of getProperty() returns null.

Which brings us to the next version of getProperty() method. This version requires two String arguments: the first argument is the key to look up and the second argument is a default value to return if the key cannot be found or if it has no value. For example, this call to getProperty() looks up the System property called subliminal.message. This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: Buy Java Now!.

System.getProperty("subliminal.message", "Get the Data!");

You would use this version of getProperty() if you didn't want to risk a NullPointerException, or if you really wanted to provide a default value for a property that didn't have value or that couldn't be found.

Java.util.properties can be used to access system persistent information such as version of the os,java compiler,jvm etc. in addition to providing default information,we can also add reply our own proportion
Import java.util.*;
Class sample
{
Public static void main(string[]args)
{
Properties p=system.get properties()
p.list(System.out);
p.list(“mykey”,”myvalue”);
string s=p.get property(“mykey”)
System.out.println(s);
string s=p.get property(“mykey”,”myvalue”)
System.out.println(s);
}
}
At command line
Java –D java=sun sample
Sets for java key-sun value
Programmatically we can set the system properties key using syetm.setproperty()
We can set system property at command line by using –Doption
Java –D sun=scjp

Q . which of the following is the valid way of setting system properties .
A )java –Da=b sample
B )java –Da=b sample.java // not valid
C ).java –D a=b sample //not valid

Q .consider the following
Class Testprops
{
Public static void main(string[]args)
{
String s=System.getproperty(“aaa”,”bbb”);
}
}
And fom the command line invocation
Java –Daaa=ccc testprops
Then ,what is the value set to”aaa”?
Ans:ccc

1)string s=getproperty(“keys”)
If the system property keys present return the corresponding value and of there is no such type of property return null.
2) string s=getproperty(“key1”,”value”);
If key1 system property present then the corresponding value will return
If there is no such system property instead of returning null it will return “value” As a different value.

0 comments:

Post a Comment