JAVA 45th-Other Object API (1)

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

JAVA 45th-Other Object API (1)

I. System class

1.

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

In fact, the time difference between the current time and the Coordinated Universal Time at midnight, January 1, January 1, 1970 (measured in milliseconds ).

Long time = 1414069291407l; // lSystem. out. println (time) is added to the long type );

2.

Static void exit (int status) terminates the currently running Java virtual machine.

Static void gc () run the Garbage Collector

3. Key Points

staticProperties GetProperties determines the current system property.
Import java. util. properties; import java. util. set; public class Main {// get the linefeed of the current operating System public static final String LINE_SEPARATOR = System. getProperty ("line. separator "); public static void main (String [] args) {// obtain system attribute information, and stored in the Properties set. The // properties set stores String-type key-value pairs. util. hashtable
 
  
// It is best to use your own storage and retrieval methods to complete element operations/* Set
  
   
StringPropertyNames () returns the key set in this attribute list. The key and its corresponding value are strings. If a key with the same name is not found in the master attribute list, it also includes different keys in the default attribute list. */Properties pro = System. getProperties (); Set
   
    
Set = pro. stringPropertyNames (); for (String string: set) {String value = pro. getProperty (string); // obtain the key value System. out. println (string + "-:-" + value);} // set the attribute information for the System. setProperty ("myname", "xxxxxx"); // The information is global and can be called by any program }}
   
  
 

Ii. Runtime class

When viewing the API documentation, we can clearly see that Runtime does not have a constructor (indicating that an object cannot be created), but there are non-static methods in the method abstract, there is a static getRuntime () method, which indicates that the Runtime class is designed using the singleton design mode.

Import java. io. IOException; public class Main {public static void main (String [] args) throws IOException, InterruptedException {Runtime r = Runtime. getRuntime (); // return a process // execute: execute xxxx.exe and call a local program. Note that an exception is thrown. // r.exe c ("notepad.exe "); // In this way, the write will be first found in the current path, and then the path // specified // r.exe c ("c: \ xxx \ LoL.exe "zookeeper was transferred to cmdr.exe c (" notepad.exe C :\\ HelloWorld. java "); // use the XX program to parse the XXX file. Note that the file must match Process p = r.exe c (" notepad.exe "); Thread. sleep (1000); // display for one second, kill, note that an exception is thrown p. destroy (); // kill sub-process }}

Iii. Math class

public final class Math 
 
 
extends Object

Note that it cannot be inherited

The Math class covers many mathematical functions and can fully challenge what you have learned. The Math method is static.

The ratio of the circumference to the diameter of the PI circle E is the base number of the natural logarithm.

Import java. io. IOException; import java. util. random; public class Main {public static void main (String [] args) {double d1 = Math. ceil (11.51); // returns the smallest integer greater than the value of double d2 = Math. floor (11.51); // returns the maximum integer less than the value of double d3 = Math. round (11.51); // returns rounded to 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 power B of a for (int I = 0; I <10; I ++) {System. out. println (int) (Math. random () * 10); // 0-9 random number // System. out. println (Math. floor (Math. random () * 10);} Random ran = new Random (); // the Random object // returns the next pseudo-random number, it is a double value that is evenly distributed between 0.0 and 1.0 from the sequence of the random number generator. For (int I = 0; I <10; I ++) {// System. out. println (int) (ran. nextDouble () * 6 + 1); // the abbreviated form is as follows: int d = ran. nextInt (6) + 1; // nextInt is the method System in Random. out. println (d );}}}


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.