栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java API(Math, System, Object, Objects, BigDecimal, lnteger, Arrays)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java API(Math, System, Object, Objects, BigDecimal, lnteger, Arrays)

目录
  • 1.Math
  • 2.System
  • 3.Object
    • 3.1 toString
    • 3.2 equals
    • 3.3 面试题
  • 4.Objects
  • 5.BigDecimal
  • 6.基本数据类型包装类
  • 7.Arrays

1.Math

Math 包含执行基本数字运算的方法。

没有构造方法。

所有成员都是静态的,通过类名可以直接调用。

package com.qdu.demo1;

public class MathDemo {
    public static void main(String[] args) {
        // public static int abs(int a)     返回参数的绝对值
        int abs = Math.abs(10);
        System.out.println(abs); // 10

        // public static double ceil(double a)      向上取整
        double ceil = Math.ceil(10.1);
        System.out.println(ceil); // 11.0

        // public static double floor(double a)     向下取整
        double floor = Math.floor(10.9);
        System.out.println(floor); // 10.0

        // public static int round(float a)     四舍五入
        long round = Math.round(10.1);
        System.out.println(round); // 10

        long round1 = Math.round(1.9);
        System.out.println(round1); // 2

        // public static int max(int a, int b)      返回两个int值中的较大值
        int max = Math.max(10, 20);
        System.out.println(max); // 20

        // public static int min(int a, int b)      返回两个int值中的较小值
        int min = Math.min(10, 20);
        System.out.println(min); // 10

        // public static double pow(double a, double b)     返回a的b次幂的值
        double pow = Math.pow(2, 3);
        System.out.println(pow); // 8.0

        // public static double random()        返回值为double的正值 [0.0, 1.0)
        for (int i = 0; i < 10 ; i++) {
            double random = Math.random();
            System.out.println(random);
        }
    }
}
2.System

System 不能实例化。

所有成员都是静态的,通过类名可以直接调用。

package com.qdu.demo1;

public class SystemDemo {
    public static void main(String[] args) {
        // public static void exit(int status)  终止当前运行的Java虚拟机,非零表示异常终止
        System.out.println(111);
        System.exit(0);

        // public static long currentTimeMillis()   返回当前时间(以毫秒为单位)
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);

        // arraycopy(数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数)    数组拷贝
        int [] arr1 = {1, 2, 3, 4, 5};
        int [] arr2 = new int[10];
        
        // 把arr1中的数据拷贝到arr2中
        System.arraycopy(arr1, 0, arr2, 0, arr1.length);
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }

        // 把arr1最后两个元素拷贝到arr2的最后两个索引上
        System.arraycopy(arr1, 3, arr2, 8, 2);
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }
}
3.Object

每个类都可以将 Object 作为父类。所有类都直接或者间接地继承该类。

构造方法:public Object()

为什么说子类的构造方法默认访问的是父类的无参构造方法?

因为它们的顶级父类只有无参构造方法。

3.1 toString

Object 类是所有类的直接或者间接父类。

直接打印一个对象就是打印这个对象的 toString 方法的返回值。

Object 类的 toString 方法得到的是对象的地址值。

我们一般会对 toString 方法进行重写。

package com.qdu.demo1;

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 String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}
package com.qdu.demo1;

public class Demo {
    public static void main(String[] args) {
        Student s = new Student("张三",23);
        System.out.println(s);
        System.out.println(s.toString());
    }
}

3.2 equals

package com.qdu.demo2;

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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}
package com.qdu.demo2;

public class Demo {
    public static void main(String[] args) {
        Student s1 = new Student("zhangsan",23);
        Student s2 = new Student("zhangsan",23);

        System.out.println(s1 == s2); // false
        System.out.println(s1.equals(s2)); // true
    }
}
3.3 面试题

(1) 调用 String 类中的 equals 方法时,必须保证参数也是字符串,否则不会比较属性值而直接返回 false。

package com.qdu.demo3;

public class InterviewTest {
    public static void main(String[] args) {
        String s1 = "abc";
        StringBuilder sb = new StringBuilder("abc");
        System.out.println(s1.equals(sb)); // false
    }
}

(2) StringBuilder 类中没有重写 equals 方法,默认使用 Object 类中的 equals 方法。

package com.qdu.demo3;

public class InterviewTest {
    public static void main(String[] args) {
        String s1 = "abc";
        StringBuilder sb = new StringBuilder("abc");
        System.out.println(sb.equals(s1)); // false
    }
}
4.Objects

没有构造方法。

所有成员方法都是静态的,通过类名可以直接调用。

package com.qdu.demo4;

import java.util.Objects;

public class MyObjectsDemo {
    public static void main(String[] args) {
        // public static String toString(对象)    返回参数中对象的字符串表示形式
        Student s = new Student("小罗同学",50);
        System.out.println(s); // Student{name='小罗同学', age=50}
        String result = Objects.toString(s);
        System.out.println(result); // Student{name='小罗同学', age=50}

        // public static String toString(对象, 默认字符串)     返回对象的字符串表示形式,如果对象为空,那么返回第二个参数
        result = Objects.toString(s, "随便写一个");
        System.out.println(result); // Student{name='小罗同学', age=50}
        Student s1 = null;
        result = Objects.toString(s1, "随便写一个");
        System.out.println(result); // 随便写一个

        // public static Boolean isNull(对象)     判断对象是否为空
        boolean res = Objects.isNull(s);
        System.out.println(res); // false
        res = Objects.isNull(s1);
        System.out.println(res); // true

        // public static Boolean nonNull(对象)    判断对象是否不为空
        res = Objects.nonNull(s);
        System.out.println(res); // true
        res = Objects.nonNull(s1);
        System.out.println(res); // false
    }
}
5.BigDecimal

BigDecimal 是用来进行精确计算的。

构造方法:

package com.qdu.demo;

import java.math.BigDecimal;

public class MyBigDecimalDemo2 {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal(10.0);
        BigDecimal bd2 = new BigDecimal("0.3");

        System.out.println(bd1);
        System.out.println(bd2);
    }
}

package com.qdu.demo;

import java.math.BigDecimal;

public class MyBigDecimalDemo3 {
    public static void main(String[] args) {
        // BigDecimal bd1 = new BigDecimal(0.1);
        // BigDecimal bd2 = new BigDecimal(0.2);

        // 如果想要进行精确运算,请使用字符串的构造
        BigDecimal bd1 = new BigDecimal("0.1");
        BigDecimal bd2 = new BigDecimal("0.2");

        // public BigDecimal add(另一个BigDecimal对象)   加法
        BigDecimal add = bd1.add(bd2);
        System.out.println("和为" + add);

        // public BigDecimal subtract (另一个BigDecimal对象)     减法
        BigDecimal subtract = bd1.subtract(bd2);
        System.out.println("差为" + subtract);

        // public BigDecimal multiply (另一个BigDecimal对象)     乘法
        BigDecimal multiply = bd1.multiply(bd2);
        System.out.println("积为" + multiply);

        // public BigDecimal divide (另一个BigDecimal对象)       除法
        BigDecimal divide = bd1.divide(bd2);
        System.out.println("商为"+divide);
    }
}

四则运算中的除法如果除不尽,请使用 divide 的三个参数的方法:BigDecimal divide = bd1.divide(参与运算的对象, 小数点后精确到多少位, 舍入模式);

  • BigDecimal.ROUND_UP 进一法
  • BigDecimal.ROUND_FLOOR 去尾法
  • BigDecimal.ROUND_HALF_UP 四舍五入
package com.qdu.demo;

import java.math.BigDecimal;

public class MyBigDecimalDemo4 {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("0.3");
        BigDecimal bd2 = new BigDecimal("4");
        BigDecimal divide = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(divide);
    }
}
6.基本数据类型包装类

将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。

下面以 lnteger 为例进行介绍。

package com.qdu.myinteger;

public class MyIntegerDemo2 {
    public static void main(String[] args) {
        // public Integer(int value)    根据int创建Integer对象(过时)
        // public Integer(String s)     根据String值创建Integer对象(过时)
        Integer i1 = new Integer(100);
        Integer i2 = new Integer("100");
        System.out.println(i1);
        System.out.println(i2);

        // public static Integer valueOf(int i)     返回表示指定的int值的Integer实例
        // public static Integer valueOf(String s)      返回一个保存指定值的Integer对象String
        Integer i3 = Integer.valueOf(200);
        Integer i4 = Integer.valueOf("200");
        System.out.println(i3);
        System.out.println(i4);
    }
}

装箱:把基本数据类型转换为对应的包装类类型。

拆箱:把包装类类型转换为对应的基本数据类型。

注意:在使用包装类类型的时候,如果做操作,最好先判断是否为 null。

建议:只要是对象,在使用前就必须进行不为 null 的判断。

package com.qdu.myinteger;

public class MyIntegerDemo3 {
    public static void main(String[] args) {
        // jdk1.5的特性,自动装箱,Java底层会帮我们自动地调用valueOf方法
        Integer i1 = 100;
        System.out.println(i1);

        // jdk1.5的特性,自动拆箱
        int i2 = i1;
        System.out.println(i2);

        Integer i3 = 100;
        i3 += 200;
        System.out.println(i3);

        Integer i4 = null;
        if(i4 != null) {
            i4 += 200;
            System.out.println(i4);
        }
    }
}

基本类型包装类的最常见操作就是:用于基本类型和字符串之间的相互转换。

int 转换为 String:

  1. 加双引号即可。
  2. public static String valueOf(int i):返回 int 参数的字符串表示形式。该方法是 String 类中的方法。

String 转换为 int:

  • public static int parseInt(String s):将字符串解析为 int 类型。该方法是 Integer 类中的方法。
package com.qdu.myinteger;

public class MyIntegerDemo4 {
    public static void main(String[] args) {
        // 字符串+任意的数据类型,结果都是一个字符串
        String s1 = "100";
        int i1 = 200;
        System.out.println(s1 + i1); //100200

        int i2 = Integer.parseInt(s1);
        System.out.println(i2 + i1); // 300

        int i3 = 100;
        String s2 = i3 + "";
        System.out.println(s2 + 100); // 100100

        String s3 = String.valueOf(i3);
        System.out.println(s3 + 100); // 100100
    }
}

示例:有一个字符串 “91 27 46 38 50”,把其中的每一个数存到 int 类型的数组中。

package com.qdu.myinteger;

public class MyIntegerDemo5 {
    public static void main(String[] args) {
        String s = "91 27 46 38 50";
        // 获取字符串中的每一个数字
        String[] strArr = s.split(" ");

        // 创建一个int类型的数组
        int [] numberArr = new int[strArr.length];

        // 把strArr中的数据进行类型转换并存入到int数组中
        for (int i = 0; i < strArr.length; i++) {
            int number = Integer.parseInt(strArr[i]);
            numberArr[i] = number;
        }

        // 遍历int类型的数组
        for (int i = 0; i < numberArr.length; i++) {
            System.out.println(numberArr[i]);
        }
    }
}
7.Arrays

没有构造方法。

所有成员方法都是静态的,可以通过类名直接调用。

package com.qdu.myarrays;

import java.util.Arrays;

public class MyArraysDemo {
    public static void main(String[] args) {
        // public static String toString(int[] a)   返回指定数组的内容的字符串表示形式
        int [] arr = {3, 2, 4, 6, 7};
        System.out.println(Arrays.toString(arr)); // [3, 2, 4, 6, 7]

        // public static void sort(int[] a)     按照数字顺序排列指定的数组
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr)); // [2, 3, 4, 6, 7]

        // public static int binarySearch(int[] a, int key)     利用二分查找返回指定元素的索引
        // 1.数组必须有序
        // 2.如果要查找的元素存在,那么返回的是这个元素实际的索引
        // 3.如果要查找的元素不存在,那么返回的是 -插入点-1
        int [] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int index = Arrays.binarySearch(arr1, 0);
        System.out.println(index); // -1
        index = Arrays.binarySearch(arr1, 11);
        System.out.println(index); // -11
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591164.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号