常用类,String类API,Date时间API
- String类的一些API
- 日期类型的
- Java比较器
- System类
- Math类
- BigInteger与BigDecimal
String类
- String被声明为final,不可被继承
- String实现了Serializable接口,表示字符串支持序列化,实现了Comparable接口,支持字符串比较
- String类内部定义了final char[] value用于存储字符串数据
- String有不可变性,当一个字符串给一个引用重新赋值时,需要重新指向内存区域去赋值,不能使用原有的value进行赋值
- 修改字符串时,value也需要重新赋值
- 当有字符串拼接时,value也是需要重新赋值
String s1 = "abc";
String s2 = "abc";
//当两个引用指向内容相同的字符串时,引用地址相同,通过""穿件的字符串序列会直接存放在常量池中
System.out.println(s1 == s2);//true
/**
* 如果s1 = "Hello"; 给已有的字符串重新赋值,字符序列会指向新的字符串引用
* System.out.println(s1 == s2); //false
* System.out.println(s1.hashCode()); //69609650
* System.out.println(s2.hashCode()); //96354
*/
- 通过字面量的方式(区别于new)给一个字符串赋值,此时字符串会声明在字符串常量池中
字符串常量池中是不会存储相同内容的字符串
String s3 = s1.replace("a","b");
System.out.println(s1);//abc
System.out.println(s3);//bbc
System.out.println(s1.hashCode()); //96354
System.out.println(s3.hashCode()); //97315 更改后的字符串序列会重新指向一个引用地址
-
String 创建对象
- String str = “Hello”;直接创建一个字符序列
- String str = new String(); 通过构造器,这个相当于this.value = new char[0];
- String str = new String(String original);
- String str = new String(char[] a); 相当于Arrays.copyOf(value, value.length);
- String str = new String(char[] a, int startIndex, int count);
-
String=“”和String str = new String("")的区别?
- ""是在常量池中创建字符串,目的是共享
- new String("")是在堆区创建字符串对象,而且这样创建方式会在常量池中也创建一次,也就是创建了两次字符串
String s4 = new String("Hello");
String s5 = "Hello";
System.out.println(s4.hashCode());//69609650
System.out.println(s5.hashCode());//69609650
String s6 = new String("Hello");
System.out.println(s6.hashCode());//69609650
/**
这是因为字符串首先会在常量池中创建一个Hello,然后因为之前已经有Hello了,地址值便指向相同,由因为这样new出来的字符串对象在堆区也是指向相同的引用,所以还是同一个对象
*/
Person p = new Person("Tom",20);
Person p2 = new Person("Tom",20);
System.out.println(p);//com.edu.hy.test04.Person@4554617c
System.out.println(p2);//com.edu.hy.test04.Person@74a14482
System.out.println(p.getName().equals(p2.getName()));//true
System.out.println(p.getName().hashCode());//84274
System.out.println(p2.getName().hashCode());//84274
- 字符串拼接
- “xxx”+“xxx”:得到的结果和""结果一样
- “xxx”+常量:这个操作会在堆区完成,因此它得到的结果是堆对象的地址
String aString = "HelloWorld";
String a1 = "Hello";
String a2 = "World";
String a3 = "Hello" + "World";
String a4 = a1 + "World";
String a5 = "Hello" + a2;
String a6 = a1 + a2;
System.out.println(aString == a3);//true
System.out.println(aString == a4);//false
System.out.println(aString == a5);//false
System.out.println(aString == a6);//false
System.out.println(a4 == a6);//false
- 实例:顺便理一下值传递
public class StringTest {
String s = new String("Hello");
char[] arr = {'W','o','r','l','d'};
Integer a = 15;//涉及到了自动拆箱
Integer a2 = new Integer(15);
Person p = new Person("Hello",22);
public void change(String s2, char[] arr2){
s2 = "Good";
arr2[0] = 'H';
}
public void change(Integer a2){
a2 = 20;
Integer a3 = a2;
}
public void change(Person p){
p.setName("Hello World");
}
public static void main(String[] args) {
StringTest st = new StringTest();
st.change(st.s,st.arr);
st.change(st.a);
st.change(st.a2);
st.change(st.p);
System.out.println(st.s);//Hello
System.out.println(st.arr);//Horld
System.out.println(st.a);//15
System.out.println(st.a2);//15
System.out.println(st.p.getName());//这里就体现出了值传递中,传递的是引用类型时的区别,传递的是引用的地址值
}
}
String类常用API
- char charAt(int index) :返回 char指定索引处的值。
- String concat(String str) :连接两个字符串相当于加号+
- int compareTo(String anotherString) :比较两个字符串是否相等,返回的是ascii差值
- int compareToIgnoreCase(String str) :忽略大小写比较
- boolean isEmpty():判断字符串是否为空
- char[] toCharArray() :将此字符串转换为新的字符数组。
- String toLowerCase() :把字符串全部转换成小写
- String toUpperCase() :大写
- String replace(char oldChar, char newChar) :替换字符串
- String trim() : 删除首位所有空格
- Boolean endsWith(String str) :以str结尾
- Boolean startsWith(String str) :以str开始
- Boolean startsWith(String str, int index) :以index处是否以str开始
- Boolean contains(String str) :字符串是否包含str字符
- int indexOf(String str) : 返回该str所在的索引,如果没有则返回-1
- String[] split(String regex) 以给定的正则表达式符号拆分字符串
- String[] split(String regex, int limit) 将这个字符串拆分为给定的 regular expression的匹配。
- Boolean matches("\d+") :“正则表达式” 这个表示字符串是否是数字 d数字 + 多个
- String substring(int beginIndex) :返回一个字符串,该字符串是此字符串的子字符串
- String substring(int beginIndex, int endIndex) :返回一个字符串,该字符串是此字符串的子字符串。
public class StringAPIDemo02 {
@Test
public void test(){
String s1 = " Hello World ";
System.out.println(s1.length());//13
System.out.println(s1.charAt(0));//空 不是null
System.out.println(s1.charAt(9));//r
//System.out.println(s1.charAt(30));//StringIndexOutOfBoundsException
String s2 = "";
System.out.println(s1.isEmpty());//false
System.out.println(s2.isEmpty());//true
System.out.println(s1.trim().length());//11 trim()去除首位空格
String s3 = "HelloWorld";
System.out.println(s3.toLowerCase());//helloworld去除字符串的所有大写格式,这句话相当于是s4 = s3.tiLowerCase()
String s4 = "helloworld";
System.out.println(s4.toUpperCase());//HELLOWORLD全部转成大写
System.out.println(s3.equals(s4));//false
System.out.println(s3.toLowerCase().equals(s4.toLowerCase()));//true
System.out.println(s1.toCharArray());// Hello World 把字符串转换成字符数组
char[] chars = s1.toCharArray();
for (char s : chars) {
System.out.print(s);// Hello World
}
System.out.println();
System.out.println(s3.equalsIgnoreCase(s4));//true
String s5 = "abc";
String s6 = s5.concat("ced");
System.out.println(s6);//concat(String str) 相当于+
String s7 = new String("adc");
System.out.println(s7.compareTo(s5));//b(ascii)-d(ascii)
/**
* 从第一个字符开始,比较两个字符的ascii码值
* - 如果第一个不相等返回两个ascii的差值
* - 如果第一个相等,取两个字符串的下一个字符的ascii做差
* - 直到遍历完毕,字符串相等返回0,不相等返回差值
*/
String s8 = "Java";
System.out.println(s8.substring(0));//Java 正在指向的字符不会减掉
System.out.println(s8.substring(1));//ava
System.out.println(s8.substring(1,2));//a左闭右开 [a,v)
String str = "Java";
System.out.println(str.endsWith("a"));//true 以a结尾
System.out.println(str.startsWith("J"));//true
System.out.println(str.startsWith("a",1));//true 从索引1处是不是由a开始
String string3 = "Hello|World|";
String[] strings = string3.split("\\|"); //以a拆分这个字符数组
for (String string : strings) {
System.out.print(string);
}
}
}
包装类怎么和基本数据类型转换?
- 字符串转换成基本数据类型
- 基本数据类型转换成字符串
字符串转换成int
String str = "123";
int num = Integer.parseInt(str);// num=123
基本类型转成String或者直接用+
String str = String.valueOf(num);//“123”;
String str = num + "";//这个存在对堆中
String转换成byte[]:编码
- String转成byte[]:也就是编码
- 调用String的getBytes(),getByte(“utf-8”);
- byte数组转换成字符串是解码
- String str = new String(byte[] bytes, Charset charset);
算法题
public class StringTest4 {
/**
* 统计"asdhfksdkjfksdfsasdfsffdff"中"df"出现的次数
*/
public static int reverse(String fuStr, String ziStr){
int fuStrLength = fuStr.length();
int ziStrLength = ziStr.length();
int count = 0;
int index = 0;
if(fuStrLength >= ziStrLength){
while((index = fuStr.indexOf(ziStr,index)) != -1){
count++;
index += ziStrLength;
}
return count;
}else {
return 0;
}
}
public static void main(String[] args) {
int a = reverse("asdhfksdkjfksdfsasdfsffdff","df");
System.out.println(a);//3
}
}