1.1、Math类介绍
Math包含执行基本数字运算的方法,如基本指数、对数、平方根和三角函数,所提供的都是静态方法,可以直接调用。
1.2、常用方法
方法名 | 说明 |
---|---|
public static int abs (int a) | 获取参数a的绝对值 |
public static double ceil (double a) | 向上取整 |
public static double floor (double a) | 向下取整 |
public static double pow (double a, double b) | 获取a的b次幂 |
public static long round (double a) | 四舍五入取整 |
public static int max (int a, int b) | 返回两数中的最大值 |
public static int min (int a, int b) | 返回两数中的最小值 |
public static double random () | 返回 [0,1) 之间的随机数 |
2、BigInteger类
2.1、BigInteger类介绍
java.math.BigInteger类是一个引用数据类型,可以用来对一些大整数做一些运算。当超出基本数据类型数据范围的整数运算时就可以使用BigInteger了。
2.2、BigInteger API
常用构造方法
构造方法 | 说明 |
---|---|
BigInteger (String value) | 可以将整数的字符串转换为BigInteger对象 |
常用方法
方法声明 | 说明 |
---|---|
public BigInteger add (BinIneger value) | 超大整数加法运算 |
public BigInteger subtract (BinIneger value) | 超大整数减法运算 |
public BigInteger multiply (BinIneger value) | 超大整数乘法运算 |
public BigInteger divide (BinIneger value) | 超大整数除法运算,除不尽时取整数部分 |
示例:
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
BigInteger num1 = new BigInteger("54511842184216");
BigInteger num2 = new BigInteger("4545484518412");
BigInteger resAdd = num1.add(num2);
BigInteger resSubtract = num1.subtract(num2);
BigInteger resMultiply = num1.multiply(num2);
BigInteger resDivide = num1.divide(num2);
System.out.println(resAdd);
System.out.println(resSubtract);
System.out.println(resMultiply);
System.out.println(resDivide);
}
}
3、BigDecimal类
3.1、BigDecimal类介绍
BigDecimal类:用来存储超出double范围的小数。用法与BigInteger类似。
3.2、BigDecimal常用API
常用构造方法
构造方法 | 说明 |
---|---|
BigDecimal(String value) | 可以将整数的字符串转换为BigDecimal对象 |
常用方法
方法声明 | 说明 |
---|---|
public BigDecimal add (BigDecimal value) | 加法运算 |
public BigDecimal subtract (BigDecimal value) | 减法运算 |
public BigDecimal multiply (BigDecimal value) | 乘法运算 |
public BigDecimal divide (BigDecimal value) | 除法运算,除不尽是会异常 |
public BigDecimal divide (BigDecimal value, int scale, int roundingMode ) | 除法运算,除不尽是用此方法。scale代表的是精确位数,roundingMode代表取舍模式:Bigdecimal.ROUND_HALF_UP四舍五入,Bigdecimal.ROUND_FLOOR去尾,Bigdecimal.ROUND_UP进一法 |
示例:
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
BigDecimal num1 = new BigDecimal("54511842184216.45454");
BigDecimal num2 = new BigDecimal("4545484518412.367");
BigDecimal resAdd = num1.add(num2);
BigDecimal resSubtract = num1.subtract(num2);
BigDecimal resMultiply = num1.multiply(num2);
BigDecimal resDivide = num1.divide(num2, 5);
System.out.println(resAdd);
System.out.println(resSubtract);
System.out.println(resMultiply);
System.out.println(resDivide);
}
}
4、Arrays工具类
4.1、Arrays类介绍
java.util.Arrays是数组的工具类,里面有很多静态的方法用来对数组进行操作,还包含一个静态工厂,可以将数组转换为List集合。
4.2、Arrays的方法使用
方法 | 说明 |
---|---|
public static void sort (int[] a) | 按照数字顺序排列指定数组 |
public static String toString (int [] a) | 返回指定数组内同的字符串表示形式 |
示例:
import java.util.Arrays;
import java.util.Random;
public class Test {
public static void main(String[] args) {
int [] array = new int[10];
Random random = new Random();
for(int i = 0; i < array.length; i++){
int x = random.nextInt(31);
array[i] = x;
}
System.out.println("排序前:" + Arrays.toString(array));
Arrays.sort(array);
System.out.println("排序后:" + Arrays.toString(array));
}
}
运行结果:
排序前:[24, 11, 7, 16, 21, 10, 15, 0, 4, 24]
排序后:[0, 4, 7, 10, 11, 15, 16, 21, 24, 24]
5、包装类
5.1、什么是包装类?
java把八种基本类型,进行了封装,变成了包装类。java中的基本数据类型没有方法和属性,而包装类就是为了让基本数据类型拥有方法和属性,实现对象化交互。
基本数据类型 | 包装类型 |
---|---|
byte | Byte |
shot | Shot |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
5.2、包装类解决啥问题?
实现String类型和7种基本类型(没有char)之间的转换
5.3、包装类怎么使用嘞?
使用Integer作为示例
Integer num = 100;
Integer num = null;
使用包装类:解决String类型和基本数据类型之间数据转换,在开发过程中数据在不同平台传输时都以字符串形式存在,有些数据表示的是数值含义,如果要用于计算,我们就需要将其转换成基本数据类型。
我们将基本数据类型转换为字符串是这样做的
int num = 100;
//任意类型和字符串相加,结果都是String类型
String str = num + "";
//或者用valueof
String str = String.valueof(num);
我们将字符串转换为基本数据类型是这样做的
//包装类型除了Character之外,都存在一个静态方法:parseXxxx,如果字符串参数的内容无法正确转换为对应的基本类型,则会抛出java.lang.NumberFormatException异常
String str = "1988";
int num = Integer.parseInt(str);
String str = "true"
boolean sign = Boolean.parseBoolean(str);
...
5.4、使用包装类的注意点
1、在把String类型数据,转换为基本数据类型时,容易发生NumberFormatException异常,原因是String类型数据包,不符合转换的基本类型数据格式。
2、包装类对象的初始值为null(是一个对象)。
6、String类
6.1、String类中常用方法
1、字符串拼接
方法 | 说明 |
---|---|
public String concat (String str) | 将字符串与参数字符串进行就拼接,返回一个新字符串,等效于加号拼接 |
public class Test {
public static void main(String[] args) {
String str1 = "Hello ";
String str2 = "World!";
String res = str1.concat(str2);
System.out.println(res);
}
}
2、字符串判断
方法 | 说明 |
---|---|
public boolean contains (CharSequence s) | 判断参数字符串在当前字符串中是否存在(区分大小写),存在返回true |
public boolean endsWith (String suffix) | 判断此字符串是否以指定后缀结尾(区分大小写) |
public boolean startsWith (String prefix) | 判断此字符串是否以指定前缀结尾(区分大小写 |
public int indexOf (String str) | 返回指定字符串第一次出现的字符串内的索引,如果不包含则返回-1 |
public int lastIndexOf (String str) | 返回指定字符串最后一次出现的字符串内的索引,如果不包含则返回-1 |
public class Test {
public static void main(String[] args) {
String str = "I Love TG, I Love 糖锅 糖锅 糖锅";
//判断字符串中是否有"糖锅"
boolean tg = str.contains("糖锅");
System.out.println(tg);
//判断字符串是否以"TG"结尾
boolean tg1 = str.endsWith("TG");
System.out.println(tg1);
//判断字符串是否以”I"开头
boolean tg2 = str.startsWith("I");
System.out.println(tg2);
//寻找字符串中第一次出现"糖锅"的位置
int tg3 = str.indexOf("糖锅");
System.out.println(tg3);
//寻找字符串中最后一次出现"糖锅"的位置
int tg4 = str.lastIndexOf("糖锅");
System.out.println(tg4);
}
}
3、替换截取
方法 | 说明 |
---|---|
public String replace (CharSequence target, CharSequence replacement) | 将字面目标序列匹配的字符串的每个字符串替换为指定的文字替换序列 |
public String substring (int beginIndex) | 将字符串从beginIndex开始截取到末尾 |
public String substring (int beginIndex, int endIndex) | 将字符串从beginIndex开始截取到endIndex - 1处(包含头,不包含尾) |
public class Test {
public static void main(String[] args) {
String str = "I love tg, I love TG";
//将love替换成like
String replace = str.replace("love", "like");
System.out.println(replace);
//截取字符串"I love TG"
String sub1 = str.substring(11);
System.out.println(sub1);
//截取字符串"I love tg"
String sub2 = str.substring(0, 9);
System.out.println(sub2);
}
}
4、字符串转换
方法 | 说明 |
---|---|
public char [] toCharArray () | 将当前字符串转换为char[]数组 |
public char charAt (int index) | 将字符串中指定索引的字符获取 |
public String toLowerCase () | 将当前字符串中所有的英文字符转换为小写,并返回转换后的新字符串,原来的字符串不变 |
public String toUpperCase () | 将当前字符串中所有的英文字符转换为大写,并返回转换后的新字符串,原来的字符串不变 |
public class Test {
public static void main(String[] args) {
String str = "i love tg";
//将字符串转换为char数组
char[] demo = str.toCharArray();
for(int i = 0; i < demo.length; i++) {
System.out.print(demo[i]);
}
//获取字符串中第一个字符
char demo1 = str.charAt(0);
System.out.println("\n" + demo1);
//将字符串中的字符转化为大写
String demo2 = str.toUpperCase();
System.out.println(demo2);
}
}
5、前后空格去除
方法 | 说明 |
---|---|
public String trim () | 去掉当前字符串的前后空格(空字符、制表符、换行符等等),并返回一个新字符串,原来的字符串不变 |
public class Test {
public static void main(String[] args) {
String str = " I love tg ";
System.out.println("去除空格前字符串的长度为:" + str.length());
//去除字符串的前后空格
String demo1 = str.trim();
System.out.println("去除空格后得到的字符串长度为:" + demo1.length());
}
}
6、字符串的切割
方法 | 说明 |
---|---|
public String[] split (String regex) | 切割字符串,将字符串以regeex作为分隔符进行切割 |
public class Test {
public static void main(String[] args) {
String str = "I love tg";
//按照空格截取字符串
String[] demo = str.split(" ");
for(int i = 0; i < demo.length; i++){
System.out.println(demo[i]);
}
}
}