String
1 public final class String 2 3 implements java.io.Serializable, Comparable<String>, CharSequence { 4 5 /** The value is used for character storage. */ 6 7 private final char value[]; 8 9 10 11 /** Cache the hash code for the string */ 12 13 private int hash; // Default to 0 14 15 16 17 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 18 19 private static final long serialVersionUID = -6849794470754667710L; 20 21 22 23 private static final ObjectStreamField[] serialPersistentFields = 24 25 new ObjectStreamField[0]; 26 27 28 29 public String() { 30 31 this.value = "".value; 32 33 } 34 35 36 37 public String(String original) { 38 39 this.value = original.value; 40 41 this.hash = original.hash; 42 43 } 44 45 46 47 public String(char value[]) { 48 49 this.value = Arrays.copyOf(value, value.length); 50 51 } 52 53 54 55 public String(char value[], int offset, int count) { 56 57 if (offset < 0) { 58 59 throw new StringIndexOutOfBoundsException(offset); 60 61 } 62 63 if (count <= 0) { 64 65 if (count < 0) { 66 67 throw new StringIndexOutOfBoundsException(count); 68 69 } 70 71 if (offset <= value.length) { 72 73 this.value = "".value; 74 75 return; 76 77 } 78 79 } 80 81 // Note: offset or count might be near -1>>>1. 82 83 if (offset > value.length - count) { 84 85 throw new StringIndexOutOfBoundsException(offset + count); 86 87 } 88 89 this.value = Arrays.copyOfRange(value, offset, offset+count); 90 91 } 92 93 94 95 public String(int[] codePoints, int offset, int count) { 96 97 if (offset < 0) { 98 99 throw new StringIndexOutOfBoundsException(offset); 100 101 } 102 103 if (count <= 0) { 104 105 if (count < 0) { 106 107 throw new StringIndexOutOfBoundsException(count); 108 109 } 110 111 if (offset <= codePoints.length) { 112 113 this.value = "".value; 114 115 return; 116 117 } 118 119 } 120 121 // Note: offset or count might be near -1>>>1. 122 123 if (offset > codePoints.length - count) { 124 125 throw new StringIndexOutOfBoundsException(offset + count); 126 127 } 128 129 130 131 final int end = offset + count; 132 133 134 135 // Pass 1: Compute precise size of char[] 136 137 int n = count; 138 139 for (int i = offset; i < end; i++) { 140 141 int c = codePoints[i]; 142 143 if (Character.isBmpCodePoint(c)) 144 145 continue; 146 147 else if (Character.isValidCodePoint(c)) 148 149 n++; 150 151 else throw new IllegalArgumentException(Integer.toString(c)); 152 153 } 154 155 156 157 // Pass 2: Allocate and fill in char[] 158 159 final char[] v = new char[n]; 160 161 162 163 for (int i = offset, j = 0; i < end; i++, j++) { 164 165 int c = codePoints[i]; 166 167 if (Character.isBmpCodePoint(c)) 168 169 v[j] = (char)c; 170 171 else 172 173 Character.toSurrogates(c, v, j++); 174 175 } 176 177 178 179 this.value = v; 180 181 } 182 183 184 185 private static void checkBounds(byte[] bytes, int offset, int length) { 186 187 if (length < 0) 188 189 throw new StringIndexOutOfBoundsException(length); 190 191 if (offset < 0) 192 193 throw new StringIndexOutOfBoundsException(offset); 194 195 if (offset > bytes.length - length) 196 197 throw new StringIndexOutOfBoundsException(offset + length); 198 199 } 200 201 202 203 public String(byte bytes[], int offset, int length, String charsetName) 204 205 throws UnsupportedEncodingException { 206 207 if (charsetName == null) 208 209 throw new NullPointerException("charsetName"); 210 211 checkBounds(bytes, offset, length); 212 213 this.value = StringCoding.decode(charsetName, bytes, offset, length); 214 215 } 216 217 218 219 public String(byte bytes[], int offset, int length, Charset charset) { 220 221 if (charset == null) 222 223 throw new NullPointerException("charset"); 224 225 checkBounds(bytes, offset, length); 226 227 this.value = StringCoding.decode(charset, bytes, offset, length); 228 229 } 230 231 232 233 public String(byte bytes[], String charsetName) 234 235 throws UnsupportedEncodingException { 236 237 this(bytes, 0, bytes.length, charsetName); 238 239 } 240 241 242 243 public String(byte bytes[], Charset charset) { 244 245 this(bytes, 0, bytes.length, charset); 246 247 } 248 249 250 251 public String(byte bytes[], int offset, int length) { 252 253 checkBounds(bytes, offset, length); 254 255 this.value = StringCoding.decode(bytes, offset, length); 256 257 } 258 259 260 261 public String(byte bytes[]) { 262 263 this(bytes, 0, bytes.length); 264 265 } 266 267 268 269 public String(StringBuffer buffer) { 270 271 synchronized(buffer) { 272 273 this.value = Arrays.copyOf(buffer.getValue(), buffer.length()); 274 275 } 276 277 } 278 279 280 281 public String(StringBuilder builder) { 282 283 this.value = Arrays.copyOf(builder.getValue(), builder.length()); 284 285 } 286 287 288 289 String(char[] value, boolean share) { 290 291 // assert share : "unshared not supported"; 292 293 this.value = value; 294 295 } 296 297 298 299 public int length() { 300 301 return value.length; 302 303 } 304 305 306 307 public boolean isEmpty() { 308 309 return value.length == 0; 310 311 } 312 313 }
部分资料直接复制网络中其他文章,仅供个人参考学习