【Java基础11】常用API(一)

1. Object

Object是所有类的基类,包括数组。当一个类没有继承其他父类,则默认自动继承Object。

1.1 toString

public String toString();

默认此方法返回的是(全类名@地址)。此信息通常来说是无用的,使用者通常想通过toStrong
来获得类内部详细信息。所以此方法存在的意义就是被子类重写,显示重要信息。

// 示例类
public class Student {
    private String name;
    private int age;
    private String sex;

    public Student() {
    }

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

    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

// Student类中添加方法
@Override
public String toString() {
    return "Student{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", sex='" + sex + '\'' +
            '}';
}

// 示例
public class Test1 {
    public static void main(String[] args) {
        Student student = new Student("张三", 18, "男");
        System.out.println(student);
    }
}

1.2 equals

public boolean equals(Object o);

默认比较当前类和被传进来的对象进行地址对比,结果返回为boolean。
对比两个变量可以直接使用==,但是这只适用基础类型,引用类型就会有问题。
对象比较需要重写equals方法,进行比较

// 在Student中添加方法
@Override
public boolean equals(Object o){
    // 判断两个类是否地址相同
    if(this==o)return true;
    // 判断传入来的是否为空,与当前类类型是否相同
    if(o==null||getClass()!=o.getClass())return false;
    Student student=(Student)o;
    // 判断两对象成员变量内容是否相同
    return age==student.age&&name.equals(student.name)&&sex.equals(student.sex);
}

// 示例
public class Test2 {
    public static void main(String[] args) {
        Student student1 = new Student("A",18,"男");
        Student student2 = new Student("B",20,"女");
        Student student3 = new Student("B",20,"女");

        System.out.println(student1==student2);
        System.out.println(student2==student3);
        System.out.println(student1.equals(student2));
        System.out.println(student2.equals(student3));
    }
}

2. Objects

这是jdk自带的工具类,此类中的方法全为静态方法,可以直接调用。

2.1 equals

public static boolean equals(Object a, Object b)

对比两个类,是否全部相同,但是被比较类还是需要重写equals方法

// 示例
public class Test3 {
    public static void main(String[] args) {
        Student student1 = new Student("A",18,"男");
        Student student2 = new Student("B",20,"女");
        Student student3 = new Student("B", 20, "女");

        System.out.println(Objects.equals(student1, student2));
        System.out.println(Objects.equals(student2, student3));
    }
}

2.2 isNull

public static boolean isNull(Object o) {
    return (a == b) || (a != null && a.equals(b));
}

判断变量是否为空null

public class Test4 {
    static int b;
    static String x;
    static String y = "aa";
    static int a = 10;

    public static void main(String[] args) {
        System.out.println(Objects.isNull(x));
        System.out.println(Objects.isNull(y));
        System.out.println(Objects.isNull(a));
        System.out.println(Objects.isNull(b));

    }
}

3. String

String表示字符串,java中所有的字符串都会被实例化成String类型

3.1 构造器

new String()

new String(String str)

new String(byte[] bytes)

...

3.2 常用方法

// 获得字符串中对应下标的字符
public char charAt(int index)

// 获得字符串字符数组
public byte[] getBytes()

// 返回指定子字符串第一次出现的字符串内的索引
public int indexOf(String str)

// 返回指定子字符串最后一次出现的字符串中的索引
public int lastIndexOf(String str)

// 返回字符串长度
public int length()

// 返回替换第一个出现的字符串 把oldChar替换成newChar
public String replace(char oldChar, char newChar)
public String replace(String oldChar, String newChar)

// 用给定的字符串替换老字符串中的所有符合规则的字符串
public String replaceAll(String regex, String replacement)

// 按照一定规则为字符串分割返回字符串数组
public String[] split(String regex)

// 测试此字符串是否以指定的字符串开头
public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int toffset)

// 测试此字符串是否以指定的字符串结尾
public boolean endsWith(String suffix)

// 字符串小写
public String toLowerCase()

// 字符串大写
public String toUpperCase()

// 去除字符串前后空格并返回
public String trim()

3.3 示例

public class StringTest {
    public static void main(String[] args) {
        String x = " You think you can, you can ";

        System.out.println(x.charAt(1));
        System.out.println(Arrays.toString(x.getBytes()));
        System.out.println(x.indexOf("you"));
        System.out.println(x.lastIndexOf("can"));
        System.out.println(x.length());
        System.out.println(x.replace("you", "me"));
        System.out.println(x.replaceAll("you", "me"));
        System.out.println(Arrays.toString(x.split(" ")));
        System.out.println(x.startsWith(" You"));
        System.out.println(x.startsWith("You"));
        System.out.println(x.endsWith("can"));
        System.out.println(x.endsWith("can "));
        System.out.println(x.toUpperCase());
        System.out.println(x.toLowerCase());
        System.out.println(x.trim());
    }
}

4. StringBuilder

这是一个字符串优化操作类,这是1.5之后开始的。String是不可变的,此类是字符串可变的。

4.1 构造器

new StringBuilder()

new StringBuilder(String str)

4.2 常用方法

// 在字符串尾部追加内容
public StringBuilder append(Object o)

// 将字符串反转并返回 
public StringBuilder reverse()

// 返回字符串长度
public int length()

// 将类型转换成String
public String toString()

4.3 示例

public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("Hello World");

        System.out.println(stringBuilder);

        stringBuilder.append(" Java");

        System.out.println(stringBuilder);

        System.out.println(stringBuilder.reverse());

        System.out.println(stringBuilder.length());

        String x = stringBuilder.toString();
        System.out.println(x);
    }
}

4.4 String和StringBuilder

  • String不可变,StringBuilder是可变的
  • String在每次赋值的时候是创建新的对象,StringBuilder是每次从常量池中获得修改值

5. Math

本类提供基本数学计算,没有提供公开构造方法,直接调用静态方法。

5.1 常用方法

// 绝对值
public static int abs(int a)
// 向上取整
public static double ceil(double a)
// 向下取整
public static double floor(double a)
// 四舍五入
public static int round(float a)
// 取最大值
public static int max(int a,int b)
// 返回a的b次幂
public static double pow(double a,double b)
// 返回随机值[0.0~1.0]
public static double random() 

5.2 示例

public class MathTest {
    public static void main(String[] args) {
        System.out.println(Math.abs(-10));
        System.out.println(Math.ceil(4.4));
        System.out.println(Math.floor(4.4));
        System.out.println(Math.max(2, 9));
        System.out.println(Math.pow(2, 2));

        for (int i = 0; i < 10; i++) {
            System.out.println(i + ":" + Math.random());
        }
    }
}

6. System

这是一个系统类,提供了与系统相关的内容,代表当前系统。

6.1 常用方法

// 阻止当前运行的jvm,当status非0时表示异常终止
public static void exit(int status)
// 放回当前系统时间,毫秒(开始时间)
public static long currentTimeMillis()
// 数组拷贝
public static void arraycopy(数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数)

6.2 示例

public class SystemTest {
    public static void main(String[] args) {

        int x[] = {1, 3, 5}, y[] = {2, 4, 6};

        System.out.println(System.currentTimeMillis());
        System.arraycopy(x, 0, y, 0, y.length);
        System.out.println(Arrays.toString(x));
        System.out.println(Arrays.toString(y));

        System.exit(0);
    }
}

7. BigDecimal

主要是解决浮点型计算精度丢失问题

7.1 常见方法

// 初始化BigDecimal
public static BigDecimal valueOf(double val)
// 加
public BigDecimal add(BigDecimal b)
// 减
public BigDecimal subtract(BigDecimal b)
// 乘
public BigDecimal multiply(BigDecimal b)
// 除
public BigDecimal divide(BigDecimal b)
// 除,精确到指定位数
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) 

7.2 示例

public class BigDecimalTest {
    public static void main(String[] args) {
        BigDecimal bigDecimal1 = BigDecimal.valueOf(4.0);

        System.out.println(bigDecimal1.add(BigDecimal.valueOf(3.1)));
        System.out.println(bigDecimal1.subtract(BigDecimal.valueOf(1.1)));
        System.out.println(bigDecimal1.multiply(BigDecimal.valueOf(3)));
        // 下面方法会报错,结果不能整除,所以需要添加四舍五入位数
        // System.out.println(bigDecimal1.divide(BigDecimal.valueOf(3)));
        System.out.println(bigDecimal1.divide(BigDecimal.valueOf(3), 3));

    }
}

本章结束,用于个人学习和小白入门,大佬勿喷!希望大家多多点赞收藏支撑支撑!

源码 【GitHub】 【码云】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值