public static void main(String[] args) throws Exception { String s=new String("jia"); String s2=s.concat("jun"); System.out.println(s); StringBuffer sb=new StringBuffer("jia"); sb.append("jun"); System.out.println(sb); } 輸出jia和jiajun
public static void main(String[] args) { String s1="jiajun"; String s2=s1; s1="666"; System.out.println(s1); } 輸出:666
String s3="jiajun"; System.out.println(s2==s3); 輸出:true
public static void main(String[] args) { String s1="jiajun"; s1=s1.replace("j","J"); System.out.println(s1); s1=s1.toLowerCase(); System.out.println(s1); } JiaJun jiajun
2047 public String More ...replace(char oldChar, char newChar) { 2048 if (oldChar != newChar) { ... 2069 return new String(0, len, buf); 2070 } 2071 } 2072 return this; 2073 }
111 public final class String 112 implements java.io.Serializable, Comparable<String>, CharSequence { The value is used for character storage. 113 114 private final char value[]; Cache the hash code for the string 116 117 private int hash; // Default to 0 118 private static final long serialVersionUID = -6849794470754667710L; 136 137 public String() { 138 this.value = new char[0]; 139 } 151 public String(String original) { 152 this.value = original.value; 153 this.hash = original.hash; 154 } 1913 public String substring(int beginIndex) { 1914 if (beginIndex < 0) { 1915 throw new StringIndexOutOfBoundsException(beginIndex); 1916 } 1917 int subLen = value.length - beginIndex; 1918 if (subLen < 0) { 1919 throw new StringIndexOutOfBoundsException(subLen); 1920 } 1921 return (beginIndex == 0) ? this : new String(value, beginIndex, subLen); 1922 }
String s1="jiajun"; String s2="jiajun"; System.out.println(s1==s2);
線程安全,出現線程安全的是在對共享變量寫的時候,而由於不可變,因此Strig是線程安全的html
最重要的是安全,若是當一個String已經傳給別人了,這個時候若是是可變,那麼能夠在後面進行修改,那麼這是麻煩並不安全的。並且在hashmap中,若是做爲key的String s1是可變的,那麼這樣是很危險的,好比說可能出現兩個一樣的鍵。java
public static void main(String[] args) throws Exception { String s1="jiajun"; Field field=String.class.getDeclaredField("value"); field.setAccessible(true); char [] value=(char[])field.get(s1); value[0]='Jiajun';
做者:jiajun 出處: http://www.cnblogs.com/-new/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。若是以爲還有幫助的話,能夠點一下右下角的【推薦】,但願可以持續的爲你們帶來好的技術文章!想跟我一塊兒進步麼?那就【關注】我吧。算法