Math
功能:复杂的数学运算
Random
功能:用于产生随机数
注意:固定种子 生成随机数的序列是一样的(序列中的数字是不一样的)
package test;
public class test8 {
public static void main(String[] args) {
System.out.println("计算绝对值:" + Math.abs(-1));
System.out.println("求大于参数的最小整数:" + Math.ceil(5.6));
System.out.println("求小于参数的最大整数:" + Math.floor(-4.2));
System.out.println("对小数进行四舍五入的结果:" + Math.round(-4.6));
System.out.println("求两个数的较大值:" + Math.max(2.1, -2.1));
System.out.println("求两个数的较小值:" + Math.min(2.1, -2.1));
System.out.println("生成大于等于0.0小于1.0的随机值:" + Math.random());
}
}
package test;
import java.util.Random;
public class test9 {
public static void main(String[] args) {
Random random1 = new Random();
System.out.println("无参构造函数输出:");
for (int i = 0; i < 10; i++) {
System.out.println(random1.nextInt(100));
}
//
Random random2 = new Random(21);
System.out.println("有参构造函数输出:");
for (int i = 0; i < 10; i++) {
System.out.println(random2.nextInt(100));
}
}
}
package test;
import java.util.Random;
public class test10 {
public static void main(String[] args) {
Random random = new Random(99);
System.out.println("产生float类型随机数:" + random.nextFloat());
System.out.println("产生double类型随机数:" + random.nextDouble());
System.out.println("产生int类型随机数:" + random.nextInt());
System.out.println("产生int类型随机数:" + random.nextInt(100));
}
}
包装类
定义:和基本数据类型对应的Class
功能:让一切皆对象的编程思想进一步得到体现
相互转换:
装箱
拆箱
public class IntExam {
public static void main(String[] args) {
//装箱
int num1=10;
Integer num2=new Integer(num1);
System.out.println(num2);
//拆箱
int num3=num2.intValue();
System.out.println(num3);
}
}
public class IntFuncsExam {
public static void main(String[] args) {
System.out.println("二进制显示8:"+Integer.toBinaryString(123));
System.out.println("十六进制显示123:"+Integer.toHexString(123));
System.out.println("八进制显示123:"+Integer.toOctalString(123));
System.out.println(Double.valueOf("12.99") instanceof Double);
}
}