资讯动态

常见API

发布时间:2026/10/18 13:49:40 来源:尧图企业网站定制
一、Mathjava.lang.Math是final类方法全是static直接用Math.xxx()调用。常用方法分类方法说明常量PI、E圆周率、自然常数最值/绝对值abs、max、min绝对值、最大、最小取整ceil、floor、round、rint向上、向下、四舍五入、最近偶数幂/根pow、sqrt、cbrt、hypot次方、平方根、立方根、斜边指数/对数exp、log、log10、log1pe^x、ln、log10、ln(1x)三角函数sin/cos/tan、asin/acos/atan、atan2参数是弧度角度弧度toRadians、toDegrees角度转弧度、弧度转角度随机数random()返回[0.0, 1.0)精确运算addExact、multiplyExact等溢出抛ArithmeticException取模floorMod负数取模更符合数学注意点三角函数用弧度不是角度。Math.sin(Math.toRadians(30))sqrt(-1)、log(-1)返回NaN不是抛异常。round返回类型Math.round(double)→longMath.round(float)→intMath.round(-3.5)结果是-3不是-4。Math.random()范围是[0, 1)不是[0, 1]。生成[a,b]整数a (int)(Math.random() * (b - a 1))浮点数不要用比较。用误差Math.abs(a - b) 1e-6。金额计算用BigDecimal。整数除法会截断4 / 3 1要写4.0 / 3。Math.abs(Integer.MIN_VALUE)仍是负数因为溢出。Math.pow和Math.sqrt的返回值都是double。如果你要用变量接收就必须用double类型。大整数/高精度用BigInteger、BigDecimal不要硬用Math。题目一判断一个数是否为一个质数质数是大于 1 且只能被 1 和它本身整除的自然数。方法一inum(朴素遍历法)从 2 试除到 number-1package com.example; import java.util.Scanner; public class Demo{ public static void main(String[] args) { Scanner snew Scanner(System.in); System.out.print(请输入要验证的整数); int nums.nextInt(); if(isPrime(num)){ System.out.println(num 是素数); }else{ System.out.println(num 不是素数); } } public static boolean isPrime(int num){ if(num2){ return false; } int count0; for (int i 2; i num; i) { count; if(num%i0){ System.out.println(循环次数count); return false; } } System.out.println(循环次数count); return true; } }运行示例请输入要验证的整数97 循环次数95 97是素数方法二iMath.sqrt(num)优化法因数总是成对出现一个小于 √n、一个大于 √n只需试除到 √numberpackage com.example; import java.util.Scanner; public class Demo{ public static void main(String[] args) { Scanner snew Scanner(System.in); System.out.print(请输入要验证的整数); int nums.nextInt(); if(isPrime(num)){ System.out.println(num 是素数); }else{ System.out.println(num 不是素数); } } public static boolean isPrime(int num){ if(num2){ return false; } int count0; for (int i 2; i Math.sqrt(num); i) { count; if(num%i0){ System.out.println(循环次数count); return false; } } System.out.println(循环次数count); return true; } }运行示例请输入要验证的整数97 循环次数8 97是素数题目二自幂数自幂数一个n位自然数等于自身各个数位上数字的n次幂之和举例1三位数 1^3 5^3 3^3 153举例2四位数 1^4 6^4 3^4 4^4 1634一位自幂数独身数三位自幂数水仙花数 四位自幂数四叶玫瑰数五位自幂数五角星数 六位自幂数六合数七位自幂数北斗七星数 八位自幂数八仙数九位自幂数九九重阳数 十位自幂数十全十美数要求1统计一共有多少个水仙花数。package com.example; public class Demo { public static void main(String[] args) { int count0; System.out.print(满足条件的有); for (int i100;i999;i){ int gei%10; int shii/10%10; int baii/100%10; double sumMath.pow(ge,3)Math.pow(shi,3)Math.pow(bai,3); if(sumi){ count; System.out.print(i ); } } System.out.println(\n一共有count个水仙花数); } }运行结果满足条件的有153 370 371 407 一共有4个水仙花数要求2证明没有两位的自幂数。package com.example; public class Demo { public static void main(String[] args) { if (istwo()){ System.out.println(有两位的自幂数); }else{ System.out.println(没有两位的自幂数); } } private static boolean istwo() { for(int i10;i99;i){ int gei%10; int shii/10%10; double sum Math.pow(ge, 2) Math.pow(shi, 2); if(sumi){ return true; } } return false; } }运行结果没有两位的自幂数要求3分别统计有多少个四叶玫瑰数和五角星数。package com.example; public class Demo { public static int countRose(){ int count0; System.out.print(四叶玫瑰数有); for (int i 1000; i 9999; i) { int gei%10; int shii/10%10; int baii/100%10; int qiani/1000%10; double sumMath.pow(ge,4)Math.pow(shi,4)Math.pow(bai,4)Math.pow(qian,4); if(sumi){ count; System.out.print(i ); } } return count; } public static int countStar(){ int count0; System.out.print(五角星数有); for (int i 10000; i 99999; i) { int gei%10; int shii/10%10; int baii/100%10; int qiani/1000%10; int wani/10000%10; double sumMath.pow(ge,5)Math.pow(shi,5)Math.pow(bai,5)Math.pow(qian,5)Math.pow(wan,5); if(sumi){ count; System.out.print(i ); } } return count; } public static void main(String[] args) { System.out.println(\n一共有countRose()个); System.out.println(\n一共有countStar()个); } }运行结果四叶玫瑰数有1634 8208 9474 一共有3个 五角星数有54748 92727 93084 一共有3个二、SystemSystem也是一个工具类提供了一些与系统相关的方法方法名说明public static void exit(int status)终止当前运行的 Java 虚拟机public static long currentTimeMillis()返回当前系统的时间毫秒值形式public static void arraycopy(数据源数组起始索引目的地数组起始索引拷贝个数)数组拷贝1.System.exit(int status)—— 终止虚拟机package com.example; public class Demo { public static void main(String[] args) { System.out.println(程序开始); System.exit(0); System.out.println(这句不会执行); } }说明调用后JVM 立刻结束后面的代码一句都不会再执行。参数0表示正常退出非0比如1表示异常退出。一般用于程序检测到严重错误、需要强制停止时。一般开发中不建议频繁使用因为它会直接杀掉整个程序连 finally 块都不会执行。2.System.currentTimeMillis()—— 获取当前时间毫秒值package com.example; public class Demo { public static void main(String[] args) { //1.获取当前时间的毫秒值 long timeSystem.currentTimeMillis(); System.out.println(当前毫秒值 time); //2.常用于计算代码运行时间 long startSystem.currentTimeMillis(); int sum0; for(int i0;i1000000;i){ sumi; } long endSystem.currentTimeMillis(); System.out.println(循环执行耗时 (end - start) 毫秒); } }说明返回的是从1970 年 1 月 1 日 00:00:00UTC到现在的毫秒数。常用于计算程序运行时间用结束时间减开始时间。返回值是long类型不要用int接收会溢出。它记录的是毫秒如果要更精确的纳秒用System.nanoTime()。计算耗时很适合但不要用它当日期格式化工具要转成日期还得配合Date或SimpleDateFormat。3.System.arraycopy(...)—— 数组拷贝参数含义arraycopy(源数组, 源数组起始索引, 目标数组, 目标数组起始索引, 拷贝个数)全部拷贝示例把 src 中从索引 0 开始的 5 个元素拷贝到 dest 索引 0 开始的位置package com.example; import java.util.Arrays; public class Demo { public static void main(String[] args) { int[] src{1,2,3,4,5}; //原数组 int[] destnew int[5]; //目标数组 //进行拷贝 System.arraycopy(src,0,dest,0,5); //打印目标数组 System.out.println(Arrays.toString(dest)); } }输出结果[1, 2, 3, 4, 5]部分拷贝示例只拷贝 src 中的 3 个元素 2,3,4放到 dest 从索引 2 开始的位置package com.example; import java.util.Arrays; public class Demo { public static void main(String[] args) { int[] src{1,2,3,4,5}; int[] dest{0,0,0,0,0}; System.arraycopy(src,1,dest,2,3); System.out.println(Arrays.toString(dest)); } }输出结果[0, 0, 2, 3, 4]注意点索引越界会报错ArrayIndexOutOfBoundsException要求源起始索引 拷贝个数 源数组长度目标起始索引 拷贝个数 目标数组长度。类型必须兼容源数组和目标数组类型要一致或能自动转换否则报ArrayStoreException。数组长度不够时可以用Arrays.copyOf()更方便会自动创建新数组。底层是native 方法C/C 实现比 for 循环一个一个赋值快很多。知识点一如果数据源数组和目的地数组都是引用数据类型那么子类类型可以赋值给父类类型。arraycopy 拷贝的是引用。如果源数组里放的是子类对象目标数组是父类类型那么子类对象赋给父类引用是合法的多态所以允许拷贝。package com.example; class Person{ String name; public Person(String name) { this.name name; } } class Student extends Person{ public Student(String name) { super(name); } } public class Demo { public static void main(String[] args) { // 源数组学生类型子类 Student[] src { new Student(张三), new Student(李四) }; // 目标数组人类型父类 Person[] dest new Person[2]; // 把 Student 拷到 Person 数组里 —— 合法 System.arraycopy(src, 0, dest, 0, 2); System.out.println(dest[0].name); // 张三 System.out.println(dest[1].name); // 李四 } }知识点2浅拷贝 只复制地址不复制地址指向的对象本身。本质上浅拷贝这个概念只对引用类型有意义。基本类型变量里存的就是值本身复制过去就是一份完整独立的值没有浅和深之分。引用类型变量里存的是地址。复制地址 浅拷贝把地址指向的对象也复制一份 深拷贝。arraycopy属于浅拷贝两个数组指向的是同一个对象改一个另一个也变。class Person { String name; Person(String name) { this.name name; } } public class Demo { public static void main(String[] args) { Person[] src { new Person(张三) }; Person[] dest new Person[1]; System.arraycopy(src, 0, dest, 0, 1); // 修改 dest 里对象的属性 dest[0].name 李四; // 看 src 里那个对象 System.out.println(src[0].name); // 输出李四 System.out.println(src[0] dest[0]); // 输出true } }三、Runtime方法名说明public static Runtime getRuntime()当前系统的运行环境对象public void exit(int status)停止虚拟机public int availableProcessors()获得CPU的线程数public long maxMemory()JVM能从系统中获取总内存大小单位bytepublic long totalMemory()JVM已经从系统中获取总内存大小单位bytepublic long freeMemory()JVM剩余内存大小单位bytepublic Process exec(String command)运行cmd命令1.getRuntime()—— 获取 Runtime 对象package com.example; public class Demo { public static void main(String[] args) { Runtime rRuntime.getRuntime(); System.out.println(r); } }运行结果java.lang.Runtimeb4c966a说明Runtime 是单例的一个 JVM 里只有一个 Runtime 对象所以不能new Runtime()只能用getRuntime()拿。后面的exit、内存方法、exec都要先拿到这个对象才能调用。Runtime 的构造方法是私有的直接new Runtime()会报错。每次调用getRuntime()返回的都是同一个对象。2. exit(int status) —— 停止虚拟机package com.example; public class Demo { public static void main(String[] args) { Runtime rRuntime.getRuntime(); System.out.println(开始); r.exit(0); System.out.println(这句不会执行); } }运行结果开始说明和System.exit(0)是同一个东西本质上System.exit内部就是调用了Runtime.getRuntime().exit(status)。参数0正常退出非0异常退出。调用后 JVM直接终止后面的代码不执行finally也不执行。实际开发里一般用System.exit()就够了rt.exit()少见但原理相同。3.availableProcessors()—— 获得 CPU 线程数package com.example; public class Demo { public static void main(String[] args) { Runtime rRuntime.getRuntime(); int nr.availableProcessors(); System.out.println(CPU核心线程数 n); } }运行结果CPU核心线程数16说明返回当前机器可用的 CPU 核心线程数常用于创建线程池时决定线程数比如核心数 CPU 数或CPU 数 * 2。返回的是逻辑处理器数量不是物理核数超线程下可能翻倍。这个值受机器性能、系统设置影响不同电脑结果不一样。4.maxMemory()—— JVM 最大可用内存package com.example; public class Demo { public static void main(String[] args) { Runtime rRuntime.getRuntime(); long l r.maxMemory(); System.out.println(JVM最大可用内存 l/1024/1024); } }运行结果JVM最大可用内存4046说明表示 JVM 最多能向系统申请多少内存是一个理论上限。单位是byte一般除以1024 * 1024换成 MB 更好看。默认值跟电脑内存有关一般是物理内存的 1/4 左右可通过-Xmx参数手动设置。5.totalMemory()—— JVM 已获取内存package com.example; public class Demo { public static void main(String[] args) { Runtime rRuntime.getRuntime(); long totalr.totalMemory(); System.out.println(JVM已获取内存 total/1024/1024MB); } }运行结果JVM已获取内存254MB说明表示 JVM当前已经从系统手里拿到的内存总量不是最大上限。这个值会动态变化JVM 内存不够时会向系统申请更多内存但不会超过maxMemory。区分maxMemory是能拿到多少totalMemory是已经拿到多少。6.freeMemory()—— JVM 剩余内存package com.example; public class Demo { public static void main(String[] args) { Runtime rRuntime.getRuntime(); long free r.freeMemory(); System.out.println(JVM剩余内存 free /1024/1024); } }运行结果JVM剩余内存249说明表示totalMemory里还没被用掉的部分。只在totalMemory范围内算不代表 JVM 真能用的全部空间还没向系统要的那部分不算。真正的 JVM 空闲总量 ≈freeMemory (maxMemory - totalMemory)。7.exec(String command)—— 运行 cmd 命令package com.example; import java.io.IOException; public class Demo { public static void main(String[] args) throws IOException { Runtime rRuntime.getRuntime(); r.exec(noted); } }常用命令示例r.exec(shutdown -s -t 60); // 60 秒后关机 r.exec(shutdown -a); // 取消关机说明可以在 Java 程序里直接执行 CMD 命令比如打开记事本、定时关机等。exec会抛IOException必须处理throws或try-catch。命令是按字符串原样执行的路径有空格要注意可用exec(String[] cmdArray)传数组。谨慎使用关机类命令测试时自己都可能被关掉 。四、Object1. public Object() —— 空参构造package com.example; class Person { String name; // 子类构造方法 public Person() { // 这里就算不写编译器也会自动加 super(); // super(); // 调用 Object 的空参构造 } } public class Demo{ public static void main(String[] args) { Person p new Person(); System.out.println(创建成功 p); } }运行结果创建成功com.example.Person2f4d3709说明Object 是所有类的老祖宗你写的每个类都直接或间接继承它。子类构造方法的第一行默认就是super()也就是调用 Object 的空参构造。Object只有空参构造没有有参构造。类不写extends时默认继承 Object。子类构造里super()可以省略编译器自动加但写了就必须在第一行。2.public String toString()—— 返回对象的字符串表示package com.example; class Person { String name; public Person(String name) { this.name name; } // 重写 toString返回想看的内容 Override public String toString() { return Person{name name }; } } public class Demo { public static void main(String[] args) { Person p new Person(张三); System.out.println(p); } }运行结果Person{name张三}说明不重写的话默认实现返回的是全类名哈希码的十六进制看不出对象内容。重写后可以返回想看的信息打印对象更直观。System.out.println(对象)会自动调用它的toString()。IDEA 可以用Alt Insert→toString()自动生成。3.public boolean equals(Object obj)—— 比较两个对象是否相等package com.example; class Student { String name; int age; public Student(String name, int age) { this.name name; this.age age; } // 重写 equals按内容比较 Override public boolean equals(Object obj) { if (this obj) return true; // 同一个对象 if (obj null || getClass() ! obj.getClass()) return false; Student other (Student) obj; return age other.age name.equals(other.name); } } public class Demo { public static void main(String[] args) { Student s1 new Student(张三, 18); Student s2 new Student(张三, 18); System.out.println(s1 s2); // false比较地址 System.out.println(s1.equals(s2)); // true重写后比较内容 } }运行结果false true说明Object 的equals默认就是比较的是地址两个不同对象永远false。想按内容相同判断相等就得重写它。比地址equals默认也比地址重写后才比内容。重写时建议先用getClass()或instanceof判断类型再强转。重写equals最好同时重写hashCode()如放进 HashSet/HashMap 时要用。String、Integer 等类官方已经重写好了直接比就行。4. protected Object clone() —— 对象克隆1浅拷贝package com.example; import java.util.Arrays; class Student implements Cloneable{ String name; int age; int[] scores; public Student(String name,int age,int[] scores){ this.namename; this.ageage; this.scoresscores; } Override public String toString() { return Student{ name name \ , age age , scores Arrays.toString(scores) }; } Override public Student clone() throws CloneNotSupportedException { return (Student)super.clone(); } } public class Demo{ public static void main(String[] args) throws CloneNotSupportedException { Student s1new Student(张三,23,new int[]{90,80,70}); Student s2s1.clone(); //1.两个是不同对象 System.out.println(s1 s2); //2.但scores是同一个数组(地址一样) System.out.println(s1.scores); System.out.println(s2.scores); System.out.println(s1.scores s2.scores); //3.改s2的分数s1也变 s2.scores[0]100; System.out.println(s1.scores[0]); } }运行结果false [I2f4d3709 [I2f4d3709 true 100说明这是 Objectclone()的默认行为什么都不用改就是浅拷贝。String 是不可变类所以 String 成员不用担这个问题改 String 会新建对象不影响原对象。int age 存的是值本身super.clone() 时直接把这个值复制一份所以两边互不影响。super.clone()只复制了对象本身这一层对象里面的引用类型成员scores还是同一个地址。所以改s2.scores里的值s1.scores也会变。基本类型int、double、boolean 等和 String 这类不可变类浅拷贝就足够独立了不用特殊处理。浅拷贝要担心的只有可变引用成员数组、自定义对象、集合等。2深拷贝package com.example; import java.util.Arrays; class Student implements Cloneable{ String name; int age; int[] scores; public Student(String name,int age,int[] scores){ this.namename; this.ageage; this.scoresscores; } Override public Student clone() throws CloneNotSupportedException { //1.先浅拷贝一层 Student copy(Student)super.clone(); //2.再把引用成员也复制一份 copy.scoresthis.scores.clone(); return copy; } } public class Demo{ public static void main(String[] args) throws CloneNotSupportedException { Student s1new Student(张三,23,new int[]{90,80,70}); Student s2s1.clone(); //1.两个是不同对象 System.out.println(s1 s2); //2.但scores是同一个数组(地址一样) System.out.println(s1.scores s2.scores); //3.改s2的分数s1也变 s2.scores[0]100; System.out.println(s1.scores[0]); } }运行结果false false 90说明深拷贝就是把引用成员也复制一份两个对象彻底独立。做法先super.clone()浅拷贝出对象再手动把引用成员.clone()或重新 new 一个。深拷贝不是 Object 自动帮你做的必须自己写。如果引用成员是自定义对象那个对象也得实现Cloneable并重写clone()然后在这里调它的clone()。如果成员是多层嵌套对象里套对象里套对象每一层都得处理比较麻烦。实际项目里深拷贝常用序列化实现ObjectOutputStream写进内存再读出来或者用工具类如 Hutool 的BeanUtil.copyProperties、Spring 的BeanUtils但它们是浅拷贝注意区别。五、ObjectsObjects 是 java.util.Objects是一个工具类方法都是 static直接用 Objects.xxx() 调用不用 new。1.public static boolean equals(Object a, Object b)—— 安全比较两个对象import java.util.Objects; public class Demo1 { public static void main(String[] args) { String a hello; String b hello; String c null; // 1. 普通情况比较两个非空对象 System.out.println(Objects.equals(a, b)); // true // 2. 关键当 a 是 null 时不会报错 System.out.println(Objects.equals(c, a)); // false System.out.println(Objects.equals(c, null));// true // 3. 对比直接用 a.equals(b) 就会有风险 // System.out.println(c.equals(a)); // 报 NullPointerException } }说明内部逻辑大致是public static boolean equals(Object a, Object b) { return (a b) || (a ! null a.equals(b)); }先判断地址是否相同a b两个 null 也相等再判断a ! null才调a.equals(b)。所以a 为 null 时绝不会报空指针这是它存在的最大意义。a.equals(b)和Objects.equals(a, b)的区别a.equals(b)a 是 null 就NPE。Objects.equals(a, b)a、b 谁为 null 都安全。它内部调用的仍是a.equals(b)所以比较规则还是看 a 的类有没有重写 equals比如 String、Integer 已经重写好了按内容比。两个都是 null 时返回truea b成立。实际开发里推荐用Objects.equals少写一个非空判断。2.public static boolean isNull(Object obj)—— 判断是否为 nullimport java.util.Objects; public class Demo2 { public static void main(String[] args) { String s1 hello; String s2 null; System.out.println(Objects.isNull(s1)); // false System.out.println(Objects.isNull(s2)); // true } }说明相当于obj null的封装为 null 返回true否则false。功能上和obj null完全一样只是写法更面向对象一点。常见用途作为方法引用传给 Stream比如list.stream().filter(Objects::isNull) // 筛出 null 元素这是直接用 null做不出来的。参数可以是任意类型包括基本类型包装类传null也没问题。3.public static boolean nonNull(Object obj)—— 判断是否不为 nullimport java.util.Objects; public class Demo3 { public static void main(String[] args) { String s1 hello; String s2 null; System.out.println(Objects.nonNull(s1)); // true System.out.println(Objects.nonNull(s2)); // false } }说明就是isNull的取反不为 null 返回true为 null 返回false。相当于obj ! null。它和isNull功能互逆用哪个看语义哪个更顺。常见用途同样是方法引用list.stream().filter(Objects::nonNull) // 去掉 null 元素别和isNull记反了nonNull 返回 true 说明不是 null。六、BigInteger

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑