IT评测·应用市场-qidao123.com技术社区

标题: java -- Math、BigInteger、BigDecimal类和基本类型的包装类、正则表达式 [打印本页]

作者: 南七星之家    时间: 2023-4-7 19:21
标题: java -- Math、BigInteger、BigDecimal类和基本类型的包装类、正则表达式
Math

java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单。
Math.PI
  1. // 静态常量
  2. public static final double PI = 3.14159265358979323846;
复制代码
abs
  1. // 返回参数的绝对值
  2. public static int abs(int a)
复制代码
  1. System.out.println(Math.abs(-10)); // 10
复制代码
round
  1. // 按照四舍五入返回最接近参数的int类型
  2. // 参数为float类型 返回值为int类型
  3. public static int round(float a)
  4. // 参数为double类型 返回值为long类型
  5. public static long round(double a)
复制代码
  1. System.out.println(Math.round(3.14f)); // 3
  2. System.out.println(Math.round(3.5)); // 4
复制代码
ceil
  1. // 向上取整, 返回double类型
  2. public static double ceil(double a)
复制代码
  1. System.out.println(Math.ceil(10.1)); // 11.0
  2. System.out.println(Math.ceil(-10.9)); // -10.0
复制代码
floor
  1. // 向下取整, 返回double类型
  2. public static double floor(double a)
复制代码
  1. System.out.println(Math.floor(10.9)); // 10.0
  2. System.out.println(Math.floor(-10.1)); // -11.0
复制代码
max
  1. // 返回两个数的最大值, 参数可以是浮点型 或 整型
  2. public static int max(int a, int b)
复制代码
  1. System.out.println(Math.max(10, 20)); // 20
复制代码
min
  1. // 返回两个数的最小值, 参数可以是浮点型 或 整型
  2. public static int min(int a, int b)
复制代码
  1. System.out.println(Math.min(10, 20)); // 10
复制代码
pow
  1. // 返回 a的b次幂
  2. public static double pow(double a, double b)
复制代码
  1. System.out.println(Math.pow(3, 5)); // 243.0
复制代码
random
  1. // 返回随机小数[0-1), 该方法调用了java.util.Random类
  2. public static double random()
复制代码
  1. System.out.println(Math.random()); // 0.20964406221200327
  2. // 1-100的随机整数
  3. System.out.println(Math.floor(Math.random() * 100) + 1);
复制代码
BigInteger

java.math.BigInteger类,不可变的任意精度的整数。如果运算中,数据的范围超过了long类型后,可以使用BigInteger类实现,该类的计算整数是不限制长度的。
  1. // 构造方法
  2. // 超过long类型的范围,已经不能称为数字了,因此构造方法中采用字符串的形式来表示超大整数,将超大整数封装成BigInteger对象。
  3. public BigInteger(String val)
复制代码
  1. BigInteger b1 = new BigInteger("123213124124543123");
  2. BigInteger b2 = new BigInteger("123213124124543123");
复制代码
add
  1. // 返回其值为 (this + val) 的 BigInteger,超大整数加法运算
  2. public BigInteger add(BigInteger val)
复制代码
  1. System.out.println(b1.add(b2));
  2. // 246426248249086246
复制代码
subtract
  1. // 返回其值为 (this - val) 的 BigInteger,超大整数加法运算
  2. public BigInteger subtract(BigInteger val)
复制代码
  1. System.out.println(b1.subtract(b2));
  2. // 0
复制代码
multiply
  1. // 返回其值为 (this * val) 的 BigInteger,超大整数加法运算
  2. public BigInteger multiply(BigInteger val)
复制代码
  1. System.out.println(b1.multiply(b2));
  2. // 15181473956530070530603493486593129
复制代码
divide
  1. // 返回其值为 (this / val) 的 BigInteger,超大整数加法运算
  2. public BigInteger divide(BigInteger val)
复制代码
  1. System.out.println(b1.divide(b2));
  2. // 1
复制代码
BigDecimal

java.math.BigDecimal类,不可变的、任意精度的有符号十进制数。该类可以实现超大浮点数据的精确运算。
  1. // 构造方法
  2. public BigDecimal(String val)
复制代码
  1. BigDecimal bd1 = new BigDecimal("0.01");
  2. BigDecimal bd2 = new BigDecimal("0.05");
复制代码
常用方法

BigDecimal的 加减乘 方法与BigInteger相同, 除法在可以除尽的情况下可以使用
若除不尽时 无法得到一个精确的小数 会报错, 需要传入额外的参数
  1. // add 加法
  2. System.out.println(bd1.add(bd2)); // 0.06
  3. // subtract 减法
  4. System.out.println(bd1.subtract(bd2)); // -0.04
  5. // multiply 乘法
  6. System.out.println(bd1.multiply(bd2)); // 0.0005
复制代码
divide
  1. public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
  2. // BigDecimal divesor:此 BigDecimal 要除以的值
  3. // int scale 保留几位小数
  4. // int roundingMode 模式:(常用)
  5.         // BigDecimal.ROUND_HALF_UP 四舍五入
  6.         // BigDecimal.ROUND_UP 向上取整
  7.         // BigDecimal.ROUND_DOWN 向下取整
复制代码
  1. System.out.println(bd1.divide(bd2,2,BigDecimal.ROUND_HALF_UP));
  2. // 0.20
复制代码
基本类型包装类

Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,如下:
基本类型对应的包装类(位于java.lang包中)byteByteshortShortintIntegerlongLongfloatFloatdoubleDoublecharCharacterbooleanBoolean静态方法
  1. // 返回表示指定的 int 值的 Integer 实例
  2. public static Integer valueOf(int i)
  3. // 返回表示指定的 String 值的 Integer 实例
  4. public static Integer valueOf(String s)
复制代码
  1. Integer i1 = Integer.valueOf(100);
  2. System.out.println(i1); // 100
  3. Integer i2 = Integer.valueOf("100");
  4. System.out.println(i2); // 100
复制代码
装箱与拆箱

基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:
基本数值 --> 包装对象
  1. Integer i = new Integer(4);//使用构造函数函数
  2. Integer ii = Integer.valueOf(4);//使用包装类中的valueOf方法
复制代码
包装对象 --> 基本数值
  1. int num = i.intValue();
复制代码
自动装箱与自动拆箱

由于经常要做基本类型与包装类之间的转换,从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可以自动完成。
  1. Integer i = 4; //自动装箱。相当于Integer i = Integer.valueOf(4);
  2. i = i + 5; //等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5;
  3. //加法运算完成后,再次装箱,把基本数值转成对象。
复制代码
基本类型与字符串之间的转换

基本类型转换为String

  1. public static void main(String[] args) {
  2.     //int --- String
  3.     //方式1
  4.     String s1 = 10 + "";
  5.     System.out.println(s1);
  6.     //方式2
  7.     //public static String valueOf(int i)
  8.     String s2 = String.valueOf(10);
  9.     System.out.println(s2);
  10. }
复制代码
String转换成基本类型

除了Character类之外,其他所有包装类都具有parseXxx的静态方法,可以将字符串参数转换为对应的基本类型:
  1. // 将字符串参数转换为对应的int基本类型。
  2. public static int parseInt(String s)
  3. // 将字符串参数转换为对应的double基本类型。
  4. public static double parseDouble(String s)
  5. // 字符串参数转换为对应的boolean基本类型。
  6. public static boolean parseBoolean(String s)
  7. // 字符串参数转换为char基本类型,使用String类中的charAt方法
  8. public char charAt(int index)
复制代码
  1. // String -> int
  2. System.out.println(Integer.parseInt("10"));
  3. // String -> doubel
  4. System.out.println(Double.parseDouble("10.21"));
  5. // String -> boolean
  6. System.out.println(Boolean.parseBoolean("true"));
  7. // String -> char
  8. System.out.println("a".charAt(0));
复制代码
compare
  1. // 构造方法
  2. public static int compare(int x, int y)
  3. // x > y 返回 1
  4. // x = y 返回 0
  5. // x < y 返回 -1
复制代码
  1. System.out.println(Integer.compare(20, 20));
  2. System.out.println(Double.compare(20.3, 20.1));
复制代码
正则表达式

正则表达式(regular expression)是对字符串操作的一种规则, 用来做字符串匹配
正则规则-字符类

规则写法规则含义[abc]a、b 或 c(简单类)[^abc]任何字符,除了 a、b 或 c(否定)[a-zA-Z]a 到 z 或 A到 Z,两头的字母包括在内(范围)[0-9]0到9,两头的数字包括在内(范围)[a-zA-Z0-9]a 到 z 或 A到 Z或0-9正则规则-预定义字符类

规则写法规则含义.任何字符\d数字[0-9]\D非数字 [^0-9]\w单词字符 [a-zA-Z0-9_]\W非单词字符[^a-zA-Z0-9_]正则规则-数量词
  1. {n} 匹配n次
  2. {n,} 匹配n次或多次
  3. {n,m}匹配n次到m次
复制代码
String类matches方法
  1. public boolean matches(String regex)
  2. // 将字符串与传入的正则表达式进行匹配, 返回布尔值
复制代码
  1. public static void main(String[] args){
  2.     //验证手机号码
  3.     String tel = "13912345678";
  4.     String telRegex = "1[345678][0-9]{9}";
  5.     boolean flag = tel.matches(telRegex);
  6.     System.out.println(flag);
  7.     //验证邮件地址
  8.     String email = "h_123123@163.com.cn.";
  9.     String emailRegex = "[a-zA-Z0-9_]+@([a-z]+\\.[a-z]+)+";
  10.     flag = email.matches(emailRegex);
  11.     System.out.println(flag);
  12. }
复制代码
String类split方法
  1. String[] split(String regex)
  2. // 传递正则表达式规则,以正则规则对字符串进行切割
复制代码
  1. String str1 = "ab  a   bbb  abc   aa      c";
  2. // String[] strArr =str1.split(" +");
  3. String[] strArr =str1.split("\\s+");
  4. System.out.println(Arrays.toString(strArr));
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4