day_13:Math类、Random类、System类、Runtime类、BigInteger、BigDecimal
Math类
Math常用的方法
public class Test01 { public static void main(String[] args) { /** * Math类:数学类 */ double pow = Math.pow(3, 2);//3的2次方 double sqrt = Math.sqrt(9);//9的平方根 //天花板:向上取整 double ceil = Math.ceil(1.00001); //地板:向下取整 double floor = Math.floor(1.99999); //四舍五入 long round = Math.round(1.4); int max = Math.max(10, 20);//获取最大值 int min = Math.min(10, 20);//获取最小值 double random = Math.random();//随机数:0(包含)到 1(不包含) int abs = Math.abs(-10);//取绝对值 System.out.println(abs); } }
练习:随机出0-99数字、随机出0-33数字、abs方法有没有可能返回负数
public class Test02 { public static void main(String[] args) { //随机出0-99数字 // int random = (int)(Math.random()*100); // System.out.println(random); //随机出0-33数字 // int random = (int)(Math.random()*100)%34; // System.out.println(random); //abs这个方法有没有可能返回负数 int abs =Math.abs(Integer.MAX_VALUE + 1); System.out.println(abs); } }
静态导入:
把该类的所有静态属性及静态方法都导入到本类来,成为本类的静态属性及静态方法
建议:不要使用,在本类里有和静态导入的类的方法重名时,会调用本来原有的方
import static java.lang.Math.*; public class Test03 { public static void main(String[] args) { /** * 静态导入:把该类的所有静态属性及静态方法都导入到本类来,成为本类的静态属性及静态方法 * 建议:不要使用,在本类里有和静态导入的类的方法重名时,会调用本来原有的方法 */ int abs = abs(-100); System.out.println(abs); System.out.println(PI); } public static int abs(int a){ return 10000; } }
Random类
此类用于生成随机数:
Random(); 创建一个新的随机数生成器
Random(long seed);种子数\
常用方法
public class Test01 { public static void main(String[] args) { Random ran = new Random(); int nextInt1 = ran.nextInt(); int nextInt2 = ran.nextInt(10);//0-9 System.out.println(nextInt2); } }
种子数
public class Test02 { public static void main(String[] args) { //种子数一旦固定,随机出的数字也是规定的 Random random = new Random(10); int nextInt = random.nextInt(); System.out.println(nextInt); } //随机出数字的方法 public static void randomMethod(){ int num = (int) ((10+20) ^ System.nanoTime()); System.out.println(num); } }
随机出7到81的数字
public class Test03 { public static void main(String[] args) { //随机出 7到81 int ranNum = ranNum(7, 81); System.out.println(ranNum); } public static int ranNum(int start,int end){ Random ran = new Random(); return ran.nextInt(end-start+1)+start; // 0 ~ 74 } }
System类
输入流:系统标准的输入流 控制台--》程序
public class Test01 { public static void main(String[] args) { /** * System.in(InputStream(输入流)):系统的标准输入流,方向:控制台->程序 */ InputStream in = System.in; Scanner scan = new Scanner(in); String next = scan.next(); } }
输出流:系统标准的输出流、系统标准错误输出流 程序--》控制台
public class Test02 { public static void main(String[] args) { /** * System.out(PrintStream打印流):系统标准的输出流,方向:程序->控制台 * System.err(PrintStream打印流):系统标准错误输出流,方向:程序->控制台 */ //打印流 // PrintStream ps = System.out; // ps.println("xxx"); //打印流 PrintStream ps = System.err; ps.println("xxx"); /** * System.in:系统标准输入流 方向:控制台->程序 * System.out:系统标准输出流 方向:程序->控制台 * System.err:系统标准错误输出流 方向:程序->控制台 * * 重定向:重新给系统标准输入输出流定义方向 */ } }
多线程问题:标准输出流、标准错误输出流
输出时既要输出内容还要输出换行,所以输出的结果有很多种情况,但是小永永远都会在小凡前面输出
public class Test03 { public static void main(String[] args) { System.out.println("小永"); System.err.println("小红"); System.out.println("小凡"); } }
键值对
public class Test04 { public static void main(String[] args) { //键值对 //获得到当前系统的所有信息 Properties properties = System.getProperties(); String property = System.getProperty("sun.jnu.encoding", "xxx"); System.out.println(property); } }
Runtime类
Runtime代表Java程序的运行时环境,可以通过 getRuntime 方法获取当前运行时。
应用程序不能自己创建Runtime对象,可以通过Runtime的静态方法getRuntime()获得Runtime对象。
Runtime类可以访问jvm的相关信息,如处理器数量,内存信息等
字符串用+连接和使用StringBuilder的append()方法消耗时长和内存的比较
public class Test02 { public static void main(String[] args) { testCode(new I1() { @Override public void code() { //消耗时长:531 消耗内存:2709632 // String str = ""; // for (int i = 0; i < 10000; i++) { // str += "夏凡感冒了..."; // } //消耗时长:2 消耗内存:552992 StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000; i++) { sb = sb.append("夏凡感冒了..."); } } }); } public static void testCode(I1 i1){ Runtime runtime = Runtime.getRuntime(); long startTime = System.currentTimeMillis(); long startMemory = runtime.freeMemory(); i1.code(); long endMemory = runtime.freeMemory(); long endTime = System.currentTimeMillis(); System.out.println("消耗时长:" + (endTime-startTime)); System.out.println("消耗内存:" + (startMemory - endMemory)); } }
BigInteger类和BigDecimal
BigInteger: 能表示比Integer更大的数字
BigDecimal:浮点数的计算
BigInteger的应用
public class Test01 { public static void main(String[] args) { //整数类型的大数值运算类 BigInteger b1 = new BigInteger("1200000000000000000000000"); BigInteger b2 = new BigInteger("1200000000000000000000000"); BigInteger b3 = b1.add(b2); System.out.println(b3); } }
BigDecimal的应用
public class Test03 { public static void main(String[] args) { BigDecimal b1 = new BigDecimal("10"); BigDecimal b2 = new BigDecimal("3"); BigDecimal b3 = b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP); System.out.println(b3); } }
自己写BigInteger类add方法的底层
package com.dream.big_class; public class MyBigInteger { private String val; public MyBigInteger(String val) { this.val = val; } public MyBigInteger add(MyBigInteger m2){ char[] charArray1 = this.val.toCharArray(); char[] charArray2 = m2.val.toCharArray(); int[] arr1 = method(charArray1); int[] arr2 = method(charArray2); int index1 = arr1.length-1; int index2 = arr2.length-1; int num = 0;//进位值 StringBuilder sb = new StringBuilder(); while(index1 != -1 || index2 != -1){ int num1 = 0; int num2 = 0; if(index1 != -1){ num1 = arr1[index1--]; } if(index2 != -1){ num2 = arr2[index2--]; } int result = (num1+num2+num)%10;//获得当前位数的值 //判断是否要进位 if(num1+num2+num >= 10){ num = 1; }else{ num = 0; } sb = sb.append(result); } if(num ==1){ sb.append(num); } sb.reverse(); return new MyBigInteger(sb.toString()); } public int[] method(char[] cs){ int[] is = new int[cs.length]; for (int i = 0; i < cs.length; i++) { //char -> String -> Integer -自动拆箱-> intsssss int value = Integer.valueOf(String.valueOf(cs[i])); is[i] = value; } return is; } @Override public String toString() { return val; } }
public class Test04 { public static void main(String[] args) { MyBigInteger m1 = new MyBigInteger("12345678987654321"); MyBigInteger m2 = new MyBigInteger("12344321123456"); MyBigInteger m3 = m1.add(m2); System.out.println(m3); } }

浙公网安备 33010602011771号