一、包装类
JAVA是一种面向对象语言,java中的类把方法与数据连接在一起,但在JAVA中不能定义基本类型对象,为了能将基本类型视为对象进行处理,java为每个基本类型都提供了包装类。
对应关系如下:
基本类型 | 包装类型 |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
1、Number类
BigDecimal、BigInteger、Byte、Double、Float、Integer、Long、Short 都是Number类的子类。
Character和Boolean 父类是Object,不属于Number下面的子类。
查看API文档:
Number类是抽象类,不能直接通过new关键字创建。 Number类有六个方法,并且这些方法有些是abstract修饰的方法(抽象方法),Number 的子类需要覆写这些抽象方法。
2、Integer包装类
查看API文档:
理解:Integer类里面包含一个int value,可以理解内部有个变量盒子,存放int值,也就是说Integer类里面有个int值。
由于父类Number已经提供6个方法,子类就可以调用父类提供6个方法,把Integer对象里面的int 值转换成其他的6个基本类型值,比如转换成byte,short,int,long,float,double 。
同理:对于Number子类的其他类型,比如Double类也是一样,可以调用Number类里面提供的6个方法把Double里面存放的值转换成这6个值。
查看 Integer 源代码,可以看到 Integer 类里面有 int 字段,有个构造方法。
当通过Integer构造方法创建Integer对象的时候 (比如new Integer(123)),就把值,存放到 int 类型的字段里面。
2.1 Integer 构造方法
查看API文档:
查看 Integer 源代码:
可以看出通过 Integer(String s) 构造方法的源码看出,最终还是把值赋值给变量 this.value,this.value 是指在Integer里面定义的一个int 变量,用于存放值。
注意:
1、当试图将字符串转换成一种数值类型时,该字符串里面包含不能转换的字符时,出现异常。例如含有字母(A、B、C)或运算符(+ 、-)等。
2、不能超出 int 的范围时,比如12345678987654321 。
3、自动装箱拆箱
自动装箱:从基本类型(int)转换成包装类型(Integer),不用手动进行转换操作,编译器会自动调用 Integer.valueof 方法把基本类型(int)转换成 (Integer)类型这种方式。
方式一:通过构造方法new Integer(123);
方式二:直接赋值给Integer ;
Integer integer = 123;
自动拆箱:从包装类型(Integer)转换成基本类型(Int),也不用在进行手动进行转换操作,编译器会自动调用 Integer.intValue 把包装类型(Integer)转换成基本类型 (int)。
方式一:调用Integer里的方法 intValue方法;
方式一:直接赋值给int;
8 种基本类型与包装类型之间的转换都支持自动拆箱和装箱操作。
4、short类、Long类和Double类
除了这个Integer包装类以外,还有Short,Long,Double包装类,结构都和Integer类似。
5、Boolean类 和 Character类
Character和Boolean 父类是Object。
Boolean类无最大,最小值。
Character就表示一个字符,而String 字符串表示多个字符。
6、String类
查看API文档:
从API看出String类特点:
1.索引是从0开始的;
2.字符串对象(String对象)一旦创建其长度和内容就不能更改;
创建String对象的方式:
1、直接赋值,String name = "abcdefg";
2、调用String的构造方法。
public class Demo {
/**
* 测试创建字符串
*/
@Test
public void test1(){
String s1 = new String(); //表示一个空字符串
String s2 = "";
String s3 = null; System.out.println(s1 == s2 ); //false
System.out.println(s1.equals(s3)); //false
//System.out.println(s3.equals(s1)); s3 放在equals前面会报空指针异常 java.lang.NullPointerException
}
}
分析:
String s1 = new String(); //s1将指向具体的String实例,默认值为“”,在内存中分配一个空间;
String s2 = " "; //s2对应一个空串,声明对象的引用 ,在内存中分配一个空间;
String s3 = null; //s3引用为空;
注意:s1和s2被实例化,而s3没有实例化,但s1和s2所指的地址空间不同,值一样,都为空。
6.1 String 类常用方法
1、char charAt(int index);
取字符串中指定位置的字符,index 从0 开始计算。
public class Demo {
/**
* 测试charAt
*/
@Test
public void testCharAt() {
String str = "abcdefg";
for (int i = 0; i < str.length(); i++) {
char element = str.charAt(i);
System.out.print("[" + element + "]"); //输出:[a][b][c][d][e][f][g]
}
}
}
2、int codePointAt(int index);
返回指定索引处的字符(Unicode 代码点)。
public class Demo {
/**
* 测试codePointAt
*/
@Test
public void testCodePointAt(){
String str = "abcdefg";
int codePointAt = str.codePointAt(2);
System.out.println(codePointAt); //输出:99 代表c 的 ASCII码
}
}
3、String concat(String str);
将指定(传入)字符串连接到此(当前)字符串的结尾。用于连接字符串。
public class Demo {
/**
* 测试concat
*/
@Test
public void testConcat(){
String str1 = "字符串1111";
String str2 = "字符串2222";
String concatStr = str1.concat(str2); //创建一个字符串
System.out.println(concatStr); //输出:字符串1111字符串2222
}
}
4、boolean endsWith(String suffix);
测试此字符串是否以指定的后缀结束。
public class Demo {
/**
* 测试endsWith
*/
@Test
public void testEndsWith(){
String str = "abcd";
boolean result = str.endsWith("d");
System.out.println(result); //返回:true
}
}
5、boolean isEmpty();
当且仅当length() 为 0 时返回 true。
public class Demo {
/**
* 测试isEmpty
*/
@Test
public void testIsEmpty(){
String str = ""; //不能为null
boolean result = str.isEmpty();
System.out.println(result); //返回:true
}
}
6、int length();
返回当前字符串长度。
7、String replace(char oldChar, char newChar);
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
public class Demo {
/**
* 测试replace
*/
@Test
public void testReplace(){
String str = "字符串222";
String newStr = str.replace("222", "替换");
System.out.println(newStr); //输出:字符串替换
}
}
8、String substring(int beginIndex);
返回一个新的字符串,从beginIndex 开始截取,它是此字符串的一个子字符串。
String substring(int beginIndex. int endIndex);
返回一个新字符串,它是此字符串的一个子字符串;注意:左闭右开,左边包括,右边不包括。
public class Demo {
/**
* 测试substring
*/
@Test
public void testSubString(){
String str1 = "abcdefg";
String substring = str1.substring(1);
System.out.println(substring); //输出:bcdefg String str2 = "abcdefg";
String substring1 = str2.substring(1, 3);
System.out.println(substring1); //输出:bc 注意:不包含index为3的字符
}
}
9、String toLowerCase();
将此String 中的所有字符都转换为小写。
10、String toUpperCase();
将此String 中的所有字符都转成大写。
11、String trim();
忽略字符串前导空白和尾部空白。
public class Demo {
/**
* 测试trim
*/
@Test
public void testTrim(){
String str = " 233333 ";
String trim = str.trim();
System.out.println(trim); //输出:233333 没有空格了
}
}
6.2 StringBuffer 类和 StringBuilder 类
查看API文档:
StringBuffer类常用方法
1、StringBuffer append(Object o);
将指定的任意类型对象追加到此StringBuffer 对象。
2、StringBuffer delete(int start, int end);
移除此序列的子字符串中的字符。
3、StringBuffer insert(int offset, Object o);
将任意类型参数的字符串表示形式插入此序列中。
4、StringBuffer replace(int start, int end, String str);
使用给定 String 中的字符替换此序列的子字符串中的字符。
public class StringBufferTest {
/**
* 测试StringBuffer方法
*/
@Test
public void testStringBufferMethod(){
//1.StringBuffer append(Object o); 添加
StringBuffer str = new StringBuffer("abcdefgh");
str.append("ijk");
System.out.println(str); //输出:abcdefghijk //2.StringBuffer delete(int start, int end); 删除
str.delete(0, 3);
System.out.println(str); //输出:defghijk 左闭右开[ ) //3、StringBuffer insert(int offset, Object o); 插入
str.insert(1, "333");
System.out.println(str); //输出:d333efghijk //4、StringBuffer replace(int start, int end, String str); 替换
str.replace(1, 4, "222");
System.out.println(str); //输出:d222efghijk
}
}
比较 StringBuffer 和 String
public class StringBufferTest {
/**
* 测试StringBuffer
* 把0-9999 整数连接成一个字符串;
* 类似效果123456789101112....9999;
*
* 分析:1. 动态获取10000个值,使用for循环
* 2. 定义一个空字符串str
* 3. 循环拼接
*/
@Test
public void testStringBuffer() {
long begin1 = System.currentTimeMillis();
String str1 = "";
for (int i = 0; i < 10000; i++) {
str1 += i;
}
System.out.println(str1);
long end1 = System.currentTimeMillis();
System.out.println(end1 - begin1); //输出: 974 花费时间多,因为要创建 10000 个对象。 System.out.println("========="); //使用stringBuffer 只创建一个对象
long begin2 = System.currentTimeMillis();
StringBuffer str2 = new StringBuffer();
for (int i = 0; i < 10000; i++) {
str2.append(i);
}
System.out.println(str2);
long end2 = System.currentTimeMillis();
System.out.println(end2 - begin2); //输出: 11 时间花费非常少
}
}
StringBuilder类
小结:
1、String:不可变得字符串;
2、StringBuffer:可变的,是线程安全;
3、StringBuilder:可变的,是线程不安全,比StringBuffer性能高点;
4、String,StringBuffer,StringBuilder 都表示字符序列,接口都是CharSequence;
5、StringBuffer,StringBuilder中有很多和String中一样的方法;
特有的方法: 主要是可以改变对象值中的方法(添加,删除,插入,替换,反转)