包装类
**含义:**包装类是将基本数据类型封装成一个类,包含属性和方法。在使用过程中,会包括自动装箱和自动拆箱。
装箱: 将基本数据类型转换成包装类 例:Integer i2 = Integer.valueOf(a);//手动装箱
拆箱: 将包装类转换成基本数据类型例:int i3 = i.intValue();//手动拆箱
注: https://blog.youkuaiyun.com/wangyang1354/article/details/52623703
String
注意:常量池在1.7之后放置在了堆空间之中
字符串的使用:
1、创建
String str = "abc";
String str2 = new String("abc");
两种方式都可以用,只不过第一种使用比较多
2、字符串的本质
字符串的本质是字符数组或者叫做字符序列
String类使用final修饰,不可以被继承
使用equals方法比较的是字符数组的每一个位置的值
String是一个不可变对象
String.hashCode();
/**
* Returns a hash code for this string. The hash code for a
* {@code String} object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using {@code int} arithmetic, where {@code s[i]} is the
* <i>i</i>th character of the string, {@code n} is the length of
* the string, and {@code ^} indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* @return a hash code value for this object.
*/
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
hash = h = isLatin1() ? StringLatin1.hashCode(value)
: StringUTF16.hashCode(value);
}
return h;
}
这里h= h*31+val[i],31的二进制是11111,在进行乘法运算的时候可以进行移位计算操作,效率高
String中一些常用的方法
- char charAt(int index):返回字符串中第index个字符
- boolean equals(String other):比较字符串与other值是否相等
- boolean equalsIgnoreCase(String other):比较时忽略大小写
- int indexOf(String str):查找str的索引
- int lastIndexOf(String str,int idx):倒着查找str的索引(匹配方式是正向的)。如果有idx,表示从idx位开始倒着查找str
- int length():返回字符串长度
- String replace(char oldChar,char newChar):返回一个新串,它是由通过用newChar替换此字符串中出现的所有oldChar而生成的
- boolean startsWith(String prefix):如果字符串以prefix开始,则返回true
- boolean endsWith(String prefix):如果字符串以prefix结束,则返回true
- String substring(int beginindex[int endindex]):返回index开始到结尾[endindex]的子串。注;两个参数截取时是左闭右开的
- String toLowerCase();返回一个新字符串,将所有大写字母变为小写字母
- String toUpperCase():返回一个新字符串,将所有小写字母变成大写字母
- String trim():返回一个新字符串,将前后空格删除
- String split():