Java Base Class Library--2017.08.04

Source: Internet
Author: User
Tags time in milliseconds

1 parameters for running Java programs

The package Collection;public class Argstest {//public static void main//Public:main program needs to be called by the JVM and needs to be freely invoked by the JVM with this main () method, So use the public modifier to expose this method//static: When invoking the Main method, the JVM cannot first create the object of the main class, and then invoke the main method through the object, the JVM directly through the class to call the Main method//void: return value, because the main method is called by the JVM, The method returns a value to the JVM, where there is no meaning to public static void main (string[] args) {System.out.println (args.length), and for (String S:args) { System.out.println (s);
0
}}}

2 Scanner Keyboard input

Package Collection;import Java.util.scanner;public class Scannerkeyboard {public static void main (string[] args) {     / /keyboard input Scanner, with a system input, through the constructor Scanner (InputStream source) Scanner scanner=new Scanner (system.in);     Nextxxx (): Gets the next input//      int i=scanner.nextint ();//      System.out.println (i);      Gets the next entry hasnextxxx (), if it is a string, directly Hasnext (0 while      (Scanner.hasnextint ())      {      System.out.println (" Next Number: "+scanner.nextint ());}}}      

3 System Class

What is the System class? The running platform for the current Java program.

What's the use? Provides class variables for standard input, standard output, and error output, and provides methods for static methods for accessing environment variables, System properties, and for loading files and dynamic-link libraries.

Package Collection;import Java.util.map;public class Systemtest {public static void main (string[] args) {// Gets all the environment variables of the system map<string, string> env=system.getenv ();//system.getenv () returns a string map view of the current system environment that cannot be modified. For (String Name:env.keySet ()) {System.out.println (name+ "--->" +env.get (Name));} System.out.println ("///////");//Gets the value of the specified environment variable System.out.println (system.getenv ("M2_hone")); System.out.println (system.getenv ("Os.name")),//currenttimemillis () Current time  (returns the current time in milliseconds) System.out.println ( System.currenttimemillis ());//nanotime () System.out.println (System.nanotime ());}}

4 Runtime class

What is it? The runtime class represents the runtime environment of a Java program that is connected to the runtime environment by the application.

What's the use? Access information about the JVM, such as the number of processors, memory information.

Package Collection;public class Exectest {public static void main (string[] args) {  //Get Java program associated run-time object//getruntime () : Returns the run-time object associated with the current Java application. Runtime rt=runtime.getruntime ();//output Processor number System.out.println ("Number of processors:" +rt.availableprocessors ());// Number of free memory System.out.println ("Free Memory:" +rt.freememory ()/1024),//maximum memory available System.out.println ("Maximum available Memory" +rt.maxmemory ());    Total Memory System.out.println ("Total Memory" +rt.totalmemory ());}}

5 The object class is the parent of all classes, arrays, and enumeration classes, and Java allows you to assign any type of variable to a variable of type object.

Objects class: Tool class for manipulating the object class

There are such things as clone (), Equals (), Finalize (), GetClass (), Hashcode (), notify (), Notifyall (), toString (), Wait ()

Package Collection;public class Objecttest {public  static void Main (string[] args) {Object Ob1=new object (); String S1=new string ("Tianjianke"); System.out.println (S1.tostring ()); System.out.println (Ob1.tostring ()); [Email protected]//class name [email protected]+ hash Code System.out.println (Ob1.hashcode ()); System.out.println (S1.hashcode ());}  }

6 Similarities and differences of String, StringBuffer and StringBuilder classes

A string is a sequence of strings, and Java provides string and StringBuffer two classes to encapsulate a string.

String is an immutable class: The final decoration is used in the JDK source code, and once a string object is created, the sequence of characters containing the object is immutable.

The StringBuffer object is a variable string of characters, which can be changed by methods such as append (), insert (), reverse (), Setcharat (), SetLength (), and so on, to change the character sequence of the string object.

The StringBuilder object is a variable string of characters, and the difference from StringBuffer is that the thread is not considered, so performance is better.

Package Collection;public class Stringbuildertest {public static void main (string[] args) {string S1=new string (" Tianjianke "); StringBuffer s2=new stringbuffer ("Tianjianke1"); StringBuilder s3=new StringBuilder ("Tianjianke2"); StringBuffer s4=s2.append (' a '); System.out.println (S4.tostring ()); System.out.println (S4.reverse (). toString ()); StringBuilder s5=s3.append ("Dafas"); System.out.println (S5); System.out.println (S5.tostring ());}}

7 Math class

What is it? Java provides the basic +,-,*,? ,%, and so on, but for more complex operations, you need to use the Java Math class.

Package Collection;public class Mathtest {public static void main (string[] args) {//floor floor that is the small System.out.println ( Math.floor (5.32));//ceil ceiling that is the big System.out.println (Math.ceil (5.32));//max (int a, int b) and min (int a,int b) System.out.println (Math.max (55, 89));}}

8Random () class with Threadlocalrandom ()

What is it? Random () is specifically used to generate a pseudo-random number, threadlocalrandom () is an enhanced version of the Random (), and in the context of concurrent access, using Theadlocalrandom () instead of random () can reduce thread resource contention.

Package Collection;import Java.util.random;public class Randomtest {public static void main (string[] args) {random random =new Random (); System.out.println (Random.nextint ()); System.out.println (Random.nextfloat ()); System.out.println (Random.nextboolean ());}}

9 BigDecimal Class

What is it? What's the use? BigDecimal a kind of data class, when the calculation of some data float, double will produce precision loss, in order to be able to accurately represent and calculate the floating-point number, Java provides the BigDecimal class.

Package Collection;import Java.math.bigdecimal;public class Doubletest {public static void main (string[] args) { System.out.println ("0.5+0.1=" + (0.5+0.01)); System.out.println ("0.05+0.01=" + (0.05+0.01));/* * 0.5+0.1=0.51   0.5+0.1=0.060000000000000005 */bigdecimal b3= New BigDecimal ("0.5145"); BigDecimal b1=new BigDecimal (0.05);    BigDecimal b2=new BigDecimal (0.04);    System.out.println ("0.05+0.04=" + (B1.add (B2)));        System.out.println ("0.5145+0.04=" + (B3.add (B2)));}}

  

Java Base Class Library--2017.08.04

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.