I. Scanner GET keyboard input
1. The main methods are: (1) hasnextxxx (); Do you have the next entry?
(2) nextxxx (); Get the next entry
(3) can also file input, omitted here;
Example:
ImportJava.util.Scanner;/** * * @authorFengkuirui * @date 2017-02-08 * scanner get keyboard input;*/ Public classScannerkeyboardtest { Public Static voidMain (string[] args) {//system.in represents standard input, i.e. keyboard input;Scanner cin =NewScanner (system.in); //default space \ t \ n = delimiterCin.usedelimiter ("\ n");//set \ n to input delimiter while(Cin.hasnext ()) {//determine if there is a next item//Output Input ItemsSystem.out.println (Cin.next ()); } } }
2. List of all methods
Ii. related usage of system
The System class represents the current Java program's and running platform, the program cannot create the system object, and the system class provides class methods and class variables that allow direct use.
1. Get the environment variables for the system
ImportJava.util.Map;/** * * @authorFengkuirui * @date 2017-02-08 * Get System Variables*/ Public classSystemtest { Public Static voidMain (string[] args) {//get the environment variables of the system;Map<string,string> env =system.getenv (); for(String Name:env.keySet ()) {//iterating through the collectionSystem.out.println (name+ "---->" +env.get (name)); } }}
The test results are as follows:
2. Distinguish the same Hashode code and different objects;
/** * * @authorFengkuirui * @date 2017-02-07 * distinguished from hashcode,*/ Public classIdentityhashcode { Public Static voidMain (string[] args) {String S1=NewString ("Java"); String S2=NewString ("Java"); System.out.println (S1.hashcode ()); System.out.println (S2.hashcode ()); System.out.println (System.identityhashcode (S1)); System.out.println (System.identityhashcode (S2)); }}
Third, runtime class
The runtime class represents the running environment of the Java program, each program has a runtime instance of the object, and the application cannot create the runtime instance itself, but only relies on the GetRuntime () method to obtain it;
1.Runtime getting information about the JVM
/** * * @authorFengkuirui * @date 2017-02-07 * Runtime Test*/ Public classRuntimetest { Public Static voidMain (string[] args) {Runtime rt=Runtime.getruntime (); System.out.println ("Number of processors" +rt.availableprocessors ()); System.out.println ("Free Memory" +rt.freememory ()); System.out.println ("Total Memory" +rt.totalmemory ()); System.out.println ("Maximum available memory" +rt.maxmemory ()); }}
2.Runtime command to start a process individually to run the operating system
/** * @author Fengkuirui * @date 2017-02-07 * Separate start a process to run the operating system after the explicit * */ Public class exectest { publicstaticvoidthrows Exception { = runtime.getruntime (); Rt.exec ("notepad.exe"); // command line start Notepad } }
Iv. Common Categories
1.Object class
The object class is the father of all classes, that is, Java allows you to assign objects of any type to the common methods provided by the object class: Boolean Equals (Object obj) determines whether the values of two objects are equal Class<?> Getclas () returns the run-time class int hashcode () returns the Hashcode value of the object, String toString () The string returned to the object represents the very efficient self-copy mechanism, which is more than twice times the static copy speed of the system default copy-only clones all of that object Member variable that does not clone a deep clone of an object referenced by a member variable value of a reference type--all copies are simple to apply:
/** * * @authorFengkuirui * @date 2017-02-08 * To achieve self-cloning*/classaddress{String Detail; PublicAddress (String detail) { This. Detail =detail; }}//implementing the Clone interfaceclassUserImplementscloneable{intAge ; Address address; PublicUser (intAge ) { This. Age =Age ; Address=NewAddress ("China"); } //Super.clone () to implement Clone () PublicUser Clone ()throwsclonenotsupportedexception{return(User)Super. Clone (); }} Public classClonetest { Public Static voidMain (string[] args)throwsclonenotsupportedexception {User u1=NewUser (29); //get U2 through cloneUser U2 =U1.clone (); //determine if U1 and U2 are the sameSystem.out.println (u1 = =U2); //determine if the address of U1,U2 is the sameSystem.out.println (u1.address = =u2.address); }}
The results are as follows:
2.Objects (New in Java 7)Null is allowed, an empty object with an hashcode value of 0,tostring is null instead of throwing an exception, reducing the exception-throwing example of a null pointer:
Importjava.util.Objects;/** * * @authorFengkuirui * @date 2017-02-08 * Objects Simple test*/ Public classObjectstest {//defines an obj with a default value of NULL; StaticObjectstest obj; Public Static voidMain (string[] args) {//hashcode of output objSystem.out.println (Objects.hashcode (obj)); //outputs the ToString () of obj;System.out.println (objects.tostring (obj)); }}
The results are as follows:
3.string,stringbuffer,stringbuilder class
The string class is an immutable sequence of characters, and each time it is changed, it does not change itself, but instead re-creates the heap, which wastes a lot of memory and causes significant overhead;
The StringBuffer class and the StringBuilder class are inherently similar, the knowledge stringbuffer is thread-safe, and the StringBuilder class does not implement thread-safe, but it is not secure, but its performance is high.
The specific method will not be described.
4.Math class
Math provides a basic mathematical method of operation.
5.ThreadLocalRandom and Random classes
The random class is specifically used to produce a pseudo-random number. Threadlocalrandom is an enhanced version of random, in the context of concurrent access, the use of the former instead of the latter can reduce the competition of resources, ensure that the system has good thread safety selection.
6.BigDecimal class
The two basic floating-point types mentioned in the preceding float,double have pointed out that it is easy to lose precision when computing:
Examples:
/** * @author */Publicclass doubletest {publicstatic void Main (string[] args) { System.out.println ("0.05 + 0.01 =" + (0.05+0.01)); System.out.println ("1.0-0.42 =" + (1.0-0.42)); System.out.println ("4.015 * =" + (4.015*100)); System.out.println ("123.3/100 =" + (123.3/100));} }
For the above problem, Java provides the BigDecimal class, which provides a large number of constructors to create BigDecimal objects, it can solve the problem of loss of precision;
Examples:
/** * @authorFengkuirui * @date 2017-02-08 * Resolve loss of precision **/ImportJava.math.BigDecimal; Public classBigdecimaltest { Public Static voidMain (string[] args) {BigDecimal F1=NewBigDecimal ("0.05"); BigDecimal F2= Bigdecimal.valueof (0.01); BigDecimal f3=NewBigDecimal (0.05); System.out.println ("Use string as constructor parameter"); System.out.println ("0.05 + 0.01 =" +F1.add (F2)); System.out.println ("0.05-0.01 =" +f1.subtract (F2)); System.out.println ("Use double as argument to constructor"); System.out.println ("0.05 + 0.01 =" +F3.add (F2)); System.out.println ("0.05-0.01 =" +f3.subtract (F2)); }}
The results are as follows:
7. Date Class
(1) The date class, most of its constructors and methods are nearly obsolete, so the detailed study;
(2) Calendar class
The Calendaer class, based on date, has a better processing time, which is an abstract class that cannot be created with a constructor, but has several static getinstance () methods to get the Calendar object.
Examples are as follows:
ImportJava.util.Calendar;Importjava.util.Date;/** * * @authorFengkuirui * @date 2017-02-08 **/ Public classCalendartest { Public Static voidMain (string[] args) {//Create a calendar default objectCalendar cal =calendar.getinstance (); //Gets the date object fromDate Date =Cal.gettime (); //get a Calendar object by date//you must first obtain an instance of the calendarCalendar Cal2 =calendar.getinstance (); Cal2.settime (date); }}
Common Methods for calendar:
void Add (int field, int amount);
int get (int field);
void Roll (int field, int amount);
(3) DateTimeFormatter class complete formatting
Java Common Classes