- 浏览: 91919 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (148)
- 全文检索 (1)
- java (29)
- xml (2)
- json (2)
- redis springmvc (1)
- Redis (5)
- 开发常识 (1)
- tomcat (2)
- 单元测试Junit (2)
- 设计模式 (2)
- spring (10)
- jvm (2)
- eclipse (4)
- echart (1)
- mybatis (1)
- mysql (3)
- web (1)
- js (2)
- PL/SQL (2)
- 其他 (1)
- 人生 (1)
- 安全 (2)
- jsp (2)
- 硬件电脑 (1)
- linux (3)
- git (10)
- oracle (8)
- ant (1)
- maven (2)
- 正则表达式 (2)
- chrome (1)
- 面试 (6)
- 多线程 (19)
- bug (11)
- java工具类 (3)
- 算法 (1)
- bug,git (1)
- shell (2)
- springmvc (2)
- Java8 (1)
- 消息队列-rocketmq (1)
- es (1)
- dubbo (0)
- spring cloud (0)
- hashmap (0)
- springboot (1)
- velocity (0)
声明:以下内容如有雷同,纯属参考。
StringBuffer 多线程安全,执行速度慢,字符串变量
StringBuilder单线程不安全,执行速度快,字符串变量
StringBuffer和StringBuilder类都表示内容可以被修改的字符串,StringBuilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义,这种情况只可能有一个线程访问它,不存在不安全的因素了,则用StringBuilder。如果要在类里面定义成员变量,并且这个类的实例对象会在多线程环境下使用,那么最好用StringBuffer。
为什么一个线程安全,一个线程不安全呢?
直接从源码来看更直接,我选jdk1.6下的StringBuffer 和 StringBuilder下的源码,进行比较,可以发现StringBuffer中的方法全部都添加了synchronized关键字。而StringBuilder中并没有。这就很明显了。
为什么一个执行效率高,一个执行效率慢?
本质上也是由多线程的加锁机制导致的。
StringBuffer 多线程 方法上加锁
StringBuilder方法上则没有加锁就是单线程。
StringBuffer 多线程安全,执行速度慢,字符串变量
StringBuilder单线程不安全,执行速度快,字符串变量
StringBuffer和StringBuilder类都表示内容可以被修改的字符串,StringBuilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义,这种情况只可能有一个线程访问它,不存在不安全的因素了,则用StringBuilder。如果要在类里面定义成员变量,并且这个类的实例对象会在多线程环境下使用,那么最好用StringBuffer。
为什么一个线程安全,一个线程不安全呢?
直接从源码来看更直接,我选jdk1.6下的StringBuffer 和 StringBuilder下的源码,进行比较,可以发现StringBuffer中的方法全部都添加了synchronized关键字。而StringBuilder中并没有。这就很明显了。
为什么一个执行效率高,一个执行效率慢?
本质上也是由多线程的加锁机制导致的。
StringBuffer 多线程 方法上加锁
package java.lang; /** * A thread-safe, mutable sequence of characters. * A string buffer is like a {@link String}, but can be modified. At any * point in time it contains some particular sequence of characters, but * the length and content of the sequence can be changed through certain * method calls. * <p> * String buffers are safe for use by multiple threads. The methods * are synchronized where necessary so that all the operations on any * particular instance behave as if they occur in some serial order * that is consistent with the order of the method calls made by each of * the individual threads involved. * <p> * The principal operations on a <code>StringBuffer</code> are the * <code>append</code> and <code>insert</code> methods, which are * overloaded so as to accept data of any type. Each effectively * converts a given datum to a string and then appends or inserts the * characters of that string to the string buffer. The * <code>append</code> method always adds these characters at the end * of the buffer; the <code>insert</code> method adds the characters at * a specified point. * <p> * For example, if <code>z</code> refers to a string buffer object * whose current contents are "<code>start</code>", then * the method call <code>z.append("le")</code> would cause the string * buffer to contain "<code>startle</code>", whereas * <code>z.insert(4, "le")</code> would alter the string buffer to * contain "<code>starlet</code>". * <p> * In general, if sb refers to an instance of a <code>StringBuffer</code>, * then <code>sb.append(x)</code> has the same effect as * <code>sb.insert(sb.length(), x)</code>. * <p> * Whenever an operation occurs involving a source sequence (such as * appending or inserting from a source sequence) this class synchronizes * only on the string buffer performing the operation, not on the source. * <p> * Every string buffer has a capacity. As long as the length of the * character sequence contained in the string buffer does not exceed * the capacity, it is not necessary to allocate a new internal * buffer array. If the internal buffer overflows, it is * automatically made larger. * * As of release JDK 5, this class has been supplemented with an equivalent * class designed for use by a single thread, {@link StringBuilder}. The * <tt>StringBuilder</tt> class should generally be used in preference to * this one, as it supports all of the same operations but it is faster, as * it performs no synchronization. * * @author Arthur van Hoff * @version %I%, %G% * @see java.lang.StringBuilder * @see java.lang.String * @since JDK1.0 */ public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharSequence { /** use serialVersionUID from JDK 1.0.2 for interoperability */ static final long serialVersionUID = 3388685877147921107L; /** * Constructs a string buffer with no characters in it and an * initial capacity of 16 characters. */ public StringBuffer() { super(16); } /** * Constructs a string buffer with no characters in it and * the specified initial capacity. * * @param capacity the initial capacity. * @exception NegativeArraySizeException if the <code>capacity</code> * argument is less than <code>0</code>. */ public StringBuffer(int capacity) { super(capacity); } /** * Constructs a string buffer initialized to the contents of the * specified string. The initial capacity of the string buffer is * <code>16</code> plus the length of the string argument. * * @param str the initial contents of the buffer. * @exception NullPointerException if <code>str</code> is <code>null</code> */ public StringBuffer(String str) { super(str.length() + 16); append(str); } /** * Constructs a string buffer that contains the same characters * as the specified <code>CharSequence</code>. The initial capacity of * the string buffer is <code>16</code> plus the length of the * <code>CharSequence</code> argument. * <p> * If the length of the specified <code>CharSequence</code> is * less than or equal to zero, then an empty buffer of capacity * <code>16</code> is returned. * * @param seq the sequence to copy. * @exception NullPointerException if <code>seq</code> is <code>null</code> * @since 1.5 */ public StringBuffer(CharSequence seq) { this(seq.length() + 16); append(seq); } public synchronized int length() { return count; } public synchronized int capacity() { return value.length; } public synchronized void ensureCapacity(int minimumCapacity) { if (minimumCapacity > value.length) { expandCapacity(minimumCapacity); } } /** * @since 1.5 */ public synchronized void trimToSize() { super.trimToSize(); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ public synchronized void setLength(int newLength) { super.setLength(newLength); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ public synchronized char charAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); return value[index]; } /** * @since 1.5 */ public synchronized int codePointAt(int index) { return super.codePointAt(index); } /** * @since 1.5 */ public synchronized int codePointBefore(int index) { return super.codePointBefore(index); } /** * @since 1.5 */ public synchronized int codePointCount(int beginIndex, int endIndex) { return super.codePointCount(beginIndex, endIndex); } /** * @since 1.5 */ public synchronized int offsetByCodePoints(int index, int codePointOffset) { return super.offsetByCodePoints(index, codePointOffset); } /** * @throws NullPointerException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { super.getChars(srcBegin, srcEnd, dst, dstBegin); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ public synchronized void setCharAt(int index, char ch) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); value[index] = ch; } /** * @see java.lang.String#valueOf(java.lang.Object) * @see #append(java.lang.String) */ public synchronized StringBuffer append(Object obj) { super.append(String.valueOf(obj)); return this; } public synchronized StringBuffer append(String str) { super.append(str); return this; } /** * Appends the specified <tt>StringBuffer</tt> to this sequence. * <p> * The characters of the <tt>StringBuffer</tt> argument are appended, * in order, to the contents of this <tt>StringBuffer</tt>, increasing the * length of this <tt>StringBuffer</tt> by the length of the argument. * If <tt>sb</tt> is <tt>null</tt>, then the four characters * <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>. * <p> * Let <i>n</i> be the length of the old character sequence, the one * contained in the <tt>StringBuffer</tt> just prior to execution of the * <tt>append</tt> method. Then the character at index <i>k</i> in * the new character sequence is equal to the character at index <i>k</i> * in the old character sequence, if <i>k</i> is less than <i>n</i>; * otherwise, it is equal to the character at index <i>k-n</i> in the * argument <code>sb</code>. * <p> * This method synchronizes on <code>this</code> (the destination) * object but does not synchronize on the source (<code>sb</code>). * * @param sb the <tt>StringBuffer</tt> to append. * @return a reference to this object. * @since 1.4 */ public synchronized StringBuffer append(StringBuffer sb) { super.append(sb); return this; } /** * Appends the specified <code>CharSequence</code> to this * sequence. * <p> * The characters of the <code>CharSequence</code> argument are appended, * in order, increasing the length of this sequence by the length of the * argument. * * <p>The result of this method is exactly the same as if it were an * invocation of this.append(s, 0, s.length()); * * <p>This method synchronizes on this (the destination) * object but does not synchronize on the source (<code>s</code>). * * <p>If <code>s</code> is <code>null</code>, then the four characters * <code>"null"</code> are appended. * * @param s the <code>CharSequence</code> to append. * @return a reference to this object. * @since 1.5 */ public StringBuffer append(CharSequence s) { // Note, synchronization achieved via other invocations if (s == null) s = "null"; if (s instanceof String) return this.append((String)s); if (s instanceof StringBuffer) return this.append((StringBuffer)s); return this.append(s, 0, s.length()); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ public synchronized StringBuffer append(CharSequence s, int start, int end) { super.append(s, start, end); return this; } public synchronized StringBuffer append(char str[]) { super.append(str); return this; } public synchronized StringBuffer append(char str[], int offset, int len) { super.append(str, offset, len); return this; } /** * @see java.lang.String#valueOf(boolean) * @see #append(java.lang.String) */ public synchronized StringBuffer append(boolean b) { super.append(b); return this; } public synchronized StringBuffer append(char c) { super.append(c); return this; } /** * @see java.lang.String#valueOf(int) * @see #append(java.lang.String) */ public synchronized StringBuffer append(int i) { super.append(i); return this; } /** * @since 1.5 */ public synchronized StringBuffer appendCodePoint(int codePoint) { super.appendCodePoint(codePoint); return this; } /** * @see java.lang.String#valueOf(long) * @see #append(java.lang.String) */ public synchronized StringBuffer append(long lng) { super.append(lng); return this; } /** * @see java.lang.String#valueOf(float) * @see #append(java.lang.String) */ public synchronized StringBuffer append(float f) { super.append(f); return this; } /** * @see java.lang.String#valueOf(double) * @see #append(java.lang.String) */ public synchronized StringBuffer append(double d) { super.append(d); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ public synchronized StringBuffer delete(int start, int end) { super.delete(start, end); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ public synchronized StringBuffer deleteCharAt(int index) { super.deleteCharAt(index); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ public synchronized StringBuffer replace(int start, int end, String str) { super.replace(start, end, str); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ public synchronized String substring(int start) { return substring(start, count); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.4 */ public synchronized CharSequence subSequence(int start, int end) { return super.substring(start, end); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ public synchronized String substring(int start, int end) { return super.substring(start, end); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ public synchronized StringBuffer insert(int index, char str[], int offset, int len) { super.insert(index, str, offset, len); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(java.lang.Object) * @see #insert(int, java.lang.String) * @see #length() */ public synchronized StringBuffer insert(int offset, Object obj) { super.insert(offset, String.valueOf(obj)); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see #length() */ public synchronized StringBuffer insert(int offset, String str) { super.insert(offset, str); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ public synchronized StringBuffer insert(int offset, char str[]) { super.insert(offset, str); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ public StringBuffer insert(int dstOffset, CharSequence s) { // Note, synchronization achieved via other invocations if (s == null) s = "null"; if (s instanceof String) return this.insert(dstOffset, (String)s); return this.insert(dstOffset, s, 0, s.length()); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ public synchronized StringBuffer insert(int dstOffset, CharSequence s, int start, int end) { super.insert(dstOffset, s, start, end); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(boolean) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuffer insert(int offset, boolean b) { return insert(offset, String.valueOf(b)); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ public synchronized StringBuffer insert(int offset, char c) { super.insert(offset, c); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(int) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuffer insert(int offset, int i) { return insert(offset, String.valueOf(i)); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(long) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuffer insert(int offset, long l) { return insert(offset, String.valueOf(l)); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(float) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuffer insert(int offset, float f) { return insert(offset, String.valueOf(f)); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(double) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuffer insert(int offset, double d) { return insert(offset, String.valueOf(d)); } /** * @throws NullPointerException {@inheritDoc} * @since 1.4 */ public int indexOf(String str) { return indexOf(str, 0); } /** * @throws NullPointerException {@inheritDoc} * @since 1.4 */ public synchronized int indexOf(String str, int fromIndex) { return String.indexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } /** * @throws NullPointerException {@inheritDoc} * @since 1.4 */ public int lastIndexOf(String str) { // Note, synchronization achieved via other invocations return lastIndexOf(str, count); } /** * @throws NullPointerException {@inheritDoc} * @since 1.4 */ public synchronized int lastIndexOf(String str, int fromIndex) { return String.lastIndexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } /** * @since JDK1.0.2 */ public synchronized StringBuffer reverse() { super.reverse(); return this; } public synchronized String toString() { return new String(value, 0, count); } /** * Serializable fields for StringBuffer. * * @serialField value char[] * The backing character array of this StringBuffer. * @serialField count int * The number of characters in this StringBuffer. * @serialField shared boolean * A flag indicating whether the backing array is shared. * The value is ignored upon deserialization. */ private static final java.io.ObjectStreamField[] serialPersistentFields = { new java.io.ObjectStreamField("value", char[].class), new java.io.ObjectStreamField("count", Integer.TYPE), new java.io.ObjectStreamField("shared", Boolean.TYPE), }; /** * readObject is called to restore the state of the StringBuffer from * a stream. */ private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { java.io.ObjectOutputStream.PutField fields = s.putFields(); fields.put("value", value); fields.put("count", count); fields.put("shared", false); s.writeFields(); } /** * readObject is called to restore the state of the StringBuffer from * a stream. */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { java.io.ObjectInputStream.GetField fields = s.readFields(); value = (char[])fields.get("value", null); count = (int)fields.get("count", 0); } }
StringBuilder方法上则没有加锁就是单线程。
/* * %W% %E% * * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.lang; /** * A mutable sequence of characters. This class provides an API compatible * with <code>StringBuffer</code>, but with no guarantee of synchronization. * This class is designed for use as a drop-in replacement for * <code>StringBuffer</code> in places where the string buffer was being * used by a single thread (as is generally the case). Where possible, * it is recommended that this class be used in preference to * <code>StringBuffer</code> as it will be faster under most implementations. * * <p>The principal operations on a <code>StringBuilder</code> are the * <code>append</code> and <code>insert</code> methods, which are * overloaded so as to accept data of any type. Each effectively * converts a given datum to a string and then appends or inserts the * characters of that string to the string builder. The * <code>append</code> method always adds these characters at the end * of the builder; the <code>insert</code> method adds the characters at * a specified point. * <p> * For example, if <code>z</code> refers to a string builder object * whose current contents are "<code>start</code>", then * the method call <code>z.append("le")</code> would cause the string * builder to contain "<code>startle</code>", whereas * <code>z.insert(4, "le")</code> would alter the string builder to * contain "<code>starlet</code>". * <p> * In general, if sb refers to an instance of a <code>StringBuilder</code>, * then <code>sb.append(x)</code> has the same effect as * <code>sb.insert(sb.length(), x)</code>. * * Every string builder has a capacity. As long as the length of the * character sequence contained in the string builder does not exceed * the capacity, it is not necessary to allocate a new internal * buffer. If the internal buffer overflows, it is automatically made larger. * * <p>Instances of <code>StringBuilder</code> are not safe for * use by multiple threads. If such synchronization is required then it is * recommended that {@link java.lang.StringBuffer} be used. * * @author Michael McCloskey * @version %I%, %G% * @see java.lang.StringBuffer * @see java.lang.String * @since 1.5 */ public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, CharSequence { /** use serialVersionUID for interoperability */ static final long serialVersionUID = 4383685877147921099L; /** * Constructs a string builder with no characters in it and an * initial capacity of 16 characters. */ public StringBuilder() { super(16); } /** * Constructs a string builder with no characters in it and an * initial capacity specified by the <code>capacity</code> argument. * * @param capacity the initial capacity. * @throws NegativeArraySizeException if the <code>capacity</code> * argument is less than <code>0</code>. */ public StringBuilder(int capacity) { super(capacity); } /** * Constructs a string builder initialized to the contents of the * specified string. The initial capacity of the string builder is * <code>16</code> plus the length of the string argument. * * @param str the initial contents of the buffer. * @throws NullPointerException if <code>str</code> is <code>null</code> */ public StringBuilder(String str) { super(str.length() + 16); append(str); } /** * Constructs a string builder that contains the same characters * as the specified <code>CharSequence</code>. The initial capacity of * the string builder is <code>16</code> plus the length of the * <code>CharSequence</code> argument. * * @param seq the sequence to copy. * @throws NullPointerException if <code>seq</code> is <code>null</code> */ public StringBuilder(CharSequence seq) { this(seq.length() + 16); append(seq); } /** * @see java.lang.String#valueOf(java.lang.Object) * @see #append(java.lang.String) */ public StringBuilder append(Object obj) { return append(String.valueOf(obj)); } public StringBuilder append(String str) { super.append(str); return this; } // Appends the specified string builder to this sequence. private StringBuilder append(StringBuilder sb) { if (sb == null) return append("null"); int len = sb.length(); int newcount = count + len; if (newcount > value.length) expandCapacity(newcount); sb.getChars(0, len, value, count); count = newcount; return this; } /** * Appends the specified <tt>StringBuffer</tt> to this sequence. * <p> * The characters of the <tt>StringBuffer</tt> argument are appended, * in order, to this sequence, increasing the * length of this sequence by the length of the argument. * If <tt>sb</tt> is <tt>null</tt>, then the four characters * <tt>"null"</tt> are appended to this sequence. * <p> * Let <i>n</i> be the length of this character sequence just prior to * execution of the <tt>append</tt> method. Then the character at index * <i>k</i> in the new character sequence is equal to the character at * index <i>k</i> in the old character sequence, if <i>k</i> is less than * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i> * in the argument <code>sb</code>. * * @param sb the <tt>StringBuffer</tt> to append. * @return a reference to this object. */ public StringBuilder append(StringBuffer sb) { super.append(sb); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ public StringBuilder append(CharSequence s) { if (s == null) s = "null"; if (s instanceof String) return this.append((String)s); if (s instanceof StringBuffer) return this.append((StringBuffer)s); if (s instanceof StringBuilder) return this.append((StringBuilder)s); return this.append(s, 0, s.length()); } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ public StringBuilder append(CharSequence s, int start, int end) { super.append(s, start, end); return this; } public StringBuilder append(char str[]) { super.append(str); return this; } public StringBuilder append(char str[], int offset, int len) { super.append(str, offset, len); return this; } /** * @see java.lang.String#valueOf(boolean) * @see #append(java.lang.String) */ public StringBuilder append(boolean b) { super.append(b); return this; } public StringBuilder append(char c) { super.append(c); return this; } /** * @see java.lang.String#valueOf(int) * @see #append(java.lang.String) */ public StringBuilder append(int i) { super.append(i); return this; } /** * @see java.lang.String#valueOf(long) * @see #append(java.lang.String) */ public StringBuilder append(long lng) { super.append(lng); return this; } /** * @see java.lang.String#valueOf(float) * @see #append(java.lang.String) */ public StringBuilder append(float f) { super.append(f); return this; } /** * @see java.lang.String#valueOf(double) * @see #append(java.lang.String) */ public StringBuilder append(double d) { super.append(d); return this; } /** * @since 1.5 */ public StringBuilder appendCodePoint(int codePoint) { super.appendCodePoint(codePoint); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ public StringBuilder delete(int start, int end) { super.delete(start, end); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ public StringBuilder deleteCharAt(int index) { super.deleteCharAt(index); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ public StringBuilder replace(int start, int end, String str) { super.replace(start, end, str); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ public StringBuilder insert(int index, char str[], int offset, int len) { super.insert(index, str, offset, len); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(java.lang.Object) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuilder insert(int offset, Object obj) { return insert(offset, String.valueOf(obj)); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see #length() */ public StringBuilder insert(int offset, String str) { super.insert(offset, str); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ public StringBuilder insert(int offset, char str[]) { super.insert(offset, str); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ public StringBuilder insert(int dstOffset, CharSequence s) { if (s == null) s = "null"; if (s instanceof String) return this.insert(dstOffset, (String)s); return this.insert(dstOffset, s, 0, s.length()); } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ public StringBuilder insert(int dstOffset, CharSequence s, int start, int end) { super.insert(dstOffset, s, start, end); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(boolean) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuilder insert(int offset, boolean b) { super.insert(offset, b); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ public StringBuilder insert(int offset, char c) { super.insert(offset, c); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(int) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuilder insert(int offset, int i) { return insert(offset, String.valueOf(i)); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(long) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuilder insert(int offset, long l) { return insert(offset, String.valueOf(l)); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(float) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuilder insert(int offset, float f) { return insert(offset, String.valueOf(f)); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @see java.lang.String#valueOf(double) * @see #insert(int, java.lang.String) * @see #length() */ public StringBuilder insert(int offset, double d) { return insert(offset, String.valueOf(d)); } /** * @throws NullPointerException {@inheritDoc} */ public int indexOf(String str) { return indexOf(str, 0); } /** * @throws NullPointerException {@inheritDoc} */ public int indexOf(String str, int fromIndex) { return String.indexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } /** * @throws NullPointerException {@inheritDoc} */ public int lastIndexOf(String str) { return lastIndexOf(str, count); } /** * @throws NullPointerException {@inheritDoc} */ public int lastIndexOf(String str, int fromIndex) { return String.lastIndexOf(value, 0, count, str.toCharArray(), 0, str.length(), fromIndex); } public StringBuilder reverse() { super.reverse(); return this; } public String toString() { // Create a copy, don't share the array return new String(value, 0, count); } /** * Save the state of the <tt>StringBuilder</tt> instance to a stream * (that is, serialize it). * * @serialData the number of characters currently stored in the string * builder (<tt>int</tt>), followed by the characters in the * string builder (<tt>char[]</tt>). The length of the * <tt>char</tt> array may be greater than the number of * characters currently stored in the string builder, in which * case extra characters are ignored. */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.defaultWriteObject(); s.writeInt(count); s.writeObject(value); } /** * readObject is called to restore the state of the StringBuffer from * a stream. */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); count = s.readInt(); value = (char[]) s.readObject(); } }
发表评论
-
linux 文件 dos unix格式,unix和dos下文本文件得区别
2023-09-06 23:23 159问题: 当在window ... -
面试之hashmap
2020-06-16 06:41 0hashmap经常会问到的面试题 1、JDK8中的HashM ... -
java 枚举values()方法
2019-07-30 20:41 418工作中,同事 ... -
newHashMapWithExpectedSize
2019-07-30 19:45 1285newHashMapWithExpectedSize VS ... -
java 8 stream应用
2019-07-15 11:43 01.生成map List<BusinessFacili ... -
技术知识点汇总
2019-02-12 15:47 01. LTS(light-task-scheduler) ... -
面试经历中遇到的面试题-one
2019-01-03 19:24 362整理记录一些面试要点 ... -
架构演进
2018-12-21 00:54 371一传统垂直mvc项目 垂直架构图:表示层->业务逻辑层- ... -
集群中session共享
2018-12-20 23:25 0当项目演进时,一个节点出错,如何保证域名能访问到别的节点。 一 ... -
父类子类静态代码块执行
2018-08-22 15:23 0先看一个基础面试题: package com.jbx.te ... -
数组元素的初始化
2018-07-10 17:16 494直接上代码 ,char的初始化值是'\u0000' p ... -
统计一个字符串中每个字符串出现的次数
2018-07-03 17:43 602经常遇到这个问题:总结一下 思路和代码 import ja ... -
DateUtil
2018-03-15 20:53 496记录一下常用的工具类,方便使用的时候可以获取。 pack ... -
indexOf判断一个字符串是否包含另一个字符串
2018-02-26 08:58 512jdk中的表述如下 indexOf public int in ... -
eclipse 添加反编译
2018-01-25 08:57 322为eclipse中*.clsas/*.class withou ... -
Java读取配置文件
2018-01-24 16:00 313Java读取配置文件test.properties 文件tes ... -
HTTP请求头
2017-12-29 16:49 0Request Headers 当访问一个action时,在谷 ... -
阿里巴巴Java开发手册(终极版)
2017-11-13 16:37 486前言 《阿里巴巴 Java 开发手册》是阿里巴巴集团技术团队 ... -
JavaEE 基础实用教程(二)------Jsp应用基础
2017-11-03 17:22 659习题 1.画出HTML文件的基 ... -
JavaEE 基础实用教程(一)---简述
2017-11-03 14:55 594第一部分 实用教程 第 ...
相关推荐
"String StringBuffer和StringBuilder区别之源码解析" 在Java中,字符串是我们经常使用的数据类型,而String、StringBuffer和StringBuilder是Java中三种常用的字符串类。在这篇文章中,我们将从源码角度对String、...
StringBuffer:字符创变量 StringBuilder:字符创变量 从上面的名字可以看到,String是“字符创常量”,也就是不可改变的对象。对于这句话的理解你可能会产生这样一个疑问 ,比如这段代码:
String, StringBuffer 与 StringBuilder 的区别 在 Java 中,String, StringBuffer 和 StringBuilder 三个类都是用于字符操作的,但它们之间有着很大的区别。 首先,String 是不可变类,意味着一旦创建了 String ...
Java 中 String, StringBuffer 与 StringBuilder 的区别 Java 中 String, StringBuffer 与 StringBuilder 三种字符串类型的区别是很多开发者经常混淆或不了解的知识点。今天,我们将深入探讨这三种字符串类型的区别...
StringBuilder 类型是可变的对象,提供了与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候。StringBuilder 在大多数实现中比 ...
在Java编程语言中,`String`、`...理解`String`、`StringBuffer`和`StringBuilder`的区别和使用场合,可以帮助开发者写出更高效、更安全的代码。在实际开发中,应根据项目需求和环境选择合适的字符串处理类。
`StringBuilder`是`JDK 5.0`引入的新类,它是`StringBuffer`的一个轻量级替代品,主要区别在于`StringBuilder`不是线程安全的。这意味着在单线程环境下,`StringBuilder`的操作速度通常会比`StringBuffer`更快,因为...
在Java编程语言中,String、StringBuffer和StringBuilder都是用来处理字符串的重要类,它们各自具有不同的特性和使用场景。下面将详细解析这三个类的区别。 首先,`String`类是最基础的字符串处理类,它被设计为不...
在Java编程语言中,String、StringBuilder和StringBuffer都是用来处理字符串的类,它们之间存在一些重要的区别,主要涉及到性能和线程安全性。 首先,`String`类代表的是字符串常量,一旦创建,其内容就不能改变。...
string,stringbuffer,stringbuilder
StringBuffer & StringBuilder 源码分析 StringBuffer 和 StringBuilder 是 Java 语言中两个常用的字符串操作类,它们都是 CharSequence 接口的实现类,并且都继承了 AbstractStringBuilder 类。下面是对这两个类的...
《Java中的StringBuffer与StringBuilder类详解》 在Java编程中,当我们需要对字符串进行多次修改时,StringBuffer和StringBuilder类成为首选。这两个类提供了一种高效且灵活的方式来处理字符串,与不可变的String类...
string,stringBuffer,stringBuilder
与 `StringBuffer` 类似,`StringBuilder` 的用法也包括 `append()`、`insert()`、`deleteCharAt()` 和 `reverse()` 方法。 **四、为什么StringBuffer和StringBuilder更适合在循环中使用?** 因为 `String` 对象是...
JAVA面试题,从源码角度分析StringBuffer和StringBuilder的区别 在本文中,我们将从源码角度分析StringBuffer和StringBuilder的区别,这两个类都是Java中常用的字符串操作类,但是它们之间有什么区别呢?下面我们来...
- **可变性与效率**:StringBuilder与StringBuffer类似,也是可变的,允许直接修改对象。但是,StringBuilder没有实现线程安全,这意味着它不包含同步方法,因此在单线程环境中,StringBuilder通常比StringBuffer更...
BATJ面试题讲解-String、StringBuffer、StringBuilder的区别
Java面试题-每日一题:String、StringBuffer、StringBuilder的区别