字符串
String
package note;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class StringNote {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "abc";
String str1 = new String("abc1");
String str2 = new String(new byte[]{'a', 'b', 99, '2'});
String str3 = new String(new byte[]{'a', 'b', 99, '3'}, "gbk");
String str4 = new String(new byte[]{'a', 'b', 99, '4','5','6','7'},1,4);
String str5 = new String(new char[]{'a', 'b', 99, '4'});
String str6 = new String(new char[]{'a', 'b', 99, '4','5','6','7'},1,4);
System.out.println("str = " + str);
System.out.println("str1 = " + str1);
System.out.println("str2 = " + str2);
System.out.println("str3 = " + str3);
System.out.println("str4 = " + str4);
System.out.println("str5 = " + str5);
System.out.println("str6 = " + str6);
System.out.println(String.format("这个double数的值是:%-8.2f和%-,3.1f的值",3.1415926,31415.926));
System.out.println(String.format("int数值是:%0,13d",1234567));
System.out.println("字符串的方法");
System.out.println("str.charAt(2) = " + str.charAt(2));
System.out.println("str.compareTo(str1) = " + str.compareTo("abc11"));
System.out.println("str.concat(\"concat\") = " + str.concat("concat"));
System.out.println("\"contains\".contains(\"ta\") = " + "contains".contains("ta"));
System.out.println("\"startWith\".startsWith(\"star\") = " + "startWith".startsWith("star"));
System.out.println("\"endsWith\".endsWith(\"with\") = " + "endsWith".endsWith("with"));
System.out.println("\"abc\".equals(\"str\") = " + "abc".equals("ABC"));
System.out.println("\"abc\".equalsIgnoreCase(\"ABC\") = " + "abc".equalsIgnoreCase("ABC"));
byte[] bytes = "getBytes".getBytes();
byte[] bytes1 = "getBytes".getBytes("utf-8");
char[] chars = "toCharArray".toCharArray();
System.out.println("Arrays.toString(bytes) = " + Arrays.toString(bytes));
System.out.println("Arrays.toString(bytes1) = " + Arrays.toString(bytes1));
System.out.println("chars = " + Arrays.toString(chars));
System.out.println("\"indexOf\".indexOf('d') = " + "indexOf".indexOf('d'));
System.out.println("\"indexOf\".indexOf(\"ex\") = " + "indexOf".indexOf("ex"));
System.out.println("\"lastIndexOfOf\".lastIndexOf(\"Of\") = " + "lastIndexOfOf".lastIndexOf("Of"));
System.out.println("\"length\".length() = " + "length".length());
System.out.println("\"substring\".substring(2) = " + "substring".substring(2));
System.out.println("\"substring\".substring(3,6) = " + "substring".substring(3, 6));
System.out.println("\"toLowerCase\".toLowerCase() = " + "toLowerCase".toLowerCase());
System.out.println("\"toUpperCase\".toUpperCase() = " + "toUpperCase".toUpperCase());
System.out.println("\" trim \".trim() = " + " trim ".trim());
System.out.println("\"java\".replace('j','s') = " + "java".replace('j', 's'));
System.out.println("\"java\".replace(\"ja\",\"ab\") = " + "java".replace("ja", "ab"));
System.out.println("Arrays.toString(\"H,ell,o\".split(\",\")) = " + Arrays.toString("H,ell,o".split(",")));
System.out.println("Sting面试题");
String s = "abcd";
String s2 = "a" + "b" + "c" + "d";
String s3 = "ab" + "cd";
String s4 = new String("abcd");
String temp = "ab";
String s5 = temp + "cd";
String s6 = getAb() + "cd";
System.out.println(s == s2);
System.out.println(s == s3);
System.out.println(s == s4);
System.out.println(s == s5);
System.out.println(s == s6);
System.out.println(s5 == s6);
}
private static String getAb() {
return "ab";
}
}

StringBuilder
- StringBuffer 与 StringBuilder 都继承自 AbstractStringBuilder,它们最大的区别就在于 StringBuffer 是线程安全的,而 StringBuilder 不是。因为在 StringBuffer 所有的方法上都有 synchronized 修饰
package note;
public class StringBuilderNote {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
StringBuilder stringBuilder1 = new StringBuilder(new String("str"));
StringBuilder stringBuilder2 = new StringBuilder(5);
System.out.println("stringBuilder.capacity() = " + stringBuilder.capacity());
System.out.println("stringBuilder1.capacity() = " + stringBuilder1.capacity());
System.out.println("stringBuilder2.capacity() = " + stringBuilder2.capacity());
stringBuilder.append(1).append("abc").append('a').append(9.8).append(new Object());
System.out.println("stringBuilder.capacity() = " + stringBuilder.capacity());
System.out.println("stringBuilder.charAt(2) = " + stringBuilder.charAt(2));
System.out.println("stringBuilder = " + stringBuilder);
stringBuilder.delete(2,5);
stringBuilder.insert(2,"abc");
System.out.println("stringBuilder = " + stringBuilder);
System.out.println("stringBuilder.length() = " + stringBuilder.length());
System.out.println("stringBuilder.substring(3) = " + stringBuilder.substring(3));
System.out.println("stringBuilder.substring(4,6) = " + stringBuilder.substring(4, 6));
StringBuilder reverse = stringBuilder.reverse();
System.out.println("reverse.toString() = " + reverse.toString());
for (int i = 0; i < stringBuilder.length(); i++) {
if (stringBuilder.charAt(i) == 'a') {
stringBuilder.deleteCharAt(i--);
}
}
}
}
StringBuffer
package note;
public class StringBufferNote {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
StringBuffer stringBuffer1 = new StringBuffer(new String("stringBuffer"));
StringBuffer stringBuffer2 = new StringBuffer(4);
stringBuffer.append("append");
System.out.println("stringBuffer.length() = " + stringBuffer.length());
System.out.println("stringBuffer.capacity() = " + stringBuffer.capacity());
stringBuffer.delete(1,4);
stringBuffer.insert(1,"insert");
System.out.println("stringBuffer = " + stringBuffer);
}
}
三者的区别
- 都是基于 char 数组实现的
- String 为 final 修饰的 char 数组,因此为不可变的字符串,String 存储在常量池里面
- StringBuilder、StringBuffer 为可变字符串,而且操作都是在数组上,数组的默认长度为 16(即为容量为 16),当容量不够是扩容到原来的 2 倍 +2
- StringBuffer 线程安全、StringBuilder 线程不安全
- 三者效率为:StringBuilder>StringBuffer>String
Object
equals,hashcode
package note;
public class ObjectNote {
public static void main(String[] args) {
String str = "java";
String str1 = new String("java");
System.out.println("str.equals(str1) = " + str.equals(str1));
System.out.println("str == str1 = " + (str == str1));
System.out.println("\"Aa\".hashCode() = " + "Aa".hashCode());
System.out.println("\"BB\".hashCode() = " + "BB".hashCode());
System.out.println("new ObjectSon() = " + new ObjectSon());
}
}
class ObjectSon {
@Override
public String toString() {
return "重写Object的toString方法,通常用来输出该对象的属性信息";
}
}
clone
package note;
public class CloneNote {
public static void main(String[] args) throws CloneNotSupportedException {
Person noby = new Person("noby", new Dog("lucky", new Toy("ball")));
System.out.println(noby);
Person kace = noby.clone();
System.out.println(kace);
Person july = noby;
System.out.println(july);
}
}
class Person implements Cloneable {
String name;
Dog dog;
public Person(String name, Dog dog) {
this.name = name;
this.dog = dog;
}
@Override
public Person clone() throws CloneNotSupportedException {
Person newperson = (Person)super.clone();
newperson.dog = dog.clone();
return newperson;
}
@Override
public String toString() {
return "Person{" +
"personhashcode" + this.hashCode() +
"name='" + name + '\'' +
", dog=" + dog +
'}';
}
}
class Dog implements Cloneable{
String name;
Toy toy;
public Dog(String name, Toy toy) {
this.name = name;
this.toy = toy;
}
@Override
public Dog clone() throws CloneNotSupportedException {
Dog newdog = (Dog)super.clone();
newdog.toy = toy.clone();
return newdog;
}
@Override
public String toString() {
return "Dog{" +
"doghashcode" + this.hashCode() +
"name='" + name + '\'' +
", toy=" + toy +
'}';
}
}
class Toy implements Cloneable{
String name;
public Toy(String name) {
this.name = name;
}
@Override
public Toy clone() throws CloneNotSupportedException {
return (Toy) super.clone();
}
@Override
public String toString() {
return "Toy{" +
"toyhashcode" + this.hashCode() +
"name='" + name + '\'' +
'}';
}
}