- String代表字符串,是个常量,创建值之后不能再更改
- 变更字符串内容实际上是新开了内存空间,并改了记录的地址值,指向新字符串
String = new String( byte[] 或者 char[] )
int = str.length
String = str.substring(index)
String = str.substring(begin , end)
boolean = str.startsWith("abc")
boolean = str.endsWith("abc")
boolean = str.contains("abc")
boolean = str.isEmpty()
int = str.indexOf("abc")
int = str.indexOf( "abc" , startIndex )
byte[] = str.getBytes() 与 char[] = str.toCharArray()
String = str.replace( old , new )
String = str.trim()
String = str.toLowerCase()
String = str.toUpperCase()
- 又称可变字符序列,即可变的字符串,其实就是一个容器,容器里存放多个字符串
- 长度可变,且支持任意类型(任意类型转成String存储)
- 所有容器里的数据最终变成一个字符串
strbuff.append( String )
strbuff.delete( start , end )
strbuff.replace( start , end , String )
strbuff.reverse()
- 通常用来检索、替换一些符合规则的文本(详细后续补坑)
- 正则表达式是通过Pattern类与Matcher类实现的;
- Pattern类用于创建一个正则表达式(匹配模式),只有简单的匹配操作
- Matcher类提供更强更便捷的正则匹配操作,以及分组支持
boolean = str.matches( regex )
String[] = str.split( regex )
String = str.replaceAll( regex , "new" )
基本类型封装成对象,提供更多操作数值的功能:
Integer = Integet.valueOf( value )
Character = Character.valueOf( value )
除上面两个外,其他六个将名字小写改为大写即可
字符串转基本类型:(静态方法)
byte = Byte.parseByte("127")
short = Short.parseShort(str)
int = Integer.parseInt(str)
long = Long.parseLong(str)
float = Float.parseFloat(str)
double = Double.parseDouble(str)
在需要的情况下,基本类型与包装类型可以通用
- 自动拆箱 = 对象转成基本数值
- 自动装箱 = 基本数值转成对象(在数值范围内,不会新创建对象空间)
- BigInteger(整形)
用不同构造方法可以将 二进制补码形式的btye数组、十进制、字符串、等等等转换为BigInteger
public static void main(String[] args) { //大数据封装为BigInteger对象 BigInteger big1 = new BigInteger(12345678909876543210); BigInteger big2 = new BigInteger(98765432101234567890); //add实现加法运算 BigInteger bigAdd = big1.add(big2); //subtract实现减法运算 BigInteger bigSub = big1.subtract(big2); //multiply实现乘法运算 BigInteger bigMul = big1.multiply(big2); //divide实现除法运算 BigInteger bigDiv = big2.divide(big1); }
BigDecimal(浮点型)
加:add
减:subtract
乘:multiply
除:divide
乘次方:pow ( int )
取绝对值:abs( )
取反:negate( )
对比:compareTo ( BigDecimal )
设置小数精确度:setScale( int )
保留小数点并选择保留方式(加一或四舍五入):setScale( int , int )
想要避免精度损失,应先将double类型的值转化为 String,然后用 BigDecimal 类型运算
public static void main(String[] args) { //大数据封装为BigDecimal对象 BigDecimal big1 = new BigDecimal("0.09"); BigDecimal big2 = new BigDecimal("0.01"); BigDecimal big3 = new BigDecimal("1.0"); BigDecimal big4 = new BigDecimal("0.32"); BigDecimal big5 = new BigDecimal("1.105"); BigDecimal big6 = new BigDecimal("100"); //add实现加法运算 BigDecimal bigAdd = big1.add(big2); //subtract实现减法运算 BigDecimal bigSub = big3.subtract(big4); //multiply实现乘法运算 BigDecimal bigMul = big5.multiply(big6); }
- 提供的是对应系统属性信息和系统操作
- System类不能手动创建对象,因为被private修饰
long = System.currentTimeMillis()
- 数学工具类,均为静态方法
double = Math.abs(double)
double = Math.max( n , n) 与 double = Math.min( n , n)
double = Math.round( double )
double = Math.random()
- 用于操作数组的工具类
- 如果指定数组引用是null,会抛出空指针异常
Arrays.sort( int[] )
int = Arrays.binarySearch( int[] , int key)