Java Learning Lesson 45th-Other Object APIs (i) System, Runtime, Math class

Source: Internet
Author: User
Tags mathematical functions natural logarithm throw exception time in milliseconds

First, the System class

1.

static long currentTimeMillis()Returns the current time in milliseconds.


In fact: The time difference (measured in milliseconds) between the current and UTC January 1, 1970 midnight.

Long time = 1414069291407l;//long type  followed by lSystem.out.println (time);

2.

static void exit (int status) terminates the currently executing Java virtual machine.

static void GC () execution garbage collector

3. Focus

GetProperties determines the current system properties. staticProperties 
Import Java.util.properties;import Java.util.set;public class Main {//Get line break for current operating system public static final String Line_ SEPARATOR = System.getproperty ("Line.separator");p ublic static void Main (string[] args) {//Get system Property information. The//properties collection stored in the Properties collection is a string-type key-value pair. Package java.util.hashtable<object,object>//It is best to use your own storage and removal methods to complete the element operation/* set<string> stringpropertynames ()          Returns the set of keys in this property list. The key and its corresponding value are strings. Assume that a key with the same name is not found in the main attribute list. It also contains the different keys in the default Properties list.

*/Properties Pro = System.getproperties (); set<string> set = Pro.stringpropertynames (); for (string string:set) {String value = Pro.getproperty (string);//Key value System.out.println (string+ "-:-" +value);}// Set the property information to the system System.setproperty ("MyName", "xxxxxx");//This information is global, no matter what program can call}}


Second, runtime class

When viewing the API documentation, it is very clear to see. Runtime does not have a constructor (stating that the object cannot be created), but there is a non-static method in the method summary, which has a static getruntime () method. This shows that the runtime class is designed using a single-case design pattern.

Import Java.io.ioexception;public class Main {public static void main (string[] args) throws IOException, Interruptedexcep tion {Runtime r = runtime.getruntime ();//Return a process//execute: Run  xxxx.exe, call a local program, pay attention to throw exception//r.exec ("notepad.exe");// This will be found in the current path, not found and then go to the pass path to find//Specify the path//r.exec ("C:\\xxx\\lol.exe"); note transfer character r.exec ("notepad.exe C:\\helloworld.java") ;//with XX program parsing xxx file, note file to match process P = r.exec ("notepad.exe"); Thread.Sleep (1000);//Display one second, kill, pay attention to throw abnormal p.destroy ();//Kill child Process}}

Third, the Math class

 
   
  
Extends Object

Note cannot be inherited

The math class covers many mathematical functions. Be able to challenge what you learn. The Math method is static

The ratio of the circumference of the PI circle to the diameter E is the base of the natural logarithm and these two do not need to be defined directly

Import Java.io.ioexception;import Java.util.random;public class Main {public static void main (string[] args) {Double D1 = M Ath.ceil (11.51);//returns the smallest integer greater than the parameter double D2 = Math.floor (11.51);//returns the largest integer less than the number of parameters double D3 = Math.Round (11.51);// Returns rounded System.out.println (D1); System.out.println (D2); SYSTEM.OUT.PRINTLN (D3); System.out.println (Math.max (4, 5)); SYSTEM.OUT.PRINTLN ((int) Math.pow (2, 3));//Returns the B-order for (int i = 0; i <; i++) {System.out.println (int) (Math.random () *10));//0-9 random number//system.out.println (Math.floor (Math.random () *10));} Random ran = new random ();//random object//Returns the next pseudo-random number, which is a double value that is evenly distributed between 0.0 and 1.0, taken from this random number generator sequence.

for (int i = 0;i<10;i++) {//system.out.println ((int) (ran.nextdouble () *6+1));//Shorthand form for example the following int d = ran.nextint (6) +1;// Nextint is the method in random System.out.println (d);}}


Java Learning Lesson 45th-Other Object APIs (i) System, Runtime, Math class

Related Article

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.