JavaSE-JavaAPI之lang包
温馨提示:
本文最后更新于 2024年07月21日,已超过 290 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
包装类
数值型(Math,Random,BigInteger,BigDecimal)
字符型(String,StringBuffer,StringBuilder)
包装类:
8大数据类型都有对应的包装类。(集合中不能存基本数据类型)
byte,short,int,long,float,double,char,boolean 栈内存
Byte,Short,Integer,Long,Float,Double,Character,Boolean
自动装箱:将基本数据类型包装为对象数据类型。
Integer a = 9; //相当于Integer a = Integer.valueOf(9);
自动拆箱:将包装类型转为基本数据类型。
int c = a; //相当于int c = a.intValue();
Tnteger类的valueOf方法和parseInt方法的区别?
- parseInt(String s, int radix); radix进制数,返回int类型
- parseInt(String s);返回int类型,实际是调用的parseInt(s,10);默认为10进制
- parseInt可能会报NumberFormatException
- valueOf(int); 返回Integer对象
- valueOf(String s);实质是先调用parseInt(s,10)返回int当参数,然后调用valueOf(int)这个方法。返回Integer对象
- valueOf(String s, int radix);实质是先调用parseInt(s, radix)返回int当参数,然后调用valueOf(int)这个方法。返回Integer对象
数值型(Math)
abs(参数)//求绝对值,可以传int,long,float,double类型,返回值也就是所传的类型
取整
round(环绕的意思)
传参为double的时候,返回long类型的数
public static long round(double a) {...}
- 传参为float的时候,返回int类型的数,传非double和boolean的基本数据类型都是是调用这个方法。
public static int round(float a) {...}
- 当round传参大于0的时候,进行四舍五入,当round传参小于0是,直接入,即-1.4返回成-1
- 例如
System.out.println("Math.round(1.4):" + Math.round(1.4)); System.out.println("Math.round(1.5):" + Math.round(1.5)); System.out.println("Math.round(-1.4):" + Math.round(-1.4)); System.out.println("Math.round(-1.5):" + Math.round(-1.5)); 打印结果: Math.round(1.4):1 Math.round(1.5):2 Math.round(-1.4):-1 Math.round(-1.5):-1
- ceil天花板数
- 只要小数部分大于0,就入
- 返回double类型,能传出boolean以外的所有基本数据类型
public static double ceil(double a) {...}
- 例如
System.out.println("Math.ceil(1.0):" + Math.ceil(1.0)); System.out.println("Math.ceil(1.4):" + Math.ceil(1.4)); System.out.println("Math.ceil(1.5):" + Math.ceil(1.5)); System.out.println("Math.ceil(1.5f):" + Math.ceil(1.5f)); System.out.println("Math.ceil(-1.0):" + Math.ceil(-1.0)); System.out.println("Math.ceil(-1.4):" + Math.ceil(-1.4)); System.out.println("Math.ceil(-1.5):" + Math.ceil(-1.5)); 打印结果: Math.ceil(1.0):1.0 Math.ceil(1.4):2.0 Math.ceil(1.5):2.0 Math.ceil(1.5f):2.0 Math.ceil(-1.0):-1.0 Math.ceil(-1.4):-1.0 Math.ceil(-1.5):-1.0
- floor地板数
- 只要小数部分大于0,就舍
- 返回double类型,能传出boolean以外的所有基本数据类型
public static double floor(double a) {...}
- 例如:
System.out.println("Math.floor(1.0):" + Math.floor(1.0)); System.out.println("Math.floor(1.1):" + Math.floor(1.1)); System.out.println("Math.floor(1.9):" + Math.floor(1.9)); System.out.println("Math.floor(1.1f):" + Math.floor(1.1f)); System.out.println("Math.floor(-1.0):" + Math.floor(-1.0)); System.out.println("Math.floor(-1.1):" + Math.floor(-1.1)); System.out.println("Math.floor(-1.9):" + Math.floor(-1.9)); 打印结果: Math.floor(1.0):1.0 Math.floor(1.1):1.0 Math.floor(1.9):1.0 Math.floor(1.1f):1.0 Math.floor(-1.0):-1.0 Math.floor(-1.1):-2.0 Math.floor(-1.9):-2.0
- pow 幂运算
- 传非boolean以外的所有基本数据类型,返回double类型
public static double pow(double a, double b) {...}
- 传非boolean以外的所有基本数据类型,返回double类型
- sqrt 开根号
- 传非boolean以外的所有基本数据类型,返回double类型
public static double sqrt(double a) {...}
- 传非boolean以外的所有基本数据类型,返回double类型
- random :产生0到1的double类型小数,包括0,不包括1。
public static double random() {...}
- 数值型(BigInteger)
- 使用new关键字创建对象,一般调用形参为String的构造方法。如:new BigInteger("" + 1);
- 加减乘除取模见下面的例子:
期间出现了一个小毛病,我直接b.multiply(b);//乘,没有写成b = b.multiply(b);//乘,一直没有效果BigInteger b = new BigInteger("" + 100); System.out.println("原来:" + b.toString()); b = b.multiply(b);//乘 System.out.println("乘法:" + b.toString()); b = b.add(b);//加 System.out.println("加法:" + b.toString()); b = b.divide(new BigInteger("3"));//除 System.out.println("除法:" + b.toString()); b = b.subtract(new BigInteger("3"));//减法 System.out.println("减法:" + b.toString()); b = b.mod(new BigInteger("3"));//取模 System.out.println("取模:" + b.toString()); 打印结果: 原来:100 乘法:10000 加法:20000 除法:6666 减法:6663 取模:0
- 求30的阶乘:
- 第一种方法:
public static BigInteger funBig(int n) { BigInteger sum = new BigInteger("" + 1); for (int i = 1; i <= n; i++) { sum = sum.multiply(new BigInteger("" + i)); } return sum; }
- 第二种方法:使用一行代码实现:
public static BigInteger fBigInteger(int n) { return n <= 1 ? new BigInteger("" + 1) : new BigInteger("" + n).multiply(fBigInteger(n - 1)); }
- 第一种方法:
- 字符型:
- String,StringBuilder,StringBuffer的详细解释,见我的另外一篇博客
https://fxyh.top/article/19 - 这里我们就说些常用的方法吧。
- String https://fxyh.top/article/20
- StringBuilder
StringBuilder sb = new StringBuilder("abcbabc"); System.out.println("最初:" + sb); sb.append("def"); System.out.println("append(def):" + sb); sb.delete(1, 3);//删除从第一个字符开始,到第三个字符,不包括第三个字符,包头不包尾 System.out.println("delete(1,3):" + sb); sb.reverse();//反转 System.out.println("反转:" + sb); int indexOf = sb.indexOf("b");//第一个b System.out.println("从前往后找b:" + indexOf); int lastIndexOf = sb.lastIndexOf("b");//最后一个b System.out.println("从后往前找b:" + lastIndexOf);
- StringBuffer和StringBuilder类似。
- String,StringBuilder,StringBuffer的详细解释,见我的另外一篇博客
正文到此结束
- 本文标签: Java
- 本文链接: https://fxyh.top/article/22
- 版权声明: 本文由fxyh原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权