JAVA based basic types of packaging, System class, Math class, Arrays class operation and large data

Personal understanding:

  In order to facilitate the operation and call some methods, we need basic types of value into an object; but when the need for special attention to converting good in the end is what their type, need to call the name of the class method which! Special attention is related problem Byte constant pool (==); gc () garbage collection mechanism, then, there was still judged by the automatic system which is new, which is old, and then different qualitative different mechanisms based on. Not the best garbage collector, no more universal collector, can only choose the most appropriate for a particular application collector.

First, the basic types of packaging:

1 Overview:

  The basic properties of the object class packaging: java basic data type value encapsulated into objects, thus providing the basic function more operating values.

  note:

  int corresponds Integer, char corresponds Character.

2, turn into a string basic types:

String str="12";
int num=Integer.parseInt(str);
double num2=Double.parseDouble(str);

  parseXXX (String s); where XXX represents the basic type, parameters can be converted into a string of basic types, if the string can not be converted into basic types, problems will occur-digital conversion of a NumberFormatException ( which must not contain unnecessary things, such as space)

3, the basic value translated into strings:

①, basic numerical value of the "" want to connect to;

②, call the String valueOf method (a static direct call to the class name);

③, toString wrapper class method calls;

System.out.println(""+12+1);
String s1=String.valueOf(88);
String s2=String.valueOf(1.2);
System.out.println(s2+1);
String s3=Integer.toString(99);
System.out.println(s3+1);

4, and the object into basic types:

①, the basic value transfer wrapper object:

  Integer (int value or String s) valueOf (int value or String s)

I = Integer new new Integer (. 4); // Use the constructor 
Integer II = new new Integer ( ". 4"); // constructor can pass a numeric string

  III = Integer Integer. ValueOf (. 4); // use the method of packaging a class valueOf

  . IIIi = Integer Integer valueOf ( ". 4"); // use the method of packaging a class valueOf

②, packaged object into a basic value (the packaging object calls):

intValue () Returns the value Ingeter int type;

int num = i.intValue();

5, the automatic boxing and unboxing (the JDK1.5 later):

Automatic unboxing: objects are automatically transferred substantially directly value;

Automatic packing: base numerical value is directly converted to automatic packaging objects;

. 4 I = Integer; // automatic packing. I = equivalent new new Integer Integer (. 4); 
i = i +. 5; // the right hand side: The object into a basic value i (automatic unpacking) i.intValue () + 5; the addition operation is completed, reloaded box, the basic value turn into objects.

(Integer than int useful, because it can be stored null)

Autoboxing demo (byte constant pool) details:

  When the value in the byte range, automatic packing, the newly created object space but does not use the existing space.

A = Integer new new Integer (. 3 ); 
Integer B = new new Integer (. 3 ); 
System.out.println (A == B); // to false 
System.out.println (a.equals (B)); // to true 

System.out.println ( "---------------------" ); 
Integer X = 127 ; 
Integer Y = 127 ;
 // autoboxing in jdk1.5 , if the value in the byte range, the newly created object space but does not use the original existing space. 
System.out.println (X == Y); // to true 
System.out.println (of x.equals (Y)); // to true

Two, System type:

1, concepts:

  System represents where the program system, the system provides a number corresponding to the attribute information, and system operation.

  System class can not manually create object because the constructor is modified private, to prevent the outside world to create an object. System class are static methods, you can access the class name.

2, common methods:

①, currentTimeMillis () Gets milliseconds difference between the current system time at 00:00 on January 1, 1970 points

②, Exit (int Status) to the end of the Java program is running. Parameters can be passed in a number. 0 generally referred to as the incoming normal state, other (non-zero) in an abnormal state

③, GC () used to run the JVM garbage collector, memory garbage removal is completed.

④, getProperty (String Key) used to obtain the specified key system attribute information (string name) recorded in the

⑤, The arraycopy used in implementing the copy source to the destination array element array portion specified position

The arraycopy (first array, taken the position of the second array, the location needs to be placed, the need to intercept length)

For example: Verify digital printing cycle time for the need to use 1-9999 (ms)

public static void main(String[] args) {
     long start = System.currentTimeMillis();
    for (int i=1; i<10000; i++) {
         System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("共耗时毫秒:" + (end-start) );
}

The src array before three elements, copied to the front dest array of three positions

public  static  void main (String [] args) {
 int [] the src = new new  int [] {1,2,3,4,5 };
 int [] dest = new new  int [] {6,7, 8,9, 10 }; 
System.arraycopy (src, 0, dest, 0,. 3 ); 
after executing the code: two elements in the array is changed 
the src array element [ 1,2,3,4,5 ] 
dest array element [ 1 , 2,3,9,10 ] 
}

 100-999 three digits generated between the loop and the number of prints, when the number can be divided by 10, the end of the run the program

public static void main(String[] args){
     Random random = new Random();
    while(true){
    int number = random.nextInt(900)+100; //0-899 + 100
    if (nmumber % 10 == 0) {
        System.exit(0);
}
}
}

Three, Math class:

1, concepts:

  Math class mathematical tools comprising a basic method for performing the mathematical operations, like ever exponential, logarithmic, trigonometric functions and square root. All of its methods are static methods, and generally does not create the object.

2, common methods:

abs method, the results are positive

Double D1 = the Math.abs (-5); // D1 is 5 
Double D2 = the Math.abs (5); // D2 is 5

ceil method, the result is double the value of the parameter value is larger than the smallest integer rounding up

Double D1 = Math.ceil (3.3); // D1 value 4.0 
Double D2 = Math.ceil (-3.3); // D2 value of -3.0 
Double D3 = Math.ceil (5.1); // D3 of value of 6.0

floor method, the result is double the value of the parameter is smaller than the maximum integer rounding down

Double D1 = Math.floor (3.3); // D1 value 3.0 
Double D2 = Math.floor (-3.3); // D2 value of -4.0 
Double D3 = Math.floor (5.1); // D3 of A value of 5.0

pow method returns the first parameter the second parameter value power

Double D1 Math.pow = (2.0, 3.0); // D1 value 8.0 
Double D2 Math.pow = (3.0, 3.0); // D2 value 27.0

round method returns a result parameter value is rounded

Double D1 = Math.round (5.5); // D1 value 6.0 
Double D2 = Math.round (5.4); // D2 was 5.0

 The method of random, to produce a greater than or equal to 0.0 and less than double the random number fractional 1.0

double d1 = Math.random();

Four, Arrays categories:

1, concepts:

  This class contains various methods for manipulating arrays (such as sorting and searching). Note that if the specified array reference is null, the access method in this class will throw null pointer exception NullPointerException .

2, common methods:

sort method, is used to specify the elements of the array in ascending order (element values ​​ordered from small to large)

// source array elements arr {1,5,9,3,7}, the array elements are sorted arr 1,3,5,7,9} { 
int [] arr = {1,5,9,3, . 7 }; 
Arrays.sort (ARR);

 toString method, used to return the specified string array element content

int [] = {1,5,9,3,7 ARR }; 
String STR = of Arrays.toString (ARR); // STR is [1, 3, 5, 7, 9]

 binarySearch method, specified in the array, to find the position of a given element value occurs. If no inquiries to the return position is - (this value should be in the position) -1. (- subscript -1) requires the array must be ordered array.

int [] ARR = {1,3,4,5,6 };
 int index = Arrays.binarySearch (ARR,. 4); // index value 2 
int index2 = Arrasy.binarySearch (ARR, 2); // index2 is -2

E.g:

Define a method to receive an array, the array storage 10 student test scores, the method asked to return after three test scores lowest test scores

public  static  int [] Method ( Double [] ARR) { 
    Arrays.sort (ARR); // be sorted array element (element values ordered from small to large) 
    int [] = Result new new  int ; [. 3] // after storage three test scores 
    System.arraycopy (arr, 0, result, 0,. 3); // copy before arr array of 3 elements into the result array 
    return result; 
}

Five large data operations:

1、BigInteger:

  Type in java long as the maximum integer type, how long for more than type of data to represent it. In the Java world, more than long integer type can not be called an integer, and they are packaged as BigInteger objects. In BigInteger class , four operations are implemented to achieve a method, the operator is not employed.

Part of the method:

public  static  void main (String [] args) {
         // large data objects encapsulate BigInteger 
          BigInteger BIG1 = new new BigInteger ( "12345678909876543210" ); 
          BigInteger BIG2 = new new BigInteger ( "98765432101234567890" );
           // the Add achieved adder 
          BigInteger bigAdd = big1.add (BIG2);
           // Subtract subtraction achieve 
          a BigInteger bigSub = big1.subtract (BIG2);
           // multiply achieve multiplication 
          a BigInteger bigMul = big1.multiply (BIG2);
           // divide perform division
          BigInteger bigDiv = big2.divide(big1);
}

2, BigDecimal categories:

  double and float type of operation is very easy to lose precision (the computer is binary, when calculating lose precision), caused no data accuracy, Java provides high-precision arithmetic we BigDecimal class can implement floating point data.

  Recommend floating-point data is given as a string as argument the result is predictable .

  

public  static  void main (String [] args) {
           // large data objects encapsulate BigDecimal 
          BigDecimal BIG1 = new new BigDecimal ( "0.09" ); 
          BigDecimal BIG2 = new new BigDecimal ( "0.01" );
           // the Add achieved adder 
          BigDecimal bigAdd = big1.add (BIG2); 
        
          the BigDecimal BIG3 = new new the BigDecimal ( "1.0" ); 
          the BigDecimal BIG4 = new new the BigDecimal ( "0.32" );
           // Subtract subtraction achieve 
          the BigDecimal bigSub = big3.subtract (BIG4);
          
          Big5 the BigDecimal = new new the BigDecimal ( "1.105" ); 
          the BigDecimal Big6 = new new the BigDecimal ( "100" );
           // Multiply achieve multiplication 
          BigDecimal bigMul = big5.multiply (big6);

  For the division of floating-point data, and different integers, it may appear infinitely non-repeating decimals, requiring the number of bits required to be retained and selected rounding mode.

divide (BigDecimal divisor, retains several decimal places, selected up or down)

= BD7 the BigDecimal new new the BigDecimal ( "1.301" ); 
the BigDecimal BD8
= new new the BigDecimal ( "100" ); System.out.println (bd7.divide (BD8, 2, BigDecimal.ROUND_FLOOR)); // In addition, taking two effective digital and rounding down

 

Guess you like

Origin www.cnblogs.com/21-forever/p/10932184.html