JAVA study notes (12)

First, the basic type packaging class

  The wrapper classes corresponding to the eight basic types are as follows:

  byte-Byte,short-Short,int-Integer,long-Long,char-Character,boolean-Boolean,

  float-Float,double-Double

  1. Convert basic numeric values ​​to strings

  (1) The basic type can be directly connected with "";

  (2) Call the valueOf method in the String class; String.valueOf();

  (3) Call the toString method in the wrapper class: Integer.toString;

public static void main(String[] args) {
        int a=2;
        String b=a+"";
        String c=String.valueOf(a);
        String d=Integer.toString(a);
        System.out.println(c+1+d);
    }

  2. Basic type and object conversion

  Basic value ----> the way to pack the object (boxing operation)

  (1) Construction method (basic data type)

  (2) Wrapper class .valueOf (basic data type) method

  Packaging object ----> basic value (unboxing operation)

    Wrapper class object.xxValue(); returns a basic data type

Integer i= new Integer(3); // boxing operation 
        Integer ii= new Integer("3"); // boxing operation 
        
        Integer i2 =Integer.valueOf(4); // boxing operation 
        Integer i3=Integer. valueOf("4"); // boxing operation
        
        int a=i.intValue(); // unboxing operation 
        Integer i=4;//automatic boxing
        int a=i+5;//Automatic unboxing
        System.out.println(a)
System.out.println(System.getProperties());
        /*int[] a= {1,5,3,8,6};
        int[] b= {2,8,9,6};
        System.arraycopy(a, 1, b, 1, 2);*/
        //System.arraycopy(src, srcPos, dest, destPos, length);
        /*
         * src: original array name
         * srcPos: the content to be copied is the subscript of the original array
         * dest: destination array
         * destPos: the subscript to be copied to the destination
         * length: the number of elements to be copied
         *
         * * / 
        int src [] = {1,2,3,4,5 };
        int dest [] = {6,7,8,9,10 };
        System.arraycopy(src, 0, dest, 0,3 );
        System.out.print("[");
        for(int i=0;i<dest.length;i++) {
            System.out.print(dest[i]+",");
        }
        System.out.print("]");
    }

3. Math class

public  static  void main(String[] args) {
         // TODO Auto-generated method stub 
            double a=Math.abs(-9.0); // absolute value 
            double b=Math.ceil(3.6); // round up 
            double c=Math.floor(3.3) ; // Round down 
            double d=Math.round(3.3); // Usage of rounding 
            double max=Math.max(a, b); // Maximum value 
            double min= Math.min(a , b); // minimum 
            double e=Math.pow(a, b); // take power

4. Array correlation

public  static  void main(String[] args) {
             int [] arr={66,2,8,4,9,33 };
             // Arrays.sort(arr); // Sort the array in ascending order 
            Arrays.toString(arr ); // The string form used to return the contents of the specified array element 
            System.out.println(Arrays.toString(arr));
            System.out.println();
            binarySearchdemo();
    }
    // find the specified array index 
    public  static  void binarySearchdemo() {
             int [] arr= {5,9,11,56,99,100 };
             int index=Arrays.binarySearch(arr, 11 );
            System.out.println(index);
    }
public static void main(String[] args) {
        // TODO Auto-generated method stub
        int arr[]= {50,6,9,11,33,55,77,88,99,100};
        int a[]=method(arr);
        System.out.println(Arrays.toString(a));
    }
    public  static  int [] method( int [] arr) {
         int [] a= new  int [3]; // Create an array of 3 ints 
        Arrays.sort(arr); // Sort the incoming array 
        System.arraycopy(arr, 0, a, 0, 3); // Give the first three students with the lowest scores to the new array 
        return a; // Return the new array 
    }

5.BigDecimal can realize high-precision calculation of floating-point data

public static void main(String[] args) {
        // TODO Auto-generated method stub
            System.out.println(0.09+0.01);
            BigDecimal b1=new BigDecimal("0.09");
            BigDecimal b2=new BigDecimal("0.01");
            b1.add(b2);
            System.out.println(b1.add(b2));
            // System.out.println(1.0-.32); // lost precision 
            System.out.println();    
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324461488&siteId=291194637