Common usage of System class
1. Mainly obtain system environment variable information
Copy the code code as follows:
public static void sysProp()throws Exception{
Map<String,String> env = System.getenv();
//Get all environment variables of the system
for(String name : env.keySet()){
System.out.println(name + " : " +env.get(name));
}
//Get the value of the specified environment variable of the system
System.out.println(env.get("JAVA_HOME"));
//Get all properties of the system
Properties prop = System.getProperties();
//Save the system properties to the configuration file
prop.store(new FileOutputStream("Prop.properties"),"System properties");
//Output specific system properties
System.out.println(System.getProperty("os.name"));
}
2. Method operations related to system time
Copy the code code as follows:
public static void sysTime(){
//Get the current time milliseconds of the system currentTimeMillis() (returns the time difference between the current time and UTC 1970.1.1 00:00)
Long time = System.currentTimeMillis();
System.out.println(time);
Long time1 = System.nanoTime();//Mainly used to calculate the time difference in nanoseconds
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. Identify whether the two objects are the same in the heap memory
Copy the code code as follows:
public static void identityHashCode(){
//str1 str2 are two different String objects
String str1 = new String("helloWorld");
String str2 = new String("helloWorld");
//Since the String class overrides the hashCode() method, their HashCode is the same
System.out.println(str1.hashCode()+" : "+str2.hashCode());
//Because they are not the same object, their calculated HashCode is different.
//Actually, this method uses the most original HashCode calculation method, which is the HashCode calculation method of Object.
System.out.println(System.identityHashCode(str1) + " : "+ System.identityHashCode(str2));
String str3 = "hello";
String str4 = "hello";
//Since they refer to the same object in the constant pool, their HashCode is the same.
System.out.println(System.identityHashCode(str3) + " : "+ System.identityHashCode(str4));
/*The output is as follows
-1554135584 : -1554135584
28705408 : 6182315
21648882 : 21648882
*/
}
Common usage of Runtime class
Every Java application has an instance of the Runtime class that enables the application to connect to the environment in which it runs.
Copy the code code as follows:
class RunTimeTest
{
public static void main(String[] args) throws Exception
{
getJvmInfo();
//execTest();
}
public static void getJvmInfo(){
//Get the runtime objects related to Java runtime
Runtime rt = Runtime.getRuntime();
System.out.println("Number of processors:" + rt.availableProcessors()+" byte");
System.out.println("Jvm total memory: "+ rt.totalMemory()+" byte");
System.out.println("Jvm free memory: "+ rt.freeMemory()+" byte");
System.out.println("The maximum amount of memory available for Jvm: "+ rt.maxMemory()+" byte");
}
public static void execTest()throws Exception{
Runtime rt = Runtime.getRuntime();
//Execute the specified string command in a separate process.
rt.exec("mspaint E://mmm.jpg");
}
}