Basic data types packaging / System classes / Math class / Arrays class

8 kinds of packaging substantially corresponding to the following types:

In addition to other integer and character of initial capital letters!

To convert a string data types are static, so be called directly by the class name:

The basic value translated into strings of three ways:

Direct basic types and "" can be connected; 34+ ""

Call valueOf method of String; String.valueOf (34)  ;

ToString package class method calls; Integer.toString (34 is)  ;

com.oracle.demo01 Package; 

public class Demo01 { 
	public static void main (String [] args) { 
	          // string rotation ----> basic data type 
		   String STR = "12 is"; 
		   // static method directly class name calling 
		   int NUM = the Integer.parseInt (STR); 
		   System.out.println (NUM +. 1); 
		   Double num2 = Double.parseDouble (STR); 
		   System.out.println (num2); 
		   // switch to basic data types - -> string 
		   System.out.println ( "" + + 12 is. 1); 
		   string S1 = String.valueOf (88); 
		   System.out.println (S1 +. 3); 
		   string S2 = String.valueOf (1.2); 
		   the System .out.println (S2 +. 1); 
		   String Integer.toString S3 = (99); 
		   System.out.println (S3 +. 1); 
	} 

}

 Basic types and object conversion:

com.oracle.demo01 Package; 

public class Demo02 { 
    public static void main (String [] args) { 
	/ * --- elementary data type> package type * / 
    	. //. 1 
    	Integer in = new new Integer (123); 
    	Integer IN2 new new Integer = ( "456"); 
    	// 2. 
    	Integer Integer.valueOf = IN3 (789); 
    	Integer IN4 = Integer.valueOf ( "147"); 
    	// --- package type objects> basic data type 
    	int i1 = in.intValue (); 
    	int in2.intValue I2 = (); 
	} 
}

 Automatic unboxing: objects are automatically transferred substantially directly value

    Automatic packing: base numerical value is automatically transferred directly to an object

When auto-boxing?

For example: Integer i = 100;

Equivalent compiler automatically make you compile the following syntax: Integer i = Integer.valueOf (100);

When auto-unboxing?

1 Integer i = 10; // packing 
2 int t = i; // unpacking, actually performs int t = i.intValue ();

System can not manually create an object class, because the constructor has been modified by private, System class are static methods can be called directly by the class name.

Common methods:

arraycopy method for realizing the copy source array portion of the target element to a specified position array

com.oracle.demo02 Package; 

public class Demo10 { 
	  public static void main (String [] args) { 
		  / * src the first three elements in the array, three positions are copied to the dest array 
			before copying element: src array elements [1,2,3,4,5], dest array element [6,7,8,9,10] 
			after copying element: src array element [1,2,3,4,5], dest array element [1 , 2,3,9,10] 
			* / 
		  int [] = {1,2,3,4,5} the src; 
		  int [] = {6,7,8,9,10} dest; 
		  System.arraycopy (the src , 0, dest, 0,. 3); 
		  // iterate dest 
			for (int I = 0; I <dest.length; I ++) { 
				System.out.println (dest [I]); 
			} 
		  
	} 
}

  

com.oracle.demo02 Package; 

public class Demo11 { 
	public static void main (String [] args) { 
		/ * 100-999 cycles generated between the three digits and the number of prints, when the number is divisible by 10 , the end of the run the program * / 
		for (int I = 101; I <= 999; I ++) { 
			IF (I% 10 == 0) { 
				System.exit (0); 
			} 
			System.out.println (I); 
		} 
	} 
}

  Math class which may be rounded up, rounding down, the absolute value, the sum of two maximum value, the minimum sum of two exponentiation, find a random number, examples:

com.oracle.demo02 Package; 

public class Demo05 { 
     public static void main (String [] args) { 
		// absolute value (whichever is positive) 
    	System.out.println (the Math.abs (-1.2)); 
    	// rounded up 
    	System.out.println (Math.ceil (12.1)); 
    	// rounded down 
    	System.out.println (Math.floor (12.9)); 
    	// find the maximum of the two values 
    	System.out. the println (Math.max (10,. 9)); 
    	// find the minimum value of two 
    	System.out.println (Math.min (10,9)); 
    	// find power 
    	System.out.println (Math. POW (2,10)); 
    	// find a random number 
    	System.out.println (Math.random ()); 
    	// rounded 
    	System.out.println (Math.round (12.5)); 
	} 
}

 Arrays class Array represents the array of tools, if the specified array reference is null. The access method in this class all throw a null pointer exception NullPointerException .

    Common method

com.oracle.demo02 Package; 

Import java.util.Arrays; 

public class Demo06 { 
    public static void main (String [] args) { 
		int [] = {5,99,1,66,11,2 ARR}; 
		// sorting 
		Arrays.sort (ARR); 
		// iterate 
/ * for (int I = 0; I <arr.length; I ++) { 
			System.out.println (ARR [I] + ""); 
		} * / 
		the System.out .println (of Arrays.toString (ARR)); 
		System.out.println (); 
		int NUM = Arrays.binarySearch (ARR, 2); // change element if not present, the element should return an array corresponding to the position the (- subscript -1) 
		// returns the index value 
		System.out.println (NUM); 
	} 
}
		

  

Guess you like

Origin www.cnblogs.com/awdsjk/p/10951299.html