国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

? Java java?? ?? Java ??? ?? ?? ??

Java ??? ?? ?? ??

Feb 27, 2017 pm 03:25 PM

Java ??? ?? ?? ??

?? ??? ??????

?? ?? ?? ??? Java?? String ???? ??? ? ????. ???? ?? ??? ??? ?????? ??? ?? ??? ? ????. ??? ??? ? ??? ??? ? ??? ??? ?????. ??? ??? ? ????. ?, ?? ??? ??? ?? ??? ??? ? ??? ?? ??? ???? ??? ??? ??? ? ????. ?????.

??? ?? ?? ??

Java ???? ?? String? ?? ????? ?? ?? ???? ????. ?? ??? ?????.

String s = "ABCabc"; 
System.out.println("s = " + s); 
 
s = "123456"; 
System.out.println("s = " + s);

??? ??? ??? ????.

s = ABCabc
s = 123456

First String Object s? ??? ?? s? ?? "ABCabc"? ???? s? ?? "123456"?? ?????. ??? ??? ?? s ?? ??? ?????? ? ? ????. ??? ? ??? String ??? ????? ??? ???? ?? ???? ??? ????. s? ?? ??? ??? String ??? ?? ??? ????. ??? ??? ?? ??? ?????. ?? ??? ???? ? ??? ??? ???? ??? ????. ??? ???? ??? ??? ???? 4??? ??????. ? ??? ?? ??? ???? ? ????.

?, s? ?? ?? ??? ???? ??? ????. ? ??? ??? ? s="123456"; ? ?? ??? ?? "123456"? ???? ?? ??? ?? ?????. -?? ??? ???? ?? ?? "ABCabc"? ??? ???? ???? ???? ?????. ??? ??? ??? ????.

Java String源碼分析

Java? C++? ? ?? ???? Java??? ?? ??? ?? ???? ? ??? ????. ?? ???? ?? ??? ?? ???? ? ??? ?? ??? ? ??, ??? ?? ?? ??, ??? ??? ?? ?? ???? ?? ??? ????? ? ????? ???. C++?? ??, ??, ???? ? ??? ??? ?? ??? ???? ? ????. ??? Java? ??? ????? C++? ???? ?????. ? ? ???? ??? ??? ?? ????. ??? Java??? ??? ???? ?? ???????. ??? ??? C++? ????? ?????.

String ??? ? ??????

String? ???? ????? ?? String ???? ?? ??? ?????. JDK1.6?? String? ?? ???? ??? ?????.

public final class String 
  implements java.io.Serializable, Comparable<String>, CharSequence 
{ 
  /** The value is used for character storage. */ 
  private final char value[]; 
 
  /** The offset is the first index of the storage that is used. */ 
  private final int offset; 
 
  /** The count is the number of characters in the String. */ 
  private final int count; 
 
  /** Cache the hash code for the string */ 
  private int hash; // Default to 0

JDK1.7??? String ???? ?? ?????? ?? ??? ???????. ??? ? ?? ??? ???? ??? ? ??? ??? ??? ????. JDK1.7?? String ???? ?? ?? ??? ? ?? ????.

public final class String 
  implements java.io.Serializable, Comparable<String>, CharSequence { 
  /** The value is used for character storage. */ 
  private final char value[]; 
 
  /** Cache the hash code for the string */ 
  private int hash; // Default to 0

? ???? ? ? ??? ??? Java? String ???? ?? ??? ???? ????. JDK6?? value? String?? ???? ????, offset? ? ???? String? ?? ????, count? String? ???? ?? ????. JDK7?? ? ??? ??? ????. ?, value? ?? ??? String ??? ????. ? ?? ??? ? ??? ??? ??? ??? ????. ?? String ??? ?? ?? ???? ?? ?? ??? ????. ? ?? ??? ? ??? ???? ??? ????. Java??? ??? ?????(?? ??? Java ??? ??? ?????). ??? ?? ?? ?? ??? ???? ??? ????. ??? String s = "ABCabc"; ??? ??? ? ?? ??? ????? ??? ??? ???:


Java String源碼分析

value, offset ? count? ? ?? ??? ????? ??? ?? ???? ?? setValue, setOffset ? setCount? ?? ?? ???? ???? ???? String ??? ????? String? ??? ? ????. ?, ?? ????? ??? ? ??? ? ? ??? String ??? ???? ???? ? ????. ?? ? ?? ?? value, offset ? count? ?? ????? String ??? ??? ? ? ?? ?? ????? ??? ? ????. ??? String ??? ??? ? ?? ??? ??? ? ????.

??? String?? ??? ? ?? ???? ??, ?? ???? ??? ?? ?? ? ????. ??? ????? ?? ???, ???, ???All, toLowerCase ?? ?????. ?? ?? ?? ???

String a = "ABCabc"; 
System.out.println("a = " + a); 
a = a.replace(&#39;A&#39;, &#39;a&#39;); 
System.out.println("a = " + a);

??? ??? ??? ????.

a = ABCabc
a = aBCabc

那么a的值看似改變了,其實也是同樣的誤區(qū)。再次說明, a只是一個引用, 不是真正的字符串對象,在調用a.replace('A', 'a')時, 方法內部創(chuàng)建了一個新的String對象,并把這個心的對象重新賦給了引用a。String中replace方法的源碼可以說明問題:

Java String源碼分析

讀者可以自己查看其他方法,都是在方法內部重新創(chuàng)建新的String對象,并且返回這個新的對象,原來的對象是不會被改變的。這也是為什么像replace, substring,toLowerCase等方法都存在返回值的原因。也是為什么像下面這樣調用不會改變對象的值:

String ss = "123456"; 
 
System.out.println("ss = " + ss); 
 
ss.replace(&#39;1&#39;, &#39;0&#39;); 
 
System.out.println("ss = " + ss);

打印結果:

ss = 123456
ss = 123456

String對象真的不可變嗎?

從上文可知String的成員變量是private final 的,也就是初始化之后不可改變。那么在這幾個成員中, value比較特殊,因為他是一個引用變量,而不是真正的對象。value是final修飾的,也就是說final不能再指向其他數(shù)組對象,那么我能改變value指向的數(shù)組嗎? 比如將數(shù)組中的某個位置上的字符變?yōu)橄聞澗€“_”。 至少在我們自己寫的普通代碼中不能夠做到,因為我們根本不能夠訪問到這個value引用,更不能通過這個引用去修改數(shù)組。

那么用什么方式可以訪問私有成員呢? 沒錯,用反射, 可以反射出String對象中的value屬性, 進而改變通過獲得的value引用改變數(shù)組的結構。下面是實例代碼:

public static void testReflection() throws Exception { 
   
  //創(chuàng)建字符串"Hello World", 并賦給引用s 
  String s = "Hello World";  
   
  System.out.println("s = " + s); //Hello World 
   
  //獲取String類中的value字段 
  Field valueFieldOfString = String.class.getDeclaredField("value"); 
   
  //改變value屬性的訪問權限 
  valueFieldOfString.setAccessible(true); 
   
  //獲取s對象上的value屬性的值 
  char[] value = (char[]) valueFieldOfString.get(s); 
   
  //改變value所引用的數(shù)組中的第5個字符 
  value[5] = &#39;_&#39;; 
   
  System.out.println("s = " + s); //Hello_World 
}

打印結果為:

s = Hello World
s = Hello_World

在這個過程中,s始終引用的同一個String對象,但是再反射前后,這個String對象發(fā)生了變化, 也就是說,通過反射是可以修改所謂的“不可變”對象的。但是一般我們不這么做。這個反射的實例還可以說明一個問題:如果一個對象,他組合的其他對象的狀態(tài)是可以改變的,那么這個對象很可能不是不可變對象。例如一個Car對象,它組合了一個Wheel對象,雖然這個Wheel對象聲明成了private final 的,但是這個Wheel對象內部的狀態(tài)可以改變, 那么就不能很好的保證Car對象不可變。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

更多Java String源碼分析相關文章請關注PHP中文網!


? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

?? ????
1744
16
Cakephp ????
1596
56
??? ????
1537
28
PHP ????
1396
31
???
?? ?? ?? ??? ??? ?? ?? ?? ??? ??? Jun 24, 2025 pm 09:41 PM

?? ?? ?? ??? ??? ?? ??? ??, ? ? ?? ? ??? ?????. 1. ??? ?? ???? ?? ???? ???-????, ? ??? ??? ??? ? ????, Hashmap? ???-??? ?? ??? ??? ???? ????. 2. NULL ? ?? ???? HashMap? ??? NULL ?? ?? ? ?? ???? ?? HashTable? NULL ?? ?? ???? ??? NullPointerException? ?????. 3. ????? ??? ????? ?? ??? ?? ?? ? ????? HashTable? ? ??? ?? ?? ??? ????. ?? ConcurrenTashMap? ???? ?? ????.

?????? ?? ???? ?????? ?????? ?? ???? ?????? Jun 24, 2025 pm 10:57 PM

staticmethodsininterfaceswereIntRectionSelffacesswithinteffaceswithinteffaceswithintintinjava8toallowutilityFunctionswithinterfaceitswithinteffaceswithinterfaceffaces

JIT ????? ??? ??? ??????? JIT ????? ??? ??? ??????? Jun 24, 2025 pm 10:45 PM

JIT ????? ??? ???, ??? ?? ? ???, ?? ?? ? ???? ? ? ?? ?? ??? ? ?? ??? ?? ??? ??????. 1. ??? ???? ?? ?? ??? ??? ?? ?? ???? ??? ?? ?????. 2. ??? ?? ? ??? ?? ?? ? ??? ???? ?? ?? ???; 3. ?? ??? ??? ?? ??? ???? ???? ???? ? ?? ?? ??? ?????. 4. ?? ??? ?? ??? ??? ???? ???? ?? ? ??? ???? ?? ??? ?????.

???? ??? ??? ??? ?????? ???? ??? ??? ??? ?????? Jun 25, 2025 pm 12:21 PM

???? ??? ??? Java?? ??? ?? ???? ??? ?? ? ? ??? ??? ???? ? ?????. ?? ???? ??? ??, ??? ?? ??? ?? ?? ??? ??? ????? ???? ????? ?????. ?? ??? ??? ??, ????? ? ??? ????, ?? ??? ??? ?????? ? ?? ? ?? ?????.

?? ??? ?????? ?? ??? ?????? Jun 24, 2025 pm 11:29 PM

??? ??? ?? ?? ??? ????? ? ???? ????? ???? ?? ???? ?? ???? ?????. ?? ??? ??? ????. ?? ?? ?? ??? ???? ???? ?? ?? ??? ??? ?? ?? ??? ??? ?????. ?? ??? ??? ????. ?? ??? ?? ??? ?? ?? ??? ?? ?? ??? ???? NewClass ()? ??? ?? ???? ????. ?? ??? ?? ??? ???? ?? ??? ?? ? ? ??? ?? ?? ??? ????? ????? ?????. ?? ??, ?? ?????? ?????, ??? ? ?? ????? ??? ?? ?????. ???? ?? ?? ??? ???? ?? ???? ?? ? ??? ???? ?? ??? ?? ?????? ?????. ???? ???? ??? ??, ?? ?? ? ?? ??? ????, ?? ?? ???? ?????.

?? ????? ?????? ?? ????? ?????? Jun 24, 2025 pm 11:09 PM

??? ? ?? ??? ???? : ????? ?? ?. 1. int? ???? ???? ?? ?? ?? ? ??? ???? ?????. 2. ?? ? ???? (int) myDouble ??? ?? ?? ??? ?????. ?? ??? ??? ?? ??? ?? ??, ?? ?? ?? ???? ?? ??? ?? ???? ?? ?????. ???? ? ??? ??? ????. ?? ??? ??? ??? ??? ??? ?? ??? ??? ? ??? ?? ???? ??? ??? ??? ??? ? ??? ?? ??? ?? ??? ?? ?? ? ? ????. ?? ?? ??? ?? ??? ??? ??? ??? ? ??????.

??? '??'???? ?????? ??? '??'???? ?????? Jun 24, 2025 pm 07:29 PM

injava, thefinalkeywordpreventsavariable'svalue'svalueffrombeingchangedafterassignment, butitsbehaviordiffersforprimitivesandobjectreences.forprimitivevariables, asinfinalintmax_speed = 100; wherereassoncesanerror.forobjectref

?? ???? ??? ??? ?????? ?? ???? ??? ??? ?????? Jun 28, 2025 am 01:01 AM

Java? ?? ??? ??? ?? ??? ??? ?? ??? ??? ?? ??? ?? ?? ??? ???? ??? ?? ???? ?????. 1. ??? ???? ??? ?? ?? ? ???? ?? ??? ???? ?? ?? ??? ? ????. 2. ???? ?? ??? ???? ??? ?? ???? ?? ?? ??? ???????. 3. ?? ???? ?? ?? ?? ? ???? ???? ?? NULL ?? ??? ? ????. 4. ?? ???? ??? ?? ?? ? ??? ?????? ?? ??? ??? ?? ?? ??? ????? ??? ??? ??? ??????? ?? ???? ??????.

See all articles