目录
一,Math(应用)
1、Math类概述
Math 包含执行基本数字运算的方法
2、Math中方法的调用方式
Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用
3、Math类的常用方法
方法名 | 说明 |
public static int abs(int a) | 返回参数的绝对值(absolute) |
public static double ceil(double a) | 返回大于或等于参数的最小double值,等于一个整数,向上取值 |
public static double floor(double a) | 返回小于或等于参数的最大double值,等于一个整数,向下取值 |
public static int round(float a) | 按照四舍五入返回最接近参数的int |
public static int max(int a,int b) | 返回两个int值中的较大值 |
public static int min(int a,int b) | 返回两个int值中的较小值 |
public static double pow (double a,double b) | 返回a的b次幂的值 |
public static double random() | 返回值为double的正值,[0.0,1.0) |
public class Demo {
public static void main(String[] args) {
int max = Math.max(1, 2);
System.out.println(max);
}
}
二,System(应用)
1、System类的常用方法
方法名 | 说明 |
public static void exit(int status) | 终止当前运行的 Java 虚拟机,非零表示异常终止 |
public static long currentTimeMillis() | 返回当前时间(以毫秒为单位) |
public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数) | 将指定源数组中的数组从指定位置复制到目标数组的指定位置 |
package com.itheima01;
import java.util.Arrays;
/*
TODO:
1. 快捷键: ctrl + alt + m(method) 抽取方法
*/
public class Demo02 {
public static void main(String[] args) {
// method();
// System.out.println("yy");
/*
TODO 2. 获取当前时间毫秒值
1). 1秒 = 1000毫秒
2). 时间原点: 1970年1月1日 0时0分0秒
*/
long time = System.currentTimeMillis();
System.out.println(time);//1680830788811 毫秒
int[] original = {10,20,30};
int[] dest = {50,60,70};
/**
* original : 源数组
* index: 源数组的起始索引
* dest : 目标数组
* index2 : 目标数组的起始索引
* length: 复制的长度
*/
System.arraycopy(original,0,dest,0,2);
//底层就是System.arraycopy
// Arrays.copyOf()
System.out.println(Arrays.toString(dest));
}
private static void method() {
boolean result = true;
while(result){
System.out.println("xx");
// break; //中止当前循环
// return; // 中止当前方法
//TODO 1. 终止当前运行的Java虚拟机
System.exit(0); // 0表示正常退出
// System.exit(1); // 非0表示异常退出
}
}
}
示例代码 需求:知道这段代码执行了多少毫秒
//TODO 需求: 知道一段程序运行时间
public class Demo03 {
public static void main(String[] args) {
//1. 目标程序起始时间
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
System.out.println("xx");
}
//2. 目标程序的结束时间
long endTime = System.currentTimeMillis();
//3. 结束时间 - 起始时间
System.out.println("循环打印100000次,耗时:" + (endTime-startTime));
}
}
三,Object类的tostring方法
1、Object类概述
Object 是类层次结构的根,每个类是 Object 作为子类。所有类(包括数组)都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份
2、查看方法源码的方式
选中方法,按下Ctrl + 鼠标左键
3、重写toString方法的方式
1).Alt + Insert 选择toString
2).在类的空白区域,右键 -> Generate -> 选择toString
4、toString方法的作用
以良好的格式,更方便的展示对象中的属性值
示例代码:
/*
TODO: 如果一个类没有显式的继承另外一个类,那么其默认继承Object
面向对象编程 (OOP: Object oriented programming)
万物皆对象
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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;
}
}
/*
TODO Object中的toString方法
1. Object中的toString方法
获取此对象的类型+内存地址的哈希码值
2. 经常在开发测试时:
让我们的自定义类型重写toString方法,
这样此类型对象直接打印,会打印toString返回值
*/
public class Demo02 {
public static void main(String[] args) {
Student s = new Student("张三",18);
/*
1. 现象:
直接打印: 内存地址值
com.itheima02.Student@1b6d3586
2. 源码:
println打印一个Object类型的对象,
底层会默认先调用其toString方法转换成字符串,再打印
println打印一个String类型的对象.
直接打印
3. 需求: 打印内存地址没有意义,想要输出对象中的属性值
*/
System.out.println(s);
String str = s.toString();
System.out.println(str);
}
}
运行结果:
Student{name='张三', age=18}
Student{name='张三', age=18}
四,Object类的equals方法
1、equals方法的作用
1).用于对象之间的比较,返回true和false的结果
2).举例:s1.equals(s2); s1和s2是两个对象
2、重写equals方法的场景
不希望比较对象的地址值,想要结合对象属性进行比较的时候。
3、重写equals方法的方式
1).alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可
2).在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。 示例代码:
import java.util.Objects;
/*
TODO: 如果一个类没有显式的继承另外一个类,那么其默认继承Object
面向对象编程 (OOP: Object oriented programming)
万物皆对象
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
//如果两个变量内存地址一致,必然是同一个对象,return true
if (this == o)
return true;
//如果o是null,或者其类型跟当前类型不一致,直接return false
if (o == null || getClass() != o.getClass())
return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, 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;
}
}
/*
TODO Object中的equals方法
1. Object中的equals方法
底层就是==, 比较的是内存地址值
2. 需求: 比较内存地址没有意义,希望比较对象属性值
3. 实现: 重写equals方法
3. 快捷键: alt + insert -> equals and hashcode
*/
public class Demo03 {
public static void main(String[] args) {
Student s1 = new Student("张三", 18);
Student s2 = new Student("张三", 18);
//引用类型用==比较,比较的是内存地址
System.out.println(s1 == s2);//false
//重写了equals方法,默认比较每个属性值是否一致,如果都一致返回值true
System.out.println(s1.equals(s2));//true
//if语句后续不写{},默认下一行代码就在其代码块中
/* if(1 > 2)
System.out.println("xx");
System.out.println("yy");*/
}
}
面试题:
// 看程序,分析结果
String s = “abc”;
String sb = new String(“abc”);
s1.equals(s2);
s1==s2
public class InterviewTest {
public static void main(String[] args) {
//s1保存的是字符串常量池中常量"abc"的地址
String s1 = "abc";
//s2保存的是堆中对象String的地址
String s2 = new String("abc");
String s3 = "abc";
String s4 = new String("abc");
System.out.println(s1==s2); //false
System.out.println(s1==s3); //true
System.out.println(s2==s4); //false
//因为String重写了equals方法,比较的是字面值
System.out.println(s2.equals(s1)); // true
}
}
/*
TODO
函数式接口: 有且仅有一个抽象方法(需要被重写)的接口
有些抽象不需要被重写
*/
public class Demo {
public static void main(String[] args) {
Animal a = new Dog();
a.equals("xx");
}
}
//接口定义的跟Object类声明一致的方法,都不需要被重写
//接口中其实已经隐式声明了Object类中的所有方法
@FunctionalInterface
interface Animal{
void eat();
// boolean equals(Object obj);//不需要被重写的抽象方法
// String toString();
}
//Dog是Object的子类,从Object继承过来的equals方法,变相重写了Animal的equals方法
class Dog implements Animal{
@Override
public void eat() {
}
}
五,Objects类
1、常用方法
方法名 | 说明 |
public static String toString(对象) | 返回参数中对象的字符串表示形式。 |
public static String toString(对象, 默认字符串) | 返回对象的字符串表示形式。 |
public static Boolean isNull(对象) | 判断对象是否为空 |
public static Boolean nonNull(对象) | 判断对象是否不为空 |
示例代码 :
import java.util.Objects;
public class Demo {
public static void main(String[] args) {
Student s1 = new Student("张三", 18);
String str = s1.toString();
//Student{name='张三', age=18}
System.out.println(str);
//返回参数中对象的字符串表示形式
String str2 = Objects.toString(s1);
//Student{name='张三', age=18}
System.out.println(str2);
Student s2 = null;
//如果第一个参数为null,那么将返回第二参数(默认值)
String str3 = Objects.toString(s2, "默认值");
//默认值
System.out.println(str3);
//判断对象是否为空
boolean result1 = Objects.isNull(s1);
System.out.println(result1); //false
//判断对象是否不为空
boolean result2 = Objects.nonNull(s1);
System.out.println(result2);//true
}
}
六,BigDecimal
1、作用
可以用来进行精确计算
2、构造方法
方法名 | 说明 |
BigDecimal(double val) | 参数为double |
BigDecimal(String val) | 参数为String |
3、常用方法
方法名 | 说明 |
public BigDecimal add(另一个BigDecimal对象) | 加法 |
public BigDecimal subtract (另一个BigDecimal对象) | 减法 |
public BigDecimal multiply (另一个BigDecimal对象) | 乘法 |
public BigDecimal divide (另一个BigDecimal对象) | 除法 |
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) | 除法 |
4、总结
1).BigDecimal是用来进行精确计算的
2).创建BigDecimal的对象,构造方法使用参数类型为字符串的。
3).四则运算中的除法,如果除不尽请使用divide的三个参数的方法。
代码示例:
BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);
参数1 ,表示参与运算的BigDecimal 对象。
参数2 ,表示小数点后面精确到多少位
参数3 ,舍入模式
RoundingMode.CEILING 向上取整
RoundingMode.FLOOR 向下取整
RoundingMode.HALF_UP 四舍五入
public class Demo {
public static void main(String[] args) {
double d1 = 0.1;
double d2 = 0.09;
/*
int(4个字节) < long(8个) < float(4) < double(8)
-2147483648~2147483647
- 2(31次) ~ 2(31次) - 1
1. 整型: 取值范围本质遍历字节所有的取值方式
2. 浮点型: 以一部分字节作为底数,另一部分作为指数
问题:
1. 这样浮点型可以用少的字节数,表示更大的数
2. 一些正常计算有可能精度丢失
*/
System.out.println(d1 - d2);//0.010000000000000009
}
}
import java.math.BigDecimal;
import java.math.RoundingMode;
/*
TODO: BigDecimal
1. 作用: 可以用来进行精确计算,替代double
*/
public class Demo02 {
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal("0.1");
BigDecimal b2 = new BigDecimal("0.09");
//减
BigDecimal result = b1.subtract(b2);
System.out.println(result);//0.01
//加
BigDecimal result2 = b1.add(b2);
System.out.println(result2); //0.19
//乘
BigDecimal result3 = b1.multiply(b2);
System.out.println(result3);//0.009
//除
BigDecimal divide = b2.divide(b1);
System.out.println(divide);//0.9
//如果除不尽,就会报错 ArithmeticException 算术异常
// BigDecimal result4 = b1.divide(b2);
BigDecimal result4 = b1.divide(b2,2, RoundingMode.FLOOR);
System.out.println(result4);
}
}