Introduction to common usage of System class and Runtime class _java

Source: Internet
Author: User

Common usage of the system class
1, the main access to the system environment variable information

Copy Code code as follows:

public static void Sysprop () throws exception{
map<string,string> env = system.getenv ();
Get all environment variables for the system
For (String Name:env.keySet ()) {
SYSTEM.OUT.PRINTLN (name + ":" +env.get (name));
}
Gets the value of the system's specified environment variable
System.out.println (Env.get ("Java_home"));

Get all the properties of the system
Properties prop = System.getproperties ();
To save a system's properties to a configuration file
Prop.store (New FileOutputStream ("Prop.properties"), "System properties");
To output specific System properties
System.out.println (System.getproperty ("Os.name"));
}

2, the method operation related to the system time

Copy Code code as follows:

public static void Systime () {
Get the system's current time millisecond Currenttimemillis () (Return the current time difference from UTC 1970.1.1 00:00)
Long time = System.currenttimemillis ();
System.out.println (time);

Long time1 = System.nanotime ();//is mainly used to calculate the time difference unit nanosecond
Long Time3 = System.currenttimemillis ();
For (Long i =0l i <999l; i++) {}
Long time2 = System.nanotime ();
Long time4 = System.currenttimemillis ();
System.out.println (time2-time1+ ":" + (Time4-time3));
}

3, to identify whether two objects are the same in heap memory

Copy Code code as follows:

public static void Identityhashcode () {
STR1 STR2 is a two different string object
String str1 = new String ("HelloWorld");
String str2 = new String ("HelloWorld");
Because the string class overrides the Hashcode () method, their hashcode is the same
System.out.println (Str1.hashcode () + ":" +str2.hashcode ());
Since they are not the same object, their computed hashcode are different.
In fact, the method uses the most primitive hashcode calculation method, that is, the Hashcode calculation method of Object
System.out.println (System.identityhashcode (STR1) + ":" + System.identityhashcode (str2));
String str3 = "Hello";
String STR4 = "Hello";
Because they are referencing the same object in a constant pool, their hashcode are the same.
System.out.println (System.identityhashcode (STR3) + ":" + System.identityhashcode (STR4));
/* output is as follows
-1554135584:-1554135584
28,705,408:6,182,315
21,648,882:21,648,882
*/
}

Common usage of runtime classes
Each Java application has a Runtime class instance that enables the application to connect to the environment in which it is running.

Copy Code code as follows:

Class Runtimetest
{
public static void Main (string[] args) throws Exception
{
Getjvminfo ();
Exectest ();
}
public static void Getjvminfo () {
Getting Java runtime-related Run-time objects
Runtime RT = Runtime.getruntime ();
System.out.println ("Number of processors:" + rt.availableprocessors () + "byte");
System.out.println ("Total JVM Memory:" + rt.totalmemory () + "byte");
SYSTEM.OUT.PRINTLN ("JVM Idle Memory:" + rt.freememory () + "byte");
System.out.println ("JVM Maximum memory available:" + rt.maxmemory () + "byte");
}
public static void Exectest () throws exception{
Runtime RT = Runtime.getruntime ();
Executes the specified string command in a separate process.
Rt.exec ("MSPaint e:\\mmm.jpg");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.