matchAPI
Math 构造私有不能创建对象,里面都是静态成员.
abs绝对值
ceil向上取整
floor向下取整
round四舍五入
max返回最大数🌳
min返回最小数📕
pow(a,b)a的B次幂
random 随机数 [0.0,1.0)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.yang.API.Math; public class TestMath { public static void main(String[] args) { // 输出绝对值 System.out.println(Math.abs(- 10 )); // 向上取整 System.out.println(Math.ceil( 10.2 )); // 向下取整 System.out.println(Math.floor( 9.8 )); // 取最大值 System.out.println(Math.max( 2 , 3 )); // 取最小值 System.out.println(Math.min( 4 , 5 )); // 四舍五入 System.out.println(Math.round( 7.7 )); System.out.println(Math.round( 6.2 )); // a的b次幂 System.out.println(Math.pow( 8 , 1000000 )); // 随机数 for ( int i= 0 ;i< 10 ;i++) { System.out.println(Math.random()); } } } |
System 不能创建对象,私有构造,静态成员
exit 退出Java虚拟机
currentTimeMillis 返回时间
arrycopy (复制数组名 ,起始索引,目的地址,起始索引,拷贝个数) 数组复制
Object类
打印重写toString,重写前打印对象内存地址使用的式Object类中的toString,重写后打印对象成员变量值
equals比较对象是否相等,默认比较地址值,想比较值重写equals方法
Object中equals使用==号比较地址值
1 2 3 4 5 6 7 8 9 | package com.yang.API.Object; public class MyEquals { public static void main(String[] args) { Student student1= new Student( "adb" , 12 ); Student student2= new Student( "abc" , 12 ); System.out.println(student1.equals(student2)); } } |
Student的祖父类Object类默认equals 不重写结果为false
1 2 3 | public boolean equals(Object obj) { return ( this == obj); } |
重写后可以比较内容同一类对象内容相同返回true
1 2 3 4 5 6 7 8 9 10 11 12 | 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 ; } |
BigDecimal进行精确计算 构造方法可为数字 和字符串 建议使用字符串(字符串数字)进行计算,防止精度的丢失。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.yang.API.BigDecimal; import java.math.BigDecimal; public class MyDecimal { public static void main(String[] args) { BigDecimal a= new BigDecimal( 0 ); BigDecimal a1= new BigDecimal( "1.56" ); BigDecimal a2= new BigDecimal( "1.56" ); BigDecimal add=a1.add(a2); System.out.println( "和" +add); BigDecimal mutiply=a1.multiply(a2); System.out.println( "乘积" +mutiply); BigDecimal divide=a1.divide(a2); System.out.println( "商" +divide); BigDecimal subtract=a1.subtract(a2); System.out.println( "差" +subtract); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.yang.API.BigDecimal; import java.math.BigDecimal; import java.math.RoundingMode; public class MyDecimal { public static void main(String[] args) { BigDecimal a= new BigDecimal( 0 ); BigDecimal a1= new BigDecimal( "1.55" ); BigDecimal a2= new BigDecimal( "1.56" ); BigDecimal add=a1.add(a2); System.out.println( "和" +add); BigDecimal mutiply=a1.multiply(a2); System.out.println( "乘积" +mutiply); // BigDecimal divide=a1.divide(a2); // System.out.println("商"+divide); BigDecimal subtract=a1.subtract(a2); System.out.println( "差" +subtract); // BigDecimal.ROUND_UP已过时 @Deprecated System.out.println( "进一法" +a1.divide(a2, 5 , RoundingMode.UP)); // BigDecimal.ROUND_FLOOR @Deprecated System.out.println( "去尾法" +a1.divide(a2, 5 , RoundingMode.FLOOR)); // BifDecimal.HALF_UP @Deprecated System.out.println( "四舍五入" +a1.divide(a2, 5 , RoundingMode.HALF_UP)); } } |
byte Byte
short Short
引用
int Integer ArryList<Integer>
long Long
folat Float
double Double
boolear Boolear
char Character
装箱 把一个基本数据类型,变成对应的包装类
自动 java底层会帮我们自动调用valueof()方法
Integer i=3;自动装箱
int i1=i; 自动拆箱
Integer i=null;
i+=4;报错 null可以赋值给包装类(引用)
但是空不能赋值给基本数据类型
Integer和Integer使用==号比较的是内存地址 一个字节的整数被缓存到IntegerCache中
所以两个 Integer 128返回false 127返回true
//除Character以外,其余包装类,都有一个静态方法:parseXxx()将字符串数据转换程基本类型的数据
parseInt(String Str)
String s1="100";
int num =Integer.parseInt(s1);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.yang.API.ParseXxx; import java.util.Scanner; public class MyParseTest { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); System.out.println( "请输入学生的信息:姓名,年龄,分数" ); String str=scanner.next(); String[] arry=str.split( "," ); Student student= new Student(arry[ 0 ],Integer.parseInt(arry[ 1 ]),Double.parseDouble(arry[ 2 ])); System.out.println(student.toString()); } } |