The basic types of packaging, System class, Math class

Basic types of packaging:

Byte: byte Byte;

Short int: short Short;

Integer: int Integer

Long integer: long Long

Character: char  Character

Boolean: boolean Boolean

Float: float Float

Float: double Double

{class Demo01 public
  public static void main (String [] args) {
  // string transfer basic data types
  String STR = "12 is";
  int NUM = the Integer.parseInt (STR);
  System.out.println (NUM +. 1) ;
  Double num2 = Double.parseDouble (STR);
  System.out.println (num2);
  // string transfer basic data types: The first method
  System.out.println ( "" + + 12 is. 1);
  // substantially data type string transfer: The second method
  string S1 = String.valueOf (88);
  string S2 = String.valueOf (1.2);
  System.out.println (S1 +. 1);
  System.out.println (S2 +. 1 );
  // string transfer basic data types: A third method
  string Integer.toString S3 = (99);
  System.out.println (S3 +. 1);
  }
}

 

public static void main(String[] args) {
  // 基本数据类型转包装类型:第一种方法
  Integer in=new Integer(123);
  Integer in2=new Integer("456");
  //基本数据类型转包装类型:第二种方法
  Integer in3=Integer.valueOf(789);
  Integer in4=Integer.valueOf("147");
  //包装类型对象转基本数据类型
  int i1=in.intValue();
  int i2=in2.intValue();
  System.out.println(i2+1);
}

自动拆箱:对象自动直接转成基本数值

自动装箱:基本数值自动直接转成对象


public static void main(String[] args) {
  // jdk1.5以后自动拆装箱
  //自动装箱:基本数据类型转包装类型对象
  Integer in=123;//Integer in=new Integer(123);
  //自动拆箱:包装类型对象转基本数据类型
  int i=in+3;//int i=in.inValue()+3;
}

常量池:当数值在byte范围之内时,进行自动装箱,不会新创建对象空间而是使用已有的空间。

public static void main(String[] args) {
  //byte常量池-128-127
  Integer in1=50;
  Integer in2=50;
  System.out.println(in1==in2);//true
  System.out.println(in1.equals(in2));//true
}

System类不能手动创建对象,因为构造方法被private修饰

 

public class Demo01 {
  public static void main(String[] args) {
    int[] arr={1,2,3,4,5,6};
    for(int i=arr.length-1;i>=0;i--){
      if(i==4){
        System.exit(0);
      }
      System.out.println(arr[i]);
    }
  }
}

 

public static void main(String[] args) {
  new Person();
  new Person();
  new Person();
  new Person();
  new Person();
  new Person();
  //调用垃圾回收器
  System.gc();
}

 

public static void main(String[] args) {
  int[] arr={99,98,97,22,11,0};
  int[] arr2=new int[3];
  //将arr前三个数组元素复制到arr2中
  System.arraycopy(arr, 0, arr2, 0, 3);
  //遍历
  for(int i=0;i<arr2.length;i++){
  System.out.println(arr2[i]);
  }
}

 

Math类:

public static void main(String[] args) {
  //求绝对值
  System.out.println(Math.abs(-1.2));
  //向上取整
  System.out.println(Math.ceil(12.3));
  //向下取整
  System.out.println(Math.floor(12.9));
  //求两个值的最大值
  System.out.println(Math.max(10, 9));
  //求两个值的最小值
  System.out.println(Math.min(10, 9));
  //求次幂
  System.out.println(Math.pow(2, 10));
  //求随机数
  System.out.println(Math.random());
  //四舍五入
  System.out.println(Math.round(12.5));
}

 

Guess you like

Origin www.cnblogs.com/boss-H/p/10954673.html