5. 常用类
5.1. 字符串类型
5.1.1. String
是什么 :
字符串就是一段文本(区别于char,char是单引号,String是双引号) String是一个类,用final修饰 怎么用
public class StringDemo {
public static void main ( String[ ] args) {
String s = "我是一个字符串" ;
String s1 = new String ( "我是另一个字符串" ) ;
int length = s. length ( ) ;
int lengthTrim = s. trim ( ) . length ( ) ;
String sumString = s+ s1;
boolean isEquals = s. equals ( s1) ;
boolean isEqualsIgnoreCase = s. equalsIgnoreCase ( s1) ;
boolean isStartWith = s. startsWith ( "abc" ) ;
boolean isEndWith = s. endsWith ( "abc" ) ;
boolean isContains = s. contains ( "abc" ) ;
int index = s. indexOf ( "我" ) ;
int lastIndex = s. lastIndexOf ( "我" ) ;
String subString = s. substring ( 3 ) ;
String subString1 = s. substring ( 3 , 5 ) ;
String subString2 = s. substring ( s. indexOf ( "我" ) , s. indexOf ( "个" ) + 1 ) ;
String s2 = String. valueOf ( 123 ) ;
String[ ] stringArray = s. split ( "一" ) ;
String replaceAfter = s. replace ( "我" , "你" ) ;
}
}
注意 :
字符串一旦被初始化,就不可以被改变,存放在方法区中的常量池中。 String是否可以被继承:
String为什么不可变:
String 类中使用 final 关键字修饰字符数组来保存字符串,private final char value[],所以 String 对象是不可变的 String如何加入到String Pool中
使用 String 的 intern()
方法,或者直接String s = "aaa"
String s = new String(“abc”);创建了几个字符串对象
将创建 1 或 2 个字符串。如果池中已存在字符串常量“abc”,则只会在堆空间创建一个字符串常量“abc”。如果池中没有字符串常量“abc”,那么它将首先在池中创建,然后在堆空间中创建,因此将创建总共 2 个字符串对象。
5.1.2. StringBuffer和StringBuilder
是什么 :字符串缓冲区(本质就是可变字符串)怎么用 :
public class StringDemo {
public static void main ( String[ ] args) {
StringBuffer sb = new StringBuffer ( "我是字符串缓冲区" ) ;
sb. append ( "吗?没错就是" ) ;
sb. insert ( 5 , "123" ) ;
sb. delete ( 1 , 5 ) ;
sb. deleteCharAt ( 2 ) ;
sb. replace ( 1 , 5 , "ddd" ) ; 将1 至( 5 - 1 ) 位置替换成"" dd
int index = sb. indexOf ( "字符串" ) ;
int lastIndex = sb. lastIndexOf ( "我" ) ;
String subString = sb. substring ( 3 ) ;
String subString1 = sb. substring ( 3 , 5 ) ;
sb. reverse ( ) ;
}
}
注意 :
StringBuffer 和 StringBuilder 的区别:
StringBuffer线程安全 。StringBuilder线程不安全。单线程操作,使用StringBuilder 效率高 。
5.2. 包装类
是什么 :按照面向对象思想将基本数据类型封装成了对象,个人理解是为了在框架中避免默认值表达不清楚,并且方便的进行和字符串互转怎么用 :
public class Demo {
public static void main ( String[ ] args) {
Integer i = Integer. parseInt ( "123" )
String intString = i. toString
}
}
注意
基本数据类型对象包装类:都有 XXX parseXXX 方法(除了Character )
分类 数据类型 默认值 占用字节 取值范围 包装类 整数型 byte (byte)0 1 -27~2 7 Byte short (short)0 2 -215~2 15 Short int 0 4 -231~2 31 Integer long 0L 8 -263~2 63 Long 浮点型 float 0.0f 4 -2128~2 128 Float double 0.0 8 -21024~2 1024 Double 布尔型 boolean false 1 Boolean 字符型 char null 2 Character
5.2.1. 自动拆装箱
是什么 :自动装箱就是Java自动将基础类型值转换成对应的包装类对象,比如将int的变量转换成Integer对象,这个过程叫做装箱,反之将Integer对象转换成int类型值,这个过程叫做拆箱。
自动装箱:Integer i =10 //隐式调用了 Integer.valueOf(10); 自动拆箱:int b = i //隐式调用了x.intValue() 注意 :
包装类判断是否相同使用equals,而不是==;因为用==判断的是内存地址是否相等 Integer a = 3 和 Integer b = 3 数字<=127则相等,否则不相等;——Integer a = 3的时候,会自动调用valueOf方法,如果-128-127从IntegerCache找,否则重建;缓冲池可以通过 -XX:AutoBoxCacheMax= 来指定这个缓冲池的大小
5.3. 日期类型
略
5.3.1. Java8与以往日期操作对比
java8 java8以前 LocalDateTime Calendar Clock Calendar DateTimeFormatter SimpleDateFormat#format Instant Date
5.4. 数学类
5.4.1. java.lang.Math
public class Test01 {
public static void main ( String[ ] args) {
for ( int i = 0 ; i< 10 ; i++ ) {
System. out. println ( Math. random ( ) ) ;
}
System. out. println ( Math. sqrt ( 100 ) ) ;
System. out. println ( Math. cbrt ( 100 ) ) ;
System. out. println ( Math. pow ( 3 , 4 ) ) ;
System. out. println ( Math. ceil ( 5.6 ) ) ;
System. out. println ( Math. floor ( 5.6 ) ) ;
System. out. println ( Math. PI) ;
System. out. println ( Math. E) ;
}
}
5.4.2. java.text.DecimalFormat
public class Test02 {
public static void main ( String[ ] args) {
DecimalFormat decimalFormat = new DecimalFormat ( "###,###.0000" ) ;
System. out. println ( decimalFormat. format ( 12345.67 ) ) ;
}
}
5.4.3. java.math.BigDecimal
public class Test03 {
public static void main ( String[ ] args) {
BigInteger i1 = new BigInteger ( "123456789123456789123456798132456798132456798132456798456" ) ;
BigInteger i2 = new BigInteger ( "123456789123456789123456798132456798132456798132456798" ) ;
BigInteger i3 = i1. add ( i2) ;
System. out. println ( i3 ) ;
i3 = i1. subtract ( i2) ;
System. out. println ( i3 ) ;
i3 = i1. multiply ( i2) ;
System. out. println ( i3 ) ;
BigDecimal d1 = new BigDecimal ( "1234567891234567891234567981324567981324567981324567984561.32465798" ) ;
BigDecimal d2 = new BigDecimal ( "12345678912345678912345679813245679813245679813245679841.32465798" ) ;
BigDecimal d3 = d1. divide ( d2, RoundingMode. CEILING) ;
System. out. println ( d3 ) ;
}
}
5.4.4. java.util.Random
public class Test04 {
public static void main ( String[ ] args) {
Random random = new Random ( ) ;
for ( int i = 1 ; i<= 10 ; i++ ) {
System. out. println ( random. nextDouble ( ) ) ;
}
System. out. println ( "-----------------" ) ;
for ( int i = 1 ; i<= 10 ; i++ ) {
System. out. println ( random. nextInt ( ) ) ;
}
System. out. println ( "-----------------" ) ;
for ( int i = 1 ; i<= 10 ; i++ ) {
System. out. println ( random. nextInt ( 100 ) ) ;
}
System. out. println ( "-----------------" ) ;
Random random1 = new Random ( 45679 ) ;
Random random2 = new Random ( 45679 ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
System. out. println ( random1. nextDouble ( ) ) ;
}
System. out. println ( "=====================" ) ;
for ( int i = 1 ; i <= 10 ; i++ ) {
System. out. println ( random2. nextDouble ( ) ) ;
}
}
}