从零开始的JavaAPI-常用类

JavaAPI-常用类汇总

1.API

API(Application Programming Interface)应用程序编程接口

  • 语言中提供的类、接口;
  • 对类、接口功能的说明文档。

2.基本数据类型包装类

Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类

即Java为每种基本类型提供了一个类,这些类被称为包装类。

/*
    基本数据类型        对应的包装类
    byte               Byte
    short              Short 
    int                Integer
    long               Long
    float              Float
    double             Double
    char               Character
    boolean            Boolean

  */

这些包装类分别封装了一个相应的基本数据类型,并为其提供了一系列操作方法。

以Integer包装类为例:

 public static void main(String[] args) {

        int num = 10;//一个int型整数
    
        //构造方法
        Integer num1 = new Integer(10);//把int型整数作为参数包装到Integer类的对象中
        System.out.println(num1.doubleValue());//输出转为double型

        Integer num2 = new Integer("15");//将字符串类型的值转换为int型
        System.out.println(num2);

        /*
        Integer类中提供很多的方法来对int类型进行操作
         */
        //转换进制方法
               System.out.println(Integer.toBinaryString(3));//十进制的整数转换为2进制
        System.out.println(Integer.toHexString(17));//十进制的整数转换为16进制
        System.out.println(Integer.toOctalString(12));//十进制的整数转换为8进制

        System.out.println(Integer.MAX_VALUE);//int型常量保持的最大值
        System.out.println(Integer.MIN_VALUE);//int型常量保持的最小值
        System.out.println(Integer.BYTES);//用于表示二进制补码二进制形式的 int值的字节数。
        System.out.println(Integer.SIZE);//用于表示二进制补码二进制形式的 int值的位数。

        //比较方法
        /*
        Integer num3 = Integer.valueOf(23);//
        Integer num4 = Integer.valueOf(22);
         */
        Integer num3 = new Integer(23);//创建一个Integer对象
        Integer num4 = new Integer(22);
        System.out.println(num3==num4);//== 用于引用类型比较时,比较的是对象内存地址是否相等
        System.out.println(num3.equals(num4));//equals()方法比较的是两个对象中具体包含的值是否相等 相等为true, 不相等为false
        System.out.println(num3.compareTo(num4));//值为-1 0 1,前者大输出1,相等输出0,前者小输出-1
        System.out.println(Integer.max(num3,num4));//取较大值
        System.out.println(Integer.min(num3,num4));//取较小值

        Integer n1 = new Integer(10);
        int n2 = n1.intValue();//取出对象中包含的具体的值
        long n3 = n1.longValue();
        System.out.println(n3);

        int n4 = Integer.parseInt("11");//parseInt(String s)将字符串参数解析为带符号的十进制整数。
        System.out.println(n4);

        //基本类型 转换为 引用类型
        Integer num7 = Integer.valueOf(10);
        Integer num8 = Integer.valueOf("10");
        System.out.println(num7==num8);//true

        //把数值转为字符串
        System.out.println(num8.toString());
        System.out.println(Integer.toString(10));


        //引用类型 转换为 基本类型
        int num5 = Integer.valueOf(num3);
        int num6 = num4.intValue();


    }

装箱和拆箱:

装箱

  • 自动将基本类型装换为包装类类型
  • 装箱的时候自动调用的是Integer的valueOf()方法

拆箱

  • 自动将包装类类型装换为基本数据类型
  • 拆箱的时候自动调用的是Integer的intValue()方法
public static void main(String[] args) {
        //构造方法
        Integer num1 = new Integer(10);//创建一个Integer对象,把int型整数作为参数包装到Integer类的对象中
        Integer num2 = Integer.valueOf(10);//基本类型 转换为 引用类型

        int num3 = num1.intValue();//取出对象中包含的具体的值
        int num4 = Integer.parseInt("10");//parseInt(String s)将字符串参数解析为带符号的十进制整数。

        /*
        自动装箱:把基本类型转为引用类型
         */
        int n = 10;
        Integer n1 = n;//自动调用valueOf()方法,valueOf():基本类型 转换为 引用类型
        /*
        自动拆箱:把引用类型转为基本类型
         */
        int n2 = n1;//自动调用intValue()方法,intValue():取出对象中包含的具体的值
        /*
        使用装箱(valueOf()) 在创建对象时,值如果在 -128~+127 之间,如果多个值相同,指向的是同一个地址;
                                                             如果值不在范围区内,则创建新对象。
        使用 new + 构造方法() 不管制是否在此区间,都会创建新对象
         */
    }

3.Object类

Object类是java中所有类的父类。

常用方法:

  • 将对象输出时,会调用toString()方法
  • 做比较时,会调用equals()方法,等同于 ==
  public static void main(String[] args) {

        /*
        Object类是java中所有类的父类
         */
        /*
        将对象输出时,会调用 toString()方法
        当类中没有定义toString()时,会默认调用父类(Object)中的toString。
        父类(Object)中的toString,将对象地址转为16进制的字符串输出,可以在类中重写Object类中的toString()
         public String toString() {
            return getClass().getName() + "@" + Integer.toHexString(hashCode());
           }
         public native int hashCode();
          native:调用本地方法,java语言不实现,调用操作系统实现
         */
        //创建一个People类,并重写Object类中的toString()方法
        People p = new People("jam",22);
        //调用People类中重写后的toString(),输出想要的 字符串
        System.out.println(p+"hello");


        /*
        Object类中的 equals() 比较对象地址是否相等,等同于 ==
        public boolean equals(Object obj) {
            return (this == obj);
          }
         */
        People p1 = new People("jam",22);
        People p2 = new People("jam",22);
        System.out.println(p1.equals(p2));
        System.out.println(p1==p2);

        /*
        其他类基本上都是重写了 equals() 比较的是内容是否相等
         */
        Integer a1 = 128;
        Integer a2 = 128;
        System.out.println(a1.equals(a2));//调用的是Integer类的equals(),比较的是值
        System.out.println(a1==a2);//128不在Integer范围区内,会创建Integer对象

    }

4.String类/StringBuffer类/StringBuilder类

String类

字符串是多个字符组成的字符序列,是常量(值是不能改变的)。

有两种创建形式:

  1. String s = “abc”;

​ 2.使用 new + 构造方法()创建

 public static void main(String[] args) {
        /*
        String s = "abc";
        String s1 = "abc";
        第一次创建时,在字符串常量池中检测有没有,有则指向,没有就在字符串常量池中创建一个对象,再指向对象
        第二次创建时,字符串常量池中有,则直接指向
         */
        String s = "abc";
        String s1 = "abc";
        System.out.println(s.equals(s1));//true,
        System.out.println(s==s1);//true

        /*
        使用 new + 构造方法()创建
        在堆中创建新对象,值存储在对内存的对象中
        只要是new创建的对象,在内存中一定是独一无二的对象空间
         */
        String s2 = new String("abc");
        String s3 = new String("abc");
        System.out.println(s2.equals(s3));//true
        System.out.println(s==s3);//false
    }

字符串值是常量,值不能改变,一旦改变就是在内存中重新创建一个新对象来保存这个新的值。

 /*
        private final char value[];
        字符串底层是char数组存储,单个字符存储
        final修饰 所以是常量不可改变
         */

String类中常用的方法:

1.判断功能的方法

 public static void main(String[] args) {
       
        /*
        判断功能
         */
        String s = "abcdefg";
        String s1= "abcdefg";
        System.out.println(s.equals(s1));//true 判断字符串值是否相等
        System.out.println(s.equalsIgnoreCase("aBCD"));//不区分大小写,判断字符串是否相同

        System.out.println(s.contains("a"));//判断字符串中是否包含子串
        System.out.println(s.contains("abc"));

        System.out.println(s.isEmpty());//判断字符串是否为空

        System.out.println(s.startsWith("ab"));//判断字符串是否是以指定前缀开头
        System.out.println(s.endsWith("fg"));//判断字符串是否是以指定后缀结尾

        System.out.println("c".compareTo("a"));//2   比较值(ASCLL/Unicode值相减)大小,用于排序比较
        System.out.println("a".compareTo("a"));//0
        System.out.println("a".compareTo("c"));//-2

    }

2.获取功能的方法

 public static void main(String[] args) {
         /*
        获取功能
         */
        String s2 = "abcdefgcd";
         //字符串下标:012345678
        System.out.println(s2.length());//返回字符串长度
        System.out.println(s2.charAt(2));//返回指定下标出处的字符
        System.out.println(s2.indexOf("d"));//从下标0处向后找,返回指定字符第一次出现的下标
        System.out.println(s2.indexOf("v"));//如果字符串中不包含指定字符,则返回-1
        System.out.println(s2.indexOf("c",3));//从指定位置(下标)向后找 字符首次出现的位置
        System.out.println(s2.indexOf("d",s2.indexOf("d")+1));//返回指定字符第二次出现的下标
        System.out.println(s2.lastIndexOf("c"));//从后往前找字符首次出现的位置
        System.out.println(s2.lastIndexOf("c",5));//从指定位置向前找 字符首次出现的位置

        System.out.println(s2.substring(2));//返回字符串从指定下标处开始到字符串结束的新字符串
        System.out.println(s2.substring(2,6));//返回字符串从指定下标开始到制定下标结束的新字符串


    }

3.替换功能的方法

 /*
    替换功能
     */
    public static void main(String[] args) {
        String s = "abcdefgbcfes";
        System.out.println(s.replace('e','E'));//参数为两个字符,用后面的字符替换前面的字符
        System.out.println(s.replace("bc","BC"));//用后面的字符串替换前面的字符串
        System.out.println(s);//字符串的值不可改变


        String s1 = "bcde1fbc3g";

        System.out.println(s1.replaceAll("\\d","BC"));//"\\d" 用指定字符串字符串替换原字符串中的数字
        System.out.println(s1.replaceFirst("\\d","BC"));//"\\d" 用指定字符串字符串替换原字符串中的第一个数字


        String s2 = " ab c ";
        System.out.println(s2.length());//长度为6
        String s3 = s2.trim();//去除字符串两端的空格
        System.out.println(s3.length());//长度为4
        String s4 = s2.replace(" ","");
        System.out.println(s4.length());

    }

4.转换功能的方法

  /*
    转换功能方法:
     */
    public static void main(String[] args) {
        String s = "abc";
        byte[] b =  s.getBytes();//字符串转换为字节数组   编码 字符——>字节
        String s1 = new String(b);//字节数组转换为字符串   解码 字节-->字符
        System.out.println(s1);

        String ss = "中文";
        try {
            //使用给定的字符集将该字符串编码为字节序列,
            byte[] bb = ss.getBytes("gbk");//gbk 规定一个中文对应两个字节
            System.out.println(Arrays.toString(bb));
            //通过 给定的字符集 将字节序列 解码为字符串
            String sb1 =new String(bb,"gbk");
            System.out.println(sb1);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {

            byte[] bb1 = ss.getBytes("utf-8");//utf-8 规定一个中文对应三个字节
            System.out.println(Arrays.toString(bb1));
            String sb2 =new String(bb1,0,3,"utf-8");//取从第0位开始的三个字节
            System.out.println(sb2);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String s2 ="abc";
        char[] c = s2.toCharArray();//字符串转换为字符数组
        System.out.println(Arrays.toString(c));
        String s3 = new String(c);//字符数组转换为字符串
        System.out.println(s3);
        
        String s4 = "abcdEFGH";
        System.out.println(s4.toUpperCase());//字符串全部转大写
        System.out.println(s4.toLowerCase());//字符串全部转小写
        
        /*
           String s5 = s4.concat("hello");
            String s6 = s4+"I"+"am"+"OK";
            concat() 连接方法 效率高于 “ ”+“ "
         */
        String s5 = s4.concat("hello");//字符串连接 将指定的字符串连接到该字符串的末尾。
        String s6 = s4+"I"+"am"+"OK";//字符串连接,效率最低的字符串拼接

        String s7 = "ab:cvafa:asdf:asfd:s";
        String[] s8 = s7.split(":");//将字符串用指定的字符分割为字符串数组
        System.out.println(Arrays.toString(s7));

        String s9= "ab5cvafa4asdf3asfd2s";
        String[] s10 = s9.split("\\d");//将字符串用指定的正则表达式分割为字符串数组
        System.out.println(Arrays.toString(s6));

    }

StringBuffer类

当我们对字符串进行拼接是,每次拼接,都会构建一个新的String对象,既耗时又浪费空间。而StringBuffer就可以解决这个问题。

StringBuffer:线程安全的可变字符序列

package day16;

public class StringBufferDemo {
    public static void main(String[] args) {
        /*
        StringBuffer类
        线程安全 的可变字符序列
        对字符串进行拼接,每次拼接,不会创建新对象
         */
        /*
        无参的构造方法,底层创建一个长度为16的char数组  char[] value;
         */
        StringBuffer s1 = new StringBuffer();
        //有参的构造方法
        StringBuffer s2 = new StringBuffer(10);//参数为底层创建数组的容量,即数组长度
        StringBuffer s3 = new StringBuffer("hello");//总长度为: 字符串长度+16
        /*
        大量的字符串拼接,建议使用StringBuffer,不会创建新的对象
         */
        s3.append("world");//字符串的拼接
        s3.append("asdmd");
//        s3.insert(4,"xxx");//在指定位置插入新的字串
//        s3.delete(0,4);//删除从指定起始位置到指定结束的字符串
//        s3.deleteCharAt(3) ;//删除指定位置的字符
//        s3.replace(0,5,"BB");//替换
//        s3.reverse();//字符串逆序/字符串反转
//        s3.substring(2);
        s3.substring(2,6);
//        System.out.println(s3.length());
        System.out.println(s3);
        System.out.println( s3.substring(2,6));
    }
}


StringBuilder类

public static void main(String[] args) {
        /*
        StringBuilder  内容可变
        StringBuffer   线程安全 内容可变
        String         内容不可变
         */
        StringBuilder s = new StringBuilder();
        //方法与StringBuffer相同
        s.append("hhh");
        System.out.println(s);
    }

5.Arrays类

java.util.Arrays类

用于操作数组工具类,里面定义了常见操作数组的静态方法。

1.equals方法

比较两个非同一数组是否相等,而数组本身的equals判断另一个数组是否是它本身。

 public static void main(String[] args) {
        /*
        java.util.Arrays类
        equals 方法
        比较两个非同一数组是否相等,而数组本身的equals判断另一个数组是否它本身
         */
        int [] a = {1,2,3,4,5};
        int [] b = {1,2,3,4,5};
        int [] c = {1,2,4,3,5};
        //比较数组内容是否一致
        System.out.println(Arrays.equals(a,b));//true
        System.out.println(Arrays.equals(a,c));//false
        //equal()为Object类中的方法  判断是否是它本身
        System.out.println(a.equals(a));//true
        System.out.println(a.equals(b));//false
        System.out.println(a.equals(c));//false

    }

2.sort–排序

public static void main(String[] args) {
        /*
        sort -排序
         */
        int[] d = {1,3,2,5,4};
        对基本类型数组元素进行排序
//         Arrays.sort(d);//[1, 2, 3, 4, 5]
        System.out.println(Arrays.toString(d));
        //对指定数组的指定部分的元素进行排序
        Arrays.sort(d,0,3);//[1, 2, 3, 5, 4]
        System.out.println(Arrays.toString(d));

        /*
         对引用类型进行排序
        自定义对象排序
        自定义类实现Comparable接口:  implements Comparable
        重写compareTo方法
         */
        Student s1 = new Student("aim",60,90,150);
        Student s2 = new Student("cim",70,90,160);
        Student s3 = new Student("bim",60,60,120);
        Student[] starray = {s1,s2,s3};
        Arrays.sort(starray);
        System.out.println(Arrays.toString(starray));
    }

3.数组复制

 public static void main(String[] args) {

        int [] a = {5,2,4,3,1};
        //数组复制,当数组长度不够用,将原来的内容复制到一个新数组中,指定新数组长度
        int [] b = Arrays.copyOf(a,10);
        System.out.println(Arrays.toString(b));//[5, 2, 4, 3, 1, 0, 0, 0, 0, 0]
    }

4.二分折半查找

 public static void main(String[] args) {
        int[] a = {5,2,3,4,1};
        //二分折半查找   前提:数组有序
        Arrays.sort(a);//对数组进行排序 从小到大
        System.out.println(Arrays.toString(a));

        int index = Arrays.binarySearch(a,1);
        System.out.println(index);//查找完返回下标
        int index2 = Arrays.binarySearch(a,5);
        System.out.println(index2);
        int index1 = Arrays.binarySearch(a,0,3,2);//在数组的指定部分进行查找
        System.out.println(index1);
    }

6.Math类

java.lang.Math提供了一系列静态方法用于科学计算。

public static void main(String[] args) {
        /*
        Math类
        java.lang.Math类提供了一系列静态方法用于科学计算
         */
        System.out.println(Math.PI);//Math类中的常量
        System.out.println(Math.abs(-23));//取绝对值
        System.out.println(Math.sqrt(16));//取平方根
        System.out.println(Math.pow(2,3));//2的3次幂
        System.out.println(2<<2);//左移  2<<2==Math.pow(2,3)

        System.out.println(Math.ceil(5.3));//向上取整
        System.out.println(Math.floor(4.8));//向下取整
        System.out.println(Math.round(6.1));//四舍五入
        System.out.println(Math.round(6.9));

        System.out.println(Math.random());//返回[0,1)的随机数(包含0,不包含1)
    }

7.Random类

java.util.Random类用于产生随机数。

   public static void main(String[] args) {
           /*
        Random类:用于产生随机数
         */
        Random r = new Random();
        System.out.println(r.nextBoolean());//获取一个随机的boolean值
        r.nextInt();//产生一个int范围的随机数
        System.out.println(r.nextInt());
        r.nextInt(10);//给一个指定值,产生一个[0,指定值)范围的随机数(包含0,不包含指定值)
        System.out.println(r.nextInt(10));

        byte[] b = new byte[10];
        r.nextBytes(b);//向数组中装入数组 .length 个byte随机数,值的范围为byte类型的范围
        System.out.println(Arrays.toString(b));
    }

8.System类

java.lang.System类包含一些有用的类字段和方法。它不能被实例化。

 public static void main(String[] args) {
      /*
        System类:System类包含一些有用的类字段和方法。不能被实例化
         */
        /*
        public final static PrintStream out = null;
        println() 是PrintStream中的方法
         */
        System.out.println("你好");

        long s =  System.currentTimeMillis();
        System.out.println(System.currentTimeMillis());//1608208114102  返回当前时间(以毫秒为单位)。
                                                       // 在1970年1月1日UTC之间的当前时间和午夜之间的差异,以毫秒为单位。
                                                       //1970 1.1 0.0.0 ---至今的毫秒值差
        for (int i = 0; i <1000000; i++) {
            System.out.println(i);
        }
        System.out.println(System.currentTimeMillis()-s);//for 循环运行时间 单位为毫秒

       /* System.exit(0);//关闭退出虚拟机
        System.out.println("hello world");//不执行*/

        System.out.println(System.getenv());//返回当前系统环境的不可修改的字符串映射视图。
        System.out.println(System.getenv("Path"));//获取指定环境变量的值。

        System.out.println(System.getProperties());//确定当前的系统属性。
        System.out.println(System.getProperty("java.runtime.version"));//当前运行版本  获取指定键指示的系统属性。

        /*int[] a = {1,2,3,4,5};
        int[] b = Arrays.copyOf(a,10);//数组的复制(方法一)*/

        int[] a = {1,2,3,4,5};
        int[] b = new int[10];
        System.arraycopy(a,0,b,0,a.length);//数组的复制(方法二)
        System.out.println(Arrays.toString(b));

    }

9.Date类/Calendar类/ SimpleDateFormat类

Date类

java.util.Date类:使用Date类代表当前系统时间。

public static void main(String[] args) {
        /*
        Data 类:使用Date类代表当前系统时间
         */
        /*Date date = new Date();//date对象表示当前程序运行的那一刻时间
        System.out.println(date);//输出当前时间   Thu Dec 17 21:04:45 CST 2020
        */
        /*Date date1 = new Date(1608208114102l);//传入一个long型 参数时间
        System.out.println(date1);
        System.out.println(date1.getTime());// ==  System.currentTimeMillis()
        */
        Date date = new Date(1608208114102l);
        System.out.println(date);
        date.setTime(1608208114102l);//修改时间
        date.setDate(15);//修改日期
        System.out.println(date.getYear()+1900);//getYear()==120,输出2020=120+1900,今年
        System.out.println(date.getMonth()+1);//月份从零开始,所以求当前月份要加1
        System.out.println(date.getDate());//获取日期,DayOfMonth
        System.out.println(date.getDay());//0(星期天) 1 2 3 4 5 6
        System.out.println(date.getHours());//一天中的第几个小时
        System.out.println(date.getMinutes());//一小时中的第几分钟

    }

Calendar类

java.util.Calendar类:Calendar类是一个抽象类,在实际使用时实现特定的子类的对象。

 public static void main(String[] args) {
       /*
      Calendar类:Calendar类是一个抽象类,在实际使用时实现特定的子类的对象
     */
      //  Calendar rightnow = new Calendar.getInstance();
        Calendar rightnow = new GregorianCalendar();//GregorianCalendar是的具体子Calendar ,并提供了世界上大多数国家使用的标准日历系统。

        System.out.println(rightnow.getTime());//获取当前时间
        System.out.println(rightnow.getTimeInMillis());// ==  System.currentTimeMillis()
                                                       // ==  date.getTime()

        rightnow.set(2023,6,1);//自定义一个日期
        //获取自定义日期的信息,如年、月、DAY_OF_MONTH、DAY_OF_WEEK(星期)等
        System.out.println(rightnow.get(Calendar.YEAR));
        System.out.println(rightnow.get(Calendar.MONTH));
        System.out.println(rightnow.get(Calendar.DAY_OF_MONTH));
        System.out.println(rightnow.get(Calendar.DAY_OF_WEEK));
        System.out.println(rightnow.get(Calendar.DAY_OF_YEAR));
        System.out.println(rightnow.get(Calendar.WEEK_OF_YEAR));

    }

SimpleDateFormat类

java.text.SimpleDateFormat类:日期格式化类。

 public static void main(String[] args) {
        /*
        SimpleDateFormat 日期格式化类
         */
    //1.日期对象 转为 指定格式的字符串
        Date date = new Date();
        System.out.println(date);//  Sat Dec 19 10:57:53 CST 2020
      //  SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E", Locale.FRANCE);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E", Locale.FRANCE);
        String str = sdf.format(date);
        System.out.println(str);//  2020-12-19 10:57:53 sam.

    //2.字符串 转为 日期对象
        String birthday = "2000-3-21";
        System.out.println(birthday); //  2000-3-21
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date1 = sdf1.parse(birthday);//从字符串中解析文本以产生一个 Date 。
            System.out.println(date1);  //Tue Mar 21 00:00:00 CST 2000
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

10.BigInteger类

java.math.BigInteger 类:不可变的任意精度整数。

BigInteger类型的数字范围较Integer,Long类型的数字范围要大得多,它支持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何大小的整数值而不会丢失任何信息。

    public static void main(String[] args) {
        /*
    BigInteger类     不可变的任意精度整数。
     */
        BigInteger a = new BigInteger("111111111111111111111111111111111111111111111");
        System.out.println(a);
        BigInteger b = new BigInteger("11111111111111111111111111111111111111111111111");

        BigInteger c = a.add(b);//加, a.add(b)是一个新的 BigInteger对象
        System.out.println(c);
        System.out.println(a);
        System.out.println(b);

        System.out.println(a.subtract(b));//减
        System.out.println(a.multiply(b));//乘
        System.out.println(a.divide(b));//除

    }

11.BigDecimal类

java.math.BigDecimal类:不变的,任意精度的带符号的十进制数字。

在计算机中不论是float还是double都是浮点数,而计算机是二进制的,浮点数会失去一定的精确度。

根本原因是:十进制值通常没有完全相同的二进制表现形式;十进制数的二进制表现形式可能不精确。只能无限接近于那个值。

        double a = 1.0-0.9;
        System.out.println(a);//0.09999999999999998
        double b = 0.8-0.7;
        System.out.println(b);//0.10000000000000009
        System.out.println(a==b);//false

所以,Java在java.math包中提供的API类BigDecimal。

 public static void main(String[] args) {
        /*
        BigDecimal类   不变的,任意精度的带符号的十进制数字。
         */
        System.out.println(10.9-10.8 );//0.09999999999999964
        System.out.println(10.9-10.8 == 0.1);//false

        BigDecimal b1 = new BigDecimal("10");
        System.out.println(b1);
        BigDecimal b2 = new BigDecimal("3");
        // BigDecimal.ROUND_CEILING   圆形模式向正无穷大转弯。
//        System.out.println(b1.divide(b2));
        System.out.println(b1.divide(b2,15,BigDecimal.ROUND_CEILING));// 3.333333333333334
        // BigDecimal.ROUND_FLOOR     舍入模式向负无穷大转弯。
        System.out.println(b1.divide(b2,6,BigDecimal.ROUND_FLOOR));//  3.333333
    }

12.补充:正则表达式

正则表达式:rexgular-expression 简称:regex

正则表达式是一种模式匹配语法,有雨多特定字符组成,每种字符匹配一种规则。

我们使用这些特定的字符,匹配某个字符串,判断字符串中是否满足规则需求

 public static void main(String[] args) {
        /*
        正则表达式:
        \d [0-9]  匹配数字
        \D [^0-9]  匹配非数字
        \w [a-zA-Z_0-9] 匹配单词字符
         [a-zA-Z] [A-z] 匹配英文字母
        数量词:
        ?:一次或一次也没有
        *:0次或多次
        +:一次或多次
        {n}:恰好nci/固定n次
        {n,}:至少n次
        {n,m}:至少n次,至多m次
         */
        String str = "adb";
        //匹配字符串时,要用符合有需求的 regex + 数量词 。无数量词时,默认匹配一位
        //System.out.println(str.matches("\\d"));//   \d 匹配数字(默认匹配一位)
        //System.out.println(str.matches("[0-9]*"));//  [0-9]* 匹配数字0次或多次  *(数量词)
        //System.out.println(str.matches("\\d?"));// \d? 匹配数字一次或一次也没有
        //System.out.println(str.matches("\\D*"));// \D* 匹配非数字0次或多次
        //System.out.println(str.matches("\\w*"));//  \w* 匹配单词字符0次或多次
        //System.out.println(str.matches("[A-z]*")); // [A-z]*  匹配英文大小写字母0次或多次
        //System.out.println(str.matches("\\D{3,6}"));// \D{3,6} 匹配非数字,至少3次至多6次
        //System.out.println(str.matches("[0-9]{3,6}"));//[0-9]{3,6} 匹配数字0-9,至少3次至多6次
        //System.out.println(str.matches("[3157]{3,6}"));//[3157]{3,6} 匹配数字1357,至少3次至多6次
        System.out.println(str.matches("[a-zA-Z]{3,8}"));// [a-zA-Z]{3,8} 匹配英文大小写字母,至少3次至多8次
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白居不易.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值