Class System
The java.lang.System class represents the running platform of the current Java program.
|-It can do input and output, garbage collection.
|-Time available;
|-Environment variables can be obtained;
|-Access to system information;
|-You can get the original hashcode of the object. (for example, the String class rewrites the hashcode method and cannot uniquely identify an object.)
Acquisition time
public class System1 time { public static void main(String[] args) { System.out.println("--currentTimeMillis(): UTC(World standard time) 1970.1.1 Time difference between the beginning and the present"); System.out.println("Millisecond:" + System.currentTimeMillis()); System.out.println("nanoTime()Can only be used to measure elapsed time"); System.out.println("Nanosecond:" + System.nanoTime()); } }
System.currentTimeMillis() allows the user to calculate the time spent running a piece of code.
The accuracy of System.nanoTime() is too high, and the hardware environment may not be accurate to nanosecond, so this method is not commonly used.
Get environment variable
import java.util.Map; public class System2 environment variable { public static void main(String[] args) { String _Environment variable name = "JAVA_HOME"; System.out.println(_Environment variable name + " = " + System.getenv(_Environment variable name)); System.out.println("-----Environment variables (all)-----"); Map<String, String> env = System.getenv(); for (String name : env.keySet()) { System.out.println(name + " = " + env.get(name)); } } }
Get system information
import java.util.Properties; public class System3getProperty { public static void main(String[] args) { System.out.println(System.getProperty("os.name")); System.out.println(System.getProperty("user.dir"));// User work path System.out.println("-----System Property(ALL)-----"); Properties props = System.getProperties(); for (Object k : props.keySet()) { String v = props.getProperty(k.toString()); System.out.println(k); System.out.println(k + " = " + v); } } }
Get the original HashCode of the object
(for example, the String class rewrites the hashCode method and cannot uniquely identify an object.)
public class System4IdentityHashCode { public static void main(String[] args) { System.out.println("---identityHashCode(): Different objects, this result must be different---"); System.out.println("---because hashCode()May be rewritten (e.g. String Class), object cannot be determined accurately---"); // s1 and s2 are two different objects String s1 = new String("Hello"); String s2 = new String("Hello"); // String rewrites the hashCode() method -- to "calculate the hashCode value based on the character sequence", // Because s1 and s2 have the same character sequence, their hashcodes () are the same // (it has nothing to do with equals. Equals is not judged according to hashCode, but it is also compared according to character sequence, and the same result can be achieved in different ways.) System.out.println("new String 1.hashCode() = " + s1.hashCode()); System.out.println("new String 2.hashCode() = " + s2.hashCode()); // s1 and s2 are different string objects, so their identityHashCode values are different System.out.println("new String 1.identityHashCode() = " + System.identityHashCode(s1)); System.out.println("new String 2.identityHashCode() = " + System.identityHashCode(s2)); String s3 = "Java"; String s4 = "Java"; // s3 and s4 are the same string objects, so their identityHashCode values are the same System.out.println("String 3.identityHashCode() = " + System.identityHashCode(s3)); System.out.println("String 4.identityHashCode() = " + System.identityHashCode(s4)); } }
Run results (each run may have different results)
---identityHashCode(): Different objects, this result must be different--- ---because hashCode()May be rewritten (e.g. String Class), object cannot be determined accurately--- new String 1.hashCode() = 69609650 new String 2.hashCode() = 69609650 new String 1.identityHashCode() = 366712642 new String 2.identityHashCode() = 1829164700 String 3.identityHashCode() = 2018699554 String 4.identityHashCode() = 2018699554