Loading

JAVASE进阶day04(常用api)

什么是aip

简单来说就是已经实现好的功能,并且封装成了供调用的方法

为什么要用api

避免重复造轮子

String

1.String

package com.lu.day04.stringapi;


/**
 * 基本字符串的判断比较重要-》以。。开头,判断相等
 */
public class StringDemo {
    public static void main(String[] args) {
        String s = "    Hello World!!    ";
        //jdk11新增的一系列strip方法
        s.isBlank();
        String strip = s.strip();
        System.out.println(strip);
        System.out.println(s);
        //去除尾空格
        s.stripTrailing();
        //去除首空格
        s.stripLeading();
        //jdk11新增
        String s1 = "  ";
        //isEmpty() 方法用于判断字符串是否为空,即字符串的长度是否为 0。
        //isBlank() 方法则更严格,它不仅判断字符串的长度是否为 0,还会判断字符串中是否只包含空白字符,例如空格、制表符、换行符等。
        System.out.println(s1.isBlank());
        System.out.println(s1.isEmpty());//jdk1.6新增
    }
}

2.StringBuilder

package com.lu.day04.stringapi;

/**
 * 基本的字符串增删改查
 */
public class StringBuilderDemo{
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        //新增
        stringBuilder.append("Hello World");
        //删除
        stringBuilder.delete(0,4);
        //改(replace,insert)
        stringBuilder.replace(0,3,"bad");
        stringBuilder.insert(0,"Hello");
        //查
        System.out.println(stringBuilder.toString());

    }
}

3.StringJoiner

package com.lu.day04.stringapi;

import java.util.StringJoiner;

/**
 * 字符串的格式拼接
 */
public class StringJoinerDemo {
    public static void main(String[] args) {
        StringJoiner stringJoiner = new StringJoiner(",","[","]");
        stringJoiner.add("张天");
        stringJoiner.add("王五");
        stringJoiner.add("张天志");
        stringJoiner.add("弯路");
        System.out.println(stringJoiner);
    }
}

@toStirng生成可以自己选择是StringBuilder生成还是StringJoiner生成

System(跟系统相关工具类)

1.currentTimeMillis

package com.lu.day04.systemapi;

public class SystemDemo {
    public static void main(String[] args) {
        //时间戳:时间的整数形式
        //时间计算原点:1970.1.1零点
        long l = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 100000; i++) {
            stringBuilder.append(i);
        }
        //为什么StringBuilder比+拼接快因为+号底层就是StringBuilder
        //+创建了很多对象
        long l1 = System.currentTimeMillis() - l;
        System.out.println(l1);
    }
}

2.gc(),arraycopy,exit(0)

package com.lu.day04.systemapi;

/**
 * 系统工具类中的方法基本都是native的本地方法,源码是由c/c++实现的
 */
public class SystemDemo1 {
    public static void main(String[] args) {
        int[] src = {1,2,3,4,5};
        int[] dest = new int[src.length];
        //数组拷贝
        System.arraycopy(src,0,dest,0,src.length);
        for (int j : dest) {
            System.out.println(j);
        }
        System.exit(0);//退出jvm虚拟机
        System.out.println("hello");
        System.gc();//垃圾收集
    }
}

Math(数学工具类)

package com.lu.day04.mathapi;

/**
 * 数学工具类
 */
public class MathDemo {
    public static void main(String[] args) {
        //1.abs 绝对值
        System.out.println(Math.abs(-1));
        //2.round 四舍五入
        System.out.println(Math.round(1.6));
        //3.ceil 向上取整 floor 向下取整
        System.out.println(Math.ceil(1.3));
        System.out.println(Math.floor(1.3));
        //4.max min
        System.out.println(Math.max(1, 10));
        System.out.println(Math.min(1, 10));
        //5.pow 幂
        System.out.println(Math.pow(2, 3));
        //6.random 随机数 [0,10)
        System.out.println(Math.random());
        //Π
        System.out.println(Math.PI);
    }
}

Object

1.所有的类的直接或间接的父类

2.toString及其重载方法

package com.lu.day04.objectapi;

import java.util.Objects;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge());
    }

    @Override
    public boolean equals(Object o) {
        //如果两个对象的地址值一样,return true->判断是不是同一个对象
        if (this == o) return true;
        //判断要比较的参数与比较是不是同一个类型的->不是返回false
        if (!(o instanceof Student student)) return false;
        //挨个比较属性值是否相等
        return getAge() == student.getAge() && getName().equals(student.getName());
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package com.lu.day04.objectapi;

public class Test {
    public static void main(String[] args) {
        Student student = new Student("李四",20);
        Student student1 = new Student("李四", 20);

        //object中默认比较的是对象的地址值
        System.out.println(student1.equals(student));
    }
}

Objects

package com.lu.day04.objectsapi;

import java.util.Arrays;
import java.util.Objects;

public class ObjectDemo {
    public static void main(String[] args) {
        int[] arr = null;
        sumArr(arr);
    }
    public static void sumArr(int[] arr){
        if (Objects.isNull(arr)){
            return;
        }
        if (Objects.nonNull(arr))
        int sum = 0;
        sum = Arrays.stream(arr).sum();
        System.out.println(sum);
    }
    public static void arrIsNull(int arr){
        Objects.requireNonNull(arr,"数组不能为空");//如果数组为空则抛出异常
    }
}

BigDecimal

1.BigDecimal的基本使用

package com.lu.day04.bigdecimalapi;

import java.math.BigDecimal;

/**
 * BigDecimal解决某些领域需要精确计算小数以及控制小数位数的情况
 */
public class BigDecimalDemo1 {
    public static void main(String[] args) {
        double a =1;
        double b = 20.2;
        double c =300.03;
        //jdk1.5引入的静态方法会自动创建BigDecimal并把double转成字符串
        BigDecimal abigDecimal = BigDecimal.valueOf(a);
        BigDecimal bbigDecimal = BigDecimal.valueOf(b);
        BigDecimal cbigDecimal = BigDecimal.valueOf(c);
        BigDecimal add = abigDecimal.add(bbigDecimal).add(cbigDecimal);
        System.out.println(add);
    }
}
package com.lu.day04.bigdecimalapi;

import java.math.BigDecimal;

/**
 * BigDecimal解决某些领域需要精确计算小数以及控制小数位数的情况
 */
public class BigDecimalDemo {
    public static void main(String[] args) {
        System.out.println(0.1+0.2);
        double a =1;
        double b = 20.2;
        double c =300.03;
        System.out.println(a+b+c);
        //传递double构造器无法解决计算精确问题
        BigDecimal bigDecimal = new BigDecimal(a);
        BigDecimal bigDecimal1 = new BigDecimal(b);
        BigDecimal bigDecimal2 = new BigDecimal(c);
        BigDecimal add = bigDecimal.add(bigDecimal2).add(bigDecimal1);
        System.out.println(add);
        System.out.println("---------");
        BigDecimal bigDecimal3 = new BigDecimal(String.valueOf(a));
        BigDecimal bigDecimal4 = new BigDecimal(String.valueOf(b));
        BigDecimal bigDecimal5 = new BigDecimal(String.valueOf(c));
        BigDecimal add1 = bigDecimal3.add(bigDecimal4).add(bigDecimal5);
        System.out.println(add1);
    }
}

2.BigDecimal加减乘除方法

package com.lu.day04.bigdecimalapi;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 累加求和
 */
public class Test {
    public static void main(String[] args) {
        double[] arr = {23.5465,5465.46,6756.5657};
        BigDecimal bigDecimal = BigDecimal.ZERO;//封装的零常量

        for (int i = 1; i < arr.length; i++) {
            bigDecimal = bigDecimal.add(BigDecimal.valueOf(arr[i])).setScale(2, RoundingMode.DOWN);
        }
        System.out.println(bigDecimal.doubleValue());

    }
}

基本数据类型包装类

1.为什么使用包装类?

将基本类型包装成对象,更符合面向对象的思想。

使用封装技术把基本类型封装成包装类之后,能有更加丰富的操作。

2.Integer

package com.lu.day04.box;

public class IntegerDemo {//自动装箱与拆箱只存在与8个包装类(对应8中基本数据类型)中
    public static void main(String[] args) {
        //1.如何把基本数据类型变成包装类
        //过时的构造方法
        int a=10;
        //Integer integer = new Integer(a);
        //使用静态方法valueOf
        Integer integer = Integer.valueOf(a);
        System.out.println(integer);
        //装箱
        Integer c = Integer.valueOf(100);
        Integer b = Integer.valueOf(200);
        //拆箱
        int i = c.intValue();
        int i1 = b.intValue();
        System.out.println(a+b);
        //自动装箱与拆箱->jvm自动调用valueOf方法
        Integer d = 300;
        Integer e = 500;
        System.out.println(d+e);

    }
}

3.基本数据类型和包装数据类型之间的转换

package com.lu.day04.box;

public class IntegerDemo1 {
    public static void main(String[] args) {
        //类型转换 字符串与包装类的转换
        //基本数据类型转字符串 String.valueOf
        String s = String.valueOf(213);
        System.out.println(s);

        //把字符串转成数值 得是正常的数字字符串
        Integer integer = Integer.valueOf("123");
        int i = Integer.parseInt("234");
        System.out.println(integer);
        System.out.println(i);
        
        Boolean false1 = Boolean.valueOf("false1");
        System.out.println(false1);
        boolean aTrue = Boolean.parseBoolean("true");//解析不了就是false
        System.out.println(aTrue);
    }
}

 4.包装类比较大小

package com.lu.day04.box;

public class IntegerDemo2 {
    public static void main(String[] args) {
        //包装类比较大小
        //面试题为什么Integer不能用==来比较
        //自动装箱的valueOf方法会缓存[-128,127]之间的常用整数值,因此地址值就一样
        //超出这个返回就new一个新对象所以不能用==
        Integer a = Integer.valueOf(128);
        Integer b = Integer.valueOf(128);
        System.out.println(a==b);
        System.out.println(a.equals(b));
        //0相等
        //-1前小于后
        //1前大于后
        System.out.println(a.compareTo(b));

    }
}

面试题

1.==和equals的区别

==对基本数据类型比较值,引用数据类型比较的是地址

equals不重写比较地址值,重写比较内容(看重写逻辑)

2.System.exit(0)和System.gc()是干嘛用的?

System.exit(0)用来退出当前jvm虚拟机

System.gc()垃圾回收机制,垃圾回收

3.为什么不建议在循环中直接用加号拼接字符串?

用+拼接底层用的是StringBuilder每循环一次就创建了一次StringBuilder对象比较浪费内存,但用StringBuilder就不会了。

4.为什么Integer不能用==来比较

自动装箱的valueOf方法会缓存[-128,127]之间的常用整数值,因此地址值就一样 超出这个返回就new一个新对象所以不能用==

5.在java中double类型0.1+0.2为什么不等于0.3

在 Java 中,由于二进制表示小数的局限性,导致一些小数在计算机中无法精确表示。

对于 0.1 + 0.2,在二进制中,0.1 和 0.2 都无法精确表示,它们的二进制表示是无限循环的。当进行加法运算时,会出现舍入误差,最终得到的结果与 0.3 存在微小的差异。

这是计算机处理浮点数时的常见现象,不仅仅在 Java 中,其他编程语言也会遇到类似的问题。

如果需要进行精确的小数计算,可以考虑使用 BigDecimal 类来避免浮点数的精度问题。

posted @ 2024-07-04 12:42  LL。。。  阅读(14)  评论(0)    收藏  举报  来源