目录
通过以下编码可以看出,字符串实际就是字符数组。
char chars[]={'a','b','c'};
String s=new String(chars);
System.out.println(s);
int len=s.length();
System.out.println(len);
字符串转成byte数组
String s = "Hello world";
byte[] bytes = s.getBytes();
for (byte b : bytes) {
System.out.print((char)b);
}
两者看似都是创建了一个字符串对象,但在内存中确是各有各的想法。
String str1= “abc”; 在编译期,JVM会去常量池来查找是否存在“abc”,如果不存在,就在常量池中开辟一个空间来存储“abc”;如果存在,就不用新开辟空间。然后在栈内存中开辟一个名字为str1的空间,来存储“abc”在常量池中的地址值。
String str2 = new String("abc") ;在编译阶段JVM先去常量池中查找是否存在“abc”,如果过不存在,则在常量池中开辟一个空间存储“abc”。
在运行时期,通过String类的构造器在堆内存中new了一个空间,然后将String池中的“abc”复制一份存放到该堆空间中,在栈中开辟名字为str2的空间,存放堆中new出来的这个String对象的地址值。
也就是说,前者在初始化的时候可能创建了一个对象,也可能一个对象也没有创建;后者因为new关键字,至少在内存中创建了一个对象,也有可能是两个对象。
String类 是final修饰的,不可以被继承。
String类的底层是基于char数组的。
String类被设计成不可变(immutable)类,所以它的所有对象都是不可变对象。例如: String str = “hello"; str = str + "world“; 所以当上文str指向了一个String对象(内容为“hello”),然后对str进行“+”操作,str原来指向的对象并没有变,而是str又指向了另外一个对象(“hello world”),原来的对象还在内存中。 由此也可以看出,频繁的对String对象进行修改,会造成很大的内存开销。此时应该用StringBuffer或StringBuilder来代替String。
而new String()更加不适合,因为每一次创建对象都会调用构造器在堆中产生新的对象,性能低下且内存更加浪费。
String s0="abc",s1="abc" ; if(s0==s1){} 结果为true 因为在java中字符串的值是不可改变的,相同的字符串在内存中只会存一份,所以a和b指向的是同一个对象。
String s2=new String("abc"); String s3=new String("abc"); if(s2==s3){} 结果为false,此时a和b指向不同的对象
String s2=new String("abc"); String s3=new String("abc"); if(a.equals(b)){} 结果为true
package com.item.action;
public class Demo {
public static void main(String[] args) {
String s0="abc";
String s1="abc";
String s2=new String("abc");
String s3=new String("abc");
System.out.println(s0==s1);
System.out.println(s1==s2);
System.out.println(s2==s3);
System.out.println(s0.hashCode());
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
System.out.println(s0.equals(s1));
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
}
}
输出结果:
length()//取得字符串的长度 substring()//字符串截取 concat() //连接两个字符串 replace()//替换 trim()//去掉起始和结尾的空格 valueOf()//转换为字符串 toLowerCase()//转换为小写 toUpperCase()//转换为大写 toCharArray()//转char数组 equals()//比较两个字符串区分大小写 equalsIgnoreCase()//比较两个字符串不区分大小写 indexOf()//查找字符或者子串第一次出现的地方 lastIndexOf()//查找字符或者子串是后一次出现的地方 split()//字符串分割
public class Demo {
public static void main(String[] args) {
String str="I HAVE A DREAM!";
String s = str.substring(2, 2+4);
System.out.println(s);
}
}
public class Demo {
public static void main(String[] args) {
String str="I HAVE A DREAM!";
String s = str.replace("DREAM", "GOOD IDEA");
System.out.println(s);
}
}
public class Demo {
public static void main(String[] args) {
String str="\tI HAVE A DREAM!\t";
String s = str.trim();
System.out.println(s);
}
}
public class Demo {
public static void main(String[] args) {
String str = "89dsa dady8)ILuhd9usa)(*YGIUhdusa hoi";
char[] array = str.toCharArray();
int low = 0;
int up = 0;
int num = 0;
int other = 0;
for (char c : array) {
if (c >= 'a' && c <= 'z') {
low++;
} else if (c >= 'A' && c <= 'Z') {
up++;
} else if (c >= '0' && c <= '9') {
num++;
} else {
other++;
}
}
System.out.println(str.length());
System.out.println(low);
System.out.println(up);
System.out.println(num);
System.out.println(other);
}
}
public class Demo {
public static void main(String[] args) {
String str="I HAVE A DREAM!";
String lowerCase = str.toLowerCase();
System.out.println(lowerCase);
String upperCase = str.toUpperCase();
System.out.println(upperCase);
}
}
import java.util.UUID;
public class Demo {
public static void main(String[] args) {
String str = UUID.randomUUID().toString().replaceAll("-", "");
String fileName = str.concat(".jpg");
int indexOf = fileName.indexOf(".jpg");
System.out.println(indexOf);
System.out.println(fileName.substring(indexOf,fileName.length()));
}
}
import java.util.UUID;
public class Demo {
public static void main(String[] args) {
String str = UUID.randomUUID().toString();
System.out.println(str);
String[] split = str.split("-",str.length());
for (String string : split) {
System.out.println(string);
}
}
}
后续会有对应的联系题目。