Math

java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单。

Math.PI

// 静态常量public static final double PI = 3.14159265358979323846;

abs

// 返回参数的绝对值public static int abs(int a)
System.out.println(Math.abs(-10)); // 10

round

// 按照四舍五入返回最接近参数的int类型// 参数为float类型 返回值为int类型public static int round(float a)// 参数为double类型 返回值为long类型public static long round(double a)
System.out.println(Math.round(3.14f)); // 3System.out.println(Math.round(3.5)); // 4

ceil

// 向上取整, 返回double类型public static double ceil(double a)
System.out.println(Math.ceil(10.1)); // 11.0System.out.println(Math.ceil(-10.9)); // -10.0

floor

// 向下取整, 返回double类型public static double floor(double a)
System.out.println(Math.floor(10.9)); // 10.0System.out.println(Math.floor(-10.1)); // -11.0

max

// 返回两个数的最大值, 参数可以是浮点型 或 整型public static int max(int a, int b)
System.out.println(Math.max(10, 20)); // 20

min

// 返回两个数的最小值, 参数可以是浮点型 或 整型public static int min(int a, int b)
System.out.println(Math.min(10, 20)); // 10

pow

// 返回 a的b次幂public static double pow(double a, double b)
System.out.println(Math.pow(3, 5)); // 243.0

random

// 返回随机小数[0-1), 该方法调用了java.util.Random类public static double random()
System.out.println(Math.random()); // 0.20964406221200327// 1-100的随机整数System.out.println(Math.floor(Math.random() * 100) + 1);

BigInteger

java.math.BigInteger类,不可变的任意精度的整数。如果运算中,数据的范围超过了long类型后,可以使用BigInteger类实现,该类的计算整数是不限制长度的。

// 构造方法// 超过long类型的范围,已经不能称为数字了,因此构造方法中采用字符串的形式来表示超大整数,将超大整数封装成BigInteger对象。public BigInteger(String val)
BigInteger b1 = new BigInteger("123213124124543123");BigInteger b2 = new BigInteger("123213124124543123");

add

// 返回其值为 (this + val) 的 BigInteger,超大整数加法运算public BigInteger add(BigInteger val)
System.out.println(b1.add(b2));// 246426248249086246

subtract

// 返回其值为 (this - val) 的 BigInteger,超大整数加法运算public BigInteger subtract(BigInteger val)
System.out.println(b1.subtract(b2));// 0

multiply

// 返回其值为 (this * val) 的 BigInteger,超大整数加法运算public BigInteger multiply(BigInteger val)
System.out.println(b1.multiply(b2));// 15181473956530070530603493486593129

divide

// 返回其值为 (this / val) 的 BigInteger,超大整数加法运算public BigInteger divide(BigInteger val)
System.out.println(b1.divide(b2));// 1

BigDecimal

java.math.BigDecimal类,不可变的、任意精度的有符号十进制数。该类可以实现超大浮点数据的精确运算。

// 构造方法public BigDecimal(String val)
BigDecimal bd1 = new BigDecimal("0.01");BigDecimal bd2 = new BigDecimal("0.05");

常用方法

BigDecimal的 加减乘 方法与BigInteger相同, 除法在可以除尽的情况下可以使用
若除不尽时 无法得到一个精确的小数 会报错, 需要传入额外的参数

// add 加法System.out.println(bd1.add(bd2)); // 0.06// subtract 减法System.out.println(bd1.subtract(bd2)); // -0.04// multiply 乘法System.out.println(bd1.multiply(bd2)); // 0.0005

divide

public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)// BigDecimal divesor:此 BigDecimal 要除以的值// int scale 保留几位小数// int roundingMode 模式:(常用)// BigDecimal.ROUND_HALF_UP 四舍五入// BigDecimal.ROUND_UP 向上取整// BigDecimal.ROUND_DOWN 向下取整
System.out.println(bd1.divide(bd2,2,BigDecimal.ROUND_HALF_UP));// 0.20

基本类型包装类

Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,如下:

基本类型对应的包装类(位于java.lang包中)
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

静态方法

// 返回表示指定的 int 值的 Integer 实例public static Integer valueOf(int i)// 返回表示指定的 String 值的 Integer 实例public static Integer valueOf(String s)
Integer i1 = Integer.valueOf(100);System.out.println(i1); // 100Integer i2 = Integer.valueOf("100");System.out.println(i2); // 100

装箱与拆箱

基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:

  • 装箱:从基本类型转换为对应的包装类对象。
  • 拆箱:从包装类对象转换为对应的基本类型。

基本数值 –> 包装对象

Integer i = new Integer(4);//使用构造函数函数Integer ii = Integer.valueOf(4);//使用包装类中的valueOf方法

包装对象 –> 基本数值

int num = i.intValue();

自动装箱与自动拆箱

由于经常要做基本类型与包装类之间的转换,从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可以自动完成。

Integer i = 4; //自动装箱。相当于Integer i = Integer.valueOf(4);i = i + 5; //等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5;//加法运算完成后,再次装箱,把基本数值转成对象。

基本类型与字符串之间的转换基本类型转换为String

  • 方式一:直接在数字后加一个空字符串
  • 方式二:通过String类静态方法valueOf()
public static void main(String[] args) {    //int --- String    //方式1    String s1 = 10 + "";    System.out.println(s1);    //方式2    //public static String valueOf(int i)    String s2 = String.valueOf(10);    System.out.println(s2);}

String转换成基本类型

除了Character类之外,其他所有包装类都具有parseXxx的静态方法,可以将字符串参数转换为对应的基本类型:

// 将字符串参数转换为对应的int基本类型。public static int parseInt(String s)// 将字符串参数转换为对应的double基本类型。public static double parseDouble(String s)// 字符串参数转换为对应的boolean基本类型。public static boolean parseBoolean(String s)// 字符串参数转换为char基本类型,使用String类中的charAt方法public char charAt(int index)
// String -> intSystem.out.println(Integer.parseInt("10"));// String -> doubelSystem.out.println(Double.parseDouble("10.21"));// String -> booleanSystem.out.println(Boolean.parseBoolean("true"));// String -> charSystem.out.println("a".charAt(0));

compare

// 构造方法public static int compare(int x, int y)// x > y 返回 1// x = y 返回 0// x < y 返回 -1
System.out.println(Integer.compare(20, 20));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_]

正则规则-数量词

{n} 匹配n次{n,} 匹配n次或多次{n,m}匹配n次到m次

String类matches方法

public boolean matches(String regex)// 将字符串与传入的正则表达式进行匹配, 返回布尔值
public static void main(String[] args){    //验证手机号码    String tel = "13912345678";    String telRegex = "1[345678][0-9]{9}";    boolean flag = tel.matches(telRegex);    System.out.println(flag);    //验证邮件地址    String email = "h_123123@163.com.cn.";    String emailRegex = "[a-zA-Z0-9_]+@([a-z]+\\.[a-z]+)+";    flag = email.matches(emailRegex);    System.out.println(flag);}

String类split方法

String[] split(String regex)// 传递正则表达式规则,以正则规则对字符串进行切割
String str1 = "ab  a   bbb  abc   aa      c";// String[] strArr =str1.split(" +");String[] strArr =str1.split("\\s+");System.out.println(Arrays.toString(strArr));