文章目录
一、包装类
1、为什么需要包装类Wrapper Class)?
JAVA并不是纯面向对象的语言。Java语言是一个面向对象的语言,但是Java中的基本数据类型却
是不面向对象的。但是我们在实际使用中经常需要将基本数据转化成对象,便于操作。比如:
集合的操作中。 这时,我们就需要将基本类型数据转化成对象!
2、包装类和基本数据类型的对应关系:
基本数据类型 | 位数 | 默认值 | 取值范围 | 包装类 |
---|---|---|---|---|
byte (位) | 8 | 0 | -2^7 - 2^7-1 | Byte |
short (短整数) | 16 | 0 | -2^15 - 2^15-1 | Short |
int(整数) | 32 | 0 | -2^31 - 2^31-1 | Integer |
long(长整数) | 64 | 0 | -2^63 - 2^63-1 | Long |
float(单精度) | 32 | 0.0 | -2^31 - 2^31-1 | Float |
double(双精度) | 64 | 0.0 | -2^63 - 2^63-1 | Double |
char(字符) | 16 | 空 | 0 - 2^16-1 | Character |
boolean(布尔值) | 8 | false | true、false | Boolean |
3、 类型转换——以Integer为例
//1、 int类型转为Integer类型
Integer integer=new Integer(3);
Integer integer1 = Integer.valueOf(10);
//2、Inetegr类型转为int类型,调用的是Integer的抽象父类Number中的方法
/**
* public abstract int intValue();
* public abstract long longValue ();
* public abstract float floatValue ();
* public abstract double doubleValue ();
* 、、、、、省略以下方法
*/
int i = integer.intValue();
double v = integer.doubleValue();
//3、字符串转为Integer类型
Integer integer2 = new Integer("3333"); //底层调用Integer.parseInt("xxx")方法
/**
* public static int parseInt(String s) throws NumberFormatException {
* return parseInt(s,10);
* }
*/
// 此方法返回值类型是int,通过自动装箱转化为Integer类型
Integer i1 = Integer.parseInt("1111");
//4、integer转换为string类型
String string = integer.toString();
//5、常见的常量
System.out.println("int类型最大的整数:"+Integer.MAX_VALUE);
//6、其他类型类似,不在记录
4、 自动装箱和拆箱——Integer为例
//1、声明一个int类型整数
int a=3;
//2、自动装箱
Integer b=a;
// 实际上是,编译器编译时为我们添加了Integer.valueOf(a)方法
Integer integer = Integer.valueOf(a);
//3、自动拆箱
int c=b;
// 实际上是,编译器编译时为我们添加了b.intValue()方法
int i = b.intValue();
//4、缓存
/**
* 缓存[-128,127]之间的数字。实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组。
* 当我们调用valueOf()的时候,首先检查是否在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出已经建好的对象
* 如果不在这个范围,则创建新的Integer对象。
*/
Integer in1 = Integer.valueOf(-128);
Integer in2 = -128;
System.out.println(in1 == in2);//true 因为123在缓存范围内
System.out.println(in1.equals(in2));//true
System.out.println("################");
Integer in3 = 1234;
Integer in4 = 1234;
System.out.println(in3 == in4);//false 因为1234不在缓存范围内
System.out.println(in3.equals(in4));//true
Integer缓存源码解析:
二、String类
1、String源码分析
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
private final char value[];
private int hash; // Default to 0
private static final long serialVersionUID = -6849794470754667710L;
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
public String() {
this.value = "".value;
}
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
..................
..................
}
从上面可以看出几点:
1)、 String类是final类,也即意味着String类不能被继承,并且它的成员方法都默认为final方法。在Java中,被 final修饰的类是不允许被继承的,并且该类中的成员方法都默认为final方法。
2)、 面列举出了String类中所有的成员属性,从上面可以看出String类其实是通过char数组来保存字符串的。
3)、 String类是不可变,String对象一旦被创建就是固定不变的了,对String对象的任何改变都不影响到原对 象,相关的任何change操作都会生成新的对象”
2、常见构造方法
-
public String():空构造
String s1=new String(); s1="李思思"; System.out.println("s1 = " + s1);
-
public String(byte[] bytes):把字节数组转成字符串
byte [] bytes = {22, 82,36}; String s3=new String(bytes); System.out.println("s3 = " + s3);
-
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
byte [] bytes = {22, 82,36}; String s4=new String(bytes,0,10); System.out.println("s4 = " + s4);
-
public String(char[] value):把字符数组转成字符串
char[] c={'a','b','c','d'}; String s5=new String(c); System.out.println("s5 = " + s5);
-
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
char[] c={'a','b','c','d'}; String s6=new String(c,0,2); System.out.println("s6 = " + s6);
-
public String(String original):把字符串常量值转成字符串
String s2=new String("张三"); System.out.println("s2 = " + s2); final String CL="lisi"; //常量 String s7=new String(CL); System.out.println("s7 = " + s7);
3、String类的常见面试题
-
下面这句话在内存中创建了几个对象?
String s1 = new String(“abc”)
//String s1 = new String("abc"); 创建了两个对象,一个是abc,一个是指向"abc"的引用对象s1; String s1 = new String("abc"); System.out.println("s1 = " + s1);
-
判断定义为String类型的s1和s2是否相等
//类型1,s1与s2的比较 /** * String s1 = "abc"; 产生了一个对象 abc; * String s2 = "abc"; 也产生了一个对象 abc; * 但是,他们实际只是创建了1个对象,都是指向同一引用。 * 因为两个变量都指向同1个对象引用,都是放在常量池(缓冲池、文字池)里的。所以不管是==还是 equals结果都为true。 */ String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2);//true System.out.println(s1.equals(s2));//true //类型2,s1与s2的比较 String s1 = "ab"; String s2 = "abc"; String s3 = s1 + "c"; System.out.println(s3 == s2);//false System.out.println(s3.equals(s2));//true //类型3,s1与s2的比较 /** * String s1 = "abc"; 产生了一个对象 abc; * String s2 = "abc"; 也产生了一个对象 abc; * 但是,他们实际只是创建了1个对象,都是指向同一引用。 * 因为两个变量都指向同1个对象引用,都是放在常量池(缓冲池、文字池)里的。所以不管是==还是 equals结果都为true。 */ String s1 = "a" + "b" + "c"; String s2 = "abc"; System.out.println(s1 == s2); //true System.out.println(s1.equals(s2));//true //类型4,s1与s2的比较 String s1 = new String("abc"); String s2 = "abc"; System.out.println(s1 == s2); //false System.out.println(s1.equals(s2)); //true
4、String类的判断功能
-
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
String s1="zhangsna"; String s2="zhangsan"; String s3="zhangSan"; System.out.println(s1.equals(s2));//equals比较字符串的内容是否相同,区分大小写,true System.out.println(s1.equals(s3));//false
-
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
String s1="zhangsna"; String s2="zhangsan"; String s3="zhangSan"; System.out.println(s1.equals(s2));//equals比较字符串的内容是否相同,区分大小写,true System.out.println(s1.equals(s3));//false System.out.println(s1.equals(s3));//false System.out.println(s1.equalsIgnoreCase(s3));//equalsIgnoreCase比较字符串的内容是否相 同,忽略大小写,true
-
boolean contains(String str):判断大字符串中是否包含小字符串
String s1="张三三"; String s2="张三"; String s3="我是王五"; String s4="王五"; String s5="王"; //contains断大字符串中是否包含小字符串 System.out.println(s1.contains(s2));//true System.out.println(s3.contains(s4));//true
-
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
String s1="王张三三"; String s2="张三"; String s3="我是王五"; String s4="王五"; String s5="王"; //startsWith判断字符串是否以某个指定的字符串开头 System.out.println(s1.startsWith(s2));//false System.out.println(s1.startsWith(s5));//true
-
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
String s1="王张三三"; String s2="张三"; String s3="我是王五"; String s4="王五"; String s5="王"; //endsWith判断字符串是否以某个指定的字符串结尾 System.out.println(s1.endsWith(s5));//false System.out.println(s1.endsWith(s2));//true
-
boolean isEmpty():判断字符串是否为空。
String s1="lsis"; String s2=""; String s3=null; System.out.println(s1.isEmpty());//false System.out.println(s2.isEmpty());//true System.out.println(s3.isEmpty()); //错误异常 java.lang.NullPointerException
5、String类的获取功能
- int length():获取字符串的长度。
String s1="woshizhansgan"; //length()是一个方法,获取的是每一个字符的个数
String s2="zhansgan科二一定过";
System.out.println(s2.length());
System.out.println(s1.length());
- char charAt(int index):获取指定索引位置的字符。
String s1="zhangsan";
System.out.println(s1.charAt(2)); //获取指定索引位置的字符
System.out.println(s1.charAt(5)); //越界,异常错误
- int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
String s1="woshizhangsan,zhangsan";
System.out.println(s1.indexOf('h')); // indexOf(int ch)参数接收的是int类型 的,传递char类型的会自动提升
System.out.println(s1.indexOf('s')); //如果不存在返回就是-1
- int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
String s1="woshizhangsan,zhangsan";
System.out.println(s1.indexOf("zh")); //indexOf(String str) 获取字符串中第一个 字符出现的位置
System.out.println(s1.indexOf("sa")); //如果不存在返回就是-1
- int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
String s1="woshizhangsan,zhangsan";
System.out.println(s1.indexOf('h', 4)); //indexOf(int 要查找的字符,int 从哪个位 置开始):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
System.out.println(s1.indexOf('i', 3)); //从指定位置开始向后找
- int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
String s1="woshizhangsan,zhangsan";
System.out.println(s1.indexOf("sa", 9)); //indexOf(int 要查找的字符串,int 从哪个 位置开始):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
System.out.println(s1.indexOf("ng", 3)); //从指定位置开始向后找
- int lastIndexOf(int ch,int fromIndex): 从后向前找,第一次出现的字符。
String s1="woshizhangsan,zhangsan";
System.out.println(s1.lastIndexOf('z')); //lastIndexOf 从后向前找,第一次出现的字符
- int lastIndexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后最后一次出现处的索引。
String s1="woshizhangsan,zhangsan";
System.out.println(s1.lastIndexOf('m',13)); //从指定位置开始向前找
- String substring(int start):从指定位置开始截取字符串,默认到末尾。
String s1="woshizhangsan,zhangsan";
System.out.println(s1.substring(5)); //substring(int start):从指定位置开始截取字 符串,默认到末尾。
- String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
String s1="woshizhangsan,zhangsan";
System.out.println(s1.substring(3, 9)); //包含头,不包含尾,左闭右开 substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
6、String的转换功能:
-
byte[] getBytes():把字符串转换为字节数组。
String s1="woshiyipizhnagsan"; byte []arr=s1.getBytes(); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } System.out.println(); String s2="降龙十八掌"; byte []arr1=s2.getBytes(); //通过gbk码表将字符串转换成字节数组 for (int i = 0; i < arr1.length; i++) { System.out.print(arr1[i]+" "); //编码:把我们看的懂转换为计算机看的懂得 } System.out.println(); String s3 = "琲"; byte[] arr3 = s3.getBytes(); for (int i = 0; i < arr3.length; i++) {//gbk码表一个中文代表两个字节 System.out.print(arr3[i] + " "); }
-
char[] toCharArray():把字符串转换为字符数组。
String s1="woshiyipizhangsan"; char [] t=s1.toCharArray(); for (int i = 0; i < s1.length(); i++) { System.out.print(t[i]+" "); } System.out.println(); String s2="降龙十八掌"; char [] t1=s2.toCharArray(); for (int i = 0; i < s2.length(); i++) { System.out.print(t1[i]+" / "); } System.out.println(); String s = "heima"; char[] arr = s.toCharArray(); //将字符串转换为字符数组 for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); }
-
static String valueOf(char[] chs):把字符数组转成字符串。
-
static String valueOf(int i):把int类型的数据转成字符串。
注意:String类的valueOf方法可以把任意类型的数据转成字符串
char[] arr={'a','a','a','a','a'};
String str=String.valueOf(arr); //底层调用了string的构造方法 :String(char value[])
System.out.println(str);
String str2=String.valueOf(100); //将100转换为字符串
System.out.println(str2+100);
Person p=new Person("张三",15);
String str3=String.valueOf(p);
System.out.println(p); //地址
System.out.println(str3); //地址/ valueOf调用了object中的tostring方法
-
String toLowerCase():把字符串转成小写。(了解)
-
String toUpperCase():把字符串转成大写。
-
String concat(String str):把字符串拼接。
String s1="ZHANGsan"; String s2="zhangSAN"; System.out.println(s1.toLowerCase()); System.out.println(s2.toUpperCase()); System.out.println(s1+s2); //用+拼接字符串更强大,可以用字符串与任意类型相加 System.out.println(s1.concat(s2)); //concat方法调用的和传入的都必须是字符串
7、String类的其他功能
-
String的替换功能及案例演示
String s1="zhangsan"; //String replace(char old,char new) System.out.println(s1.replace('z', 'o')); //字符替换 //String replace(String old,String new) System.out.println(s1.replace("sa", "吗吗"));//字符串替换
-
String的去除字符串两端的空格及案例演示
//String trim () String s1=" 站上 "; System.out.println(s1.trim());// trim() 去除字符串两端的空格
-
String的按字典顺序比较两个字符串及案例演示
String s1="zhangsan";
String s2="deima";
String s3="deama";
String s4="ZHANGSAN";
//int compareTo(String str) 区分大小写
System.out.println(s1.compareTo(s2));
System.out.println(s2.compareTo(s3));//按照码表值比较,查找的是unicode码表值
System.out.println(s1.compareTo(s4));//compareTo 区分大小写
//int compareToIgnoreCase(String str 不区分大小写
System.out.println(s1.compareToIgnoreCase(s4));//compareToIgnoreCase 不区分大小写