• JAVA Class12


    学习内容:

    1.基本类型包装类:

    byte:Byte  short:Short  int:Integer  long:Long

    float:Float    double:Double  boolean:Boolean  char:Character

    (1)数值转字符串:

    public class Test {
        public static void main(String[] args) {
            //数值转字符串
            int j = 5;
            String s = String.valueOf(j);
            Integer in = j;
            String ss = in.toString();
            String st = Integer.toString(10);//这里的toString()是包装类特有的方法,非继承自Object
            int a = 1;
            String t = a+"";//字符串拼接转换
        }
    }

    (2)字符串转数值:

    public class Test {
        public static void main(String[] args) {
            //字符串转数值
            String str = "999";
            int i= Integer.parseInt(str);
            double d = Double.parseDouble(str);
        }
    }

    (3)基本数值类型转包装类(装箱)

    public class Test {    
        public static void main(String[] args) {   
            //基本类型转包装类(装箱)
            Integer e = new Integer(10);
            Integer es = new Integer("10"); 
            Integer ts = Integer.valueOf(5); 
            //自动装箱
            int nt = 5;
            Integer tn = nt;
            Integer au = 5;
        }
    }

    (4)包装类转基本数值类型(拆箱)

    public class Test {
        public static void main(String[] args) {           
             Integer e = new Integer(10);
             //拆箱
             int em = e.intValue();
             //自动拆箱
             int ea = e;
             //自动拆装箱是JDK1.5版本特性
        }
    }

    (5)数值范围问题

    public class Test {    
        public static void main(String[] args) {
                //在Byte(-128-127)范围内,创建新的引用自动指向同一地址
                Integer i1 = 100;
                Integer i2 = 100;
                System.out.println(i1 == i2); //true
                Integer i3 = 140;
                Integer i4 = 140;
                System.out.println(i1 == i2); //false
        }
    }

    2.System类

    System的类方法都是静态方法,构造方法私有化,所以不能实例化,

    常用方法:

    currentTimeMillis() 返回自1970/1/1 00:00:00到现在经过的毫秒数

    arraycopy(src,srcPos,dest,destPos,length) 复制数组

    //src: 源数组
    //srcPos: 从源数组复制数据的启始位置
    //dest: 目标数组
    //destPos: 复制到目标数组的启始位置
    //length: 复制的长度

    public class Test {
        public static void main(String[] args) {
            int[] a = {1,2,3,4};
            int[] b = new int[4];
            System.arraycopy(a, 0, b, 0,4);
            for(int i=0;i<b.length;i++) {
                System.out.println(b[i]);
            }
        }
    }

    3.Math类

    常用方法

    public class Test {
        public static void main(String[] args) {
            double d = -3.14;
            double a = 3.14;
            double c = 3.6;
            double e = 3.2;
            double max = Math.max(3, 5);
            double min  =Math.min(3, 5);
            double p = Math.pow(2,3);
            double r = Math.random();
            System.out.println(Math.abs(d));//取绝对值
            System.out.println(Math.ceil(a));//向上取整
            System.out.println(Math.floor(c));//向下取整
            System.out.println(Math.round(e));//四舍五入
            System.out.println(max);//取最大值
            System.out.println(min);//取最小值
            System.out.println(p);//次方运算
            System.out.println(r);//随机数
            *Math.sqrt()//计算平方根
            *Math.cbrt()//计算立方根
        }
    }

    4.Arrays方法

    import java.util.Arrays;
    public class Testm {
        public static void main(String[] args) {
            int[] arr = {5,12,2,7,10};
            Arrays.sort(arr);//由小到大排序
            for(int i=0;i<arr.length;i++) {
                System.out.println(arr[i]);
            }
            String sa = Arrays.toString(arr);//转为字符串
            System.out.println(sa);
            int[] arr2 = {1,2,3,4,5};//必须是有序数组
            int index = Arrays.binarySearch(arr2,7);//返回指定数据的索引值,如果没有,则把该值按大小顺序插入数组,返回插入点下标-1
            System.out.println(index);
        }
    
    }

    PS 从大到小排序问题:

    要用到Collections类的reverseOrder方法,如果是LIst类则用Collections类的sort方法

    Collections.sort(num,Collections.reverseOrder());

    import java.util.Scanner;
    import java.util.Collections;
    import java.util.Arrays;
    public class Sort {
        public void sort() {
            Scanner s = new Scanner(System.in);
            System.out.println("请输入第一个数");
            int s1 = s.nextInt();
            System.out.println("请输入第二个数");
            int s2 = s.nextInt();
            System.out.println("请输入第三个数");
            int s3 = s.nextInt();
            System.out.println("请输入第四个数");
            int s4 = s.nextInt();
            Integer[] arr = {s1,s2,s3,s4};
            Arrays.sort(arr,Collections.reverseOrder());
                //使用Collections类的reverseOrder方法
            for(int i=0;i<arr.length;i++) {
                System.out.print(arr[i]+" ");
            }
        }
    }

    5.BigInteger与BigDecimal

    (1)BigInteger,用于操作大于long最大长度的整数

    import java.math.BigInteger;
    public class Testm {
        public static void main(String[] args) {
            BigInteger big1 = new BigInteger("12345678909876543210");
            BigInteger big2 = new BigInteger("98765432101234567890");
            BigInteger bsub = big1.add(big2);//加法
            BigInteger bm = big1.subtract(big2);//减法
            BigInteger bmt = big1.multiply(big2);//乘法
            BigInteger bmd = big2.divide(big1);//除法
        }
    
    }

    (2)BigDecimal,用于解决两个小数运算数值不准确问题

    import java.math.BigDecimal;
    public class Testm {
        public static void main(String[] args) {
                 System.out.println(0.09 + 0.01);
                //输出0.09999999999999999  精度丢失
                 BigDecimal big3 = new BigDecimal("0.09");
                 BigDecimal big4 = new BigDecimal("0.01");
                 //add实现加法运算
                 BigDecimal bigAdd = big3.add(big4);
                 System.out.println(bigAdd);//输出0.1
        }
    
    }    
  • 相关阅读:
    学习Python第五天
    学习Python第三天
    Mongodb
    JMS links
    笔记整理
    a* products
    String
    business knowledge
    [Java coding] leetcode notes
    [Java Basics3] XML, Unit testing
  • 原文地址:https://www.cnblogs.com/whwjava/p/8716669.html
Copyright © 2020-2023  润新知