2013-08-17 22 views
5

Stavo cercando di capire come è stato implementato uno java String. Il codice jdk7 source qui sotto mostra un controllo per originalValue.length > size. Non riesco a capire come/quando avverrà. Ho provato a usare eclipse debugger su qualche stringa java dichiarazioni di creazione, ma questo controllo non è mai stato vero. È possibile escogitare un argomento String che renderebbe questo controllo vero?java String constructor logic

public final class String{ 
    /** 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 

    /** 
    * Initializes a newly created {@code String} object so that it represents 
    * the same sequence of characters as the argument; in other words, the 
    * newly created string is a copy of the argument string. Unless an 
    * explicit copy of {@code original} is needed, use of this constructor is 
    * unnecessary since Strings are immutable. 
    * 
    * @param original 
    *   A {@code String} 
    */ 
    public String(String original) { 
     int size = original.count; 
     char[] originalValue = original.value; 
     char[] v; 
     if (originalValue.length > size) { 
      // The array representing the String is bigger than the new 
      // String itself. Perhaps this constructor is being called 
      // in order to trim the baggage, so make a copy of the array. 
      int off = original.offset; 
      v = Arrays.copyOfRange(originalValue, off, off+size); 
     } else { 
      // The array representing the String is the same 
      // size as the String, so no point in making a copy. 
      v = originalValue; 
     } 
     this.offset = 0; 
     this.count = size; 
     this.value = v; 
    } 
... 
} 

risposta

3

Date un'occhiata a questo pezzo di codice:

String s1 = "123456789"; 
String s2 = new String(s1.substring(0, 2)); 

Il secondo costruttore corrisponderà condizione. Il trucco è nel metodo della sottostringa. Non crea una sottostringa reale, ma piuttosto copia l'array sottostante e imposta semplicemente nuovi limiti ad esso. L'idea di costruire una nuova stringa è creare una copia di una stringa, non solo assegnare la stessa matrice. Questo è il motivo per cui l'utilizzo di una piccola sottostringa da una stringa di grandi dimensioni può portare all'eccezione di OOM. Perché per rappresentare una piccola informazione viene usato un grande array.

+0

è s2.length> 3 qui? – Karthikeyan

+0

Sì, poiché è lo stesso array di char sotto (non solo uno uguale). – oddparity

+0

In questo caso, l'array underender di s2 sarà di dimensione due, non nove. – Mikhail

3

È possibile eseguire il debug di questo. Value rappresenta il sottostante char[]. count rappresenta la view

String s = new String("Hello "); //value= [H, e, l, l, o, , , ] count=8 

String os = s.trim(); //value= [H, e, l, l, o, , , ] count=5