71 *
72 * @apiNote
73 * {@code StringBuilder} implements {@code Comparable} but does not override
74 * {@link Object#equals equals}. Thus, the natural ordering of {@code StringBuilder}
75 * is inconsistent with equals. Care should be exercised if {@code StringBuilder}
76 * objects are used as keys in a {@code SortedMap} or elements in a {@code SortedSet}.
77 * See {@link Comparable}, {@link java.util.SortedMap SortedMap}, or
78 * {@link java.util.SortedSet SortedSet} for more information.
79 *
80 * @author Michael McCloskey
81 * @see java.lang.StringBuffer
82 * @see java.lang.String
83 * @since 1.5
84 */
85 public final class StringBuilder
86 extends AbstractStringBuilder
87 implements java.io.Serializable, Comparable<StringBuilder>, CharSequence
88 {
89
90 /** use serialVersionUID for interoperability */
91 static final long serialVersionUID = 4383685877147921099L;
92
93 /**
94 * Constructs a string builder with no characters in it and an
95 * initial capacity of 16 characters.
96 */
97 @HotSpotIntrinsicCandidate
98 public StringBuilder() {
99 super(16);
100 }
101
102 /**
103 * Constructs a string builder with no characters in it and an
104 * initial capacity specified by the {@code capacity} argument.
105 *
106 * @param capacity the initial capacity.
107 * @throws NegativeArraySizeException if the {@code capacity}
108 * argument is less than {@code 0}.
109 */
110 @HotSpotIntrinsicCandidate
441
442 @Override
443 @HotSpotIntrinsicCandidate
444 public String toString() {
445 // Create a copy, don't share the array
446 return isLatin1() ? StringLatin1.newString(value, 0, count)
447 : StringUTF16.newString(value, 0, count);
448 }
449
450 /**
451 * Save the state of the {@code StringBuilder} instance to a stream
452 * (that is, serialize it).
453 *
454 * @serialData the number of characters currently stored in the string
455 * builder ({@code int}), followed by the characters in the
456 * string builder ({@code char[]}). The length of the
457 * {@code char} array may be greater than the number of
458 * characters currently stored in the string builder, in which
459 * case extra characters are ignored.
460 */
461 private void writeObject(java.io.ObjectOutputStream s)
462 throws java.io.IOException {
463 s.defaultWriteObject();
464 s.writeInt(count);
465 char[] val = new char[capacity()];
466 if (isLatin1()) {
467 StringLatin1.getChars(value, 0, count, val, 0);
468 } else {
469 StringUTF16.getChars(value, 0, count, val, 0);
470 }
471 s.writeObject(val);
472 }
473
474 /**
475 * readObject is called to restore the state of the StringBuffer from
476 * a stream.
477 */
478 private void readObject(java.io.ObjectInputStream s)
479 throws java.io.IOException, ClassNotFoundException {
480 s.defaultReadObject();
481 count = s.readInt();
482 char[] val = (char[]) s.readObject();
483 initBytes(val, 0, val.length);
484 }
485
486 }
|
71 *
72 * @apiNote
73 * {@code StringBuilder} implements {@code Comparable} but does not override
74 * {@link Object#equals equals}. Thus, the natural ordering of {@code StringBuilder}
75 * is inconsistent with equals. Care should be exercised if {@code StringBuilder}
76 * objects are used as keys in a {@code SortedMap} or elements in a {@code SortedSet}.
77 * See {@link Comparable}, {@link java.util.SortedMap SortedMap}, or
78 * {@link java.util.SortedSet SortedSet} for more information.
79 *
80 * @author Michael McCloskey
81 * @see java.lang.StringBuffer
82 * @see java.lang.String
83 * @since 1.5
84 */
85 public final class StringBuilder
86 extends AbstractStringBuilder
87 implements java.io.Serializable, Comparable<StringBuilder>, CharSequence
88 {
89
90 /** use serialVersionUID for interoperability */
91 @java.io.Serial
92 static final long serialVersionUID = 4383685877147921099L;
93
94 /**
95 * Constructs a string builder with no characters in it and an
96 * initial capacity of 16 characters.
97 */
98 @HotSpotIntrinsicCandidate
99 public StringBuilder() {
100 super(16);
101 }
102
103 /**
104 * Constructs a string builder with no characters in it and an
105 * initial capacity specified by the {@code capacity} argument.
106 *
107 * @param capacity the initial capacity.
108 * @throws NegativeArraySizeException if the {@code capacity}
109 * argument is less than {@code 0}.
110 */
111 @HotSpotIntrinsicCandidate
442
443 @Override
444 @HotSpotIntrinsicCandidate
445 public String toString() {
446 // Create a copy, don't share the array
447 return isLatin1() ? StringLatin1.newString(value, 0, count)
448 : StringUTF16.newString(value, 0, count);
449 }
450
451 /**
452 * Save the state of the {@code StringBuilder} instance to a stream
453 * (that is, serialize it).
454 *
455 * @serialData the number of characters currently stored in the string
456 * builder ({@code int}), followed by the characters in the
457 * string builder ({@code char[]}). The length of the
458 * {@code char} array may be greater than the number of
459 * characters currently stored in the string builder, in which
460 * case extra characters are ignored.
461 */
462 @java.io.Serial
463 private void writeObject(java.io.ObjectOutputStream s)
464 throws java.io.IOException {
465 s.defaultWriteObject();
466 s.writeInt(count);
467 char[] val = new char[capacity()];
468 if (isLatin1()) {
469 StringLatin1.getChars(value, 0, count, val, 0);
470 } else {
471 StringUTF16.getChars(value, 0, count, val, 0);
472 }
473 s.writeObject(val);
474 }
475
476 /**
477 * readObject is called to restore the state of the StringBuffer from
478 * a stream.
479 */
480 @java.io.Serial
481 private void readObject(java.io.ObjectInputStream s)
482 throws java.io.IOException, ClassNotFoundException {
483 s.defaultReadObject();
484 count = s.readInt();
485 char[] val = (char[]) s.readObject();
486 initBytes(val, 0, val.length);
487 }
488
489 }
|