求java在线java帮助文档下载?能查函数的比如StringBuffer 中的append?

网上的JAVA面试题经常有两个String 字符串相加没有& StringBuffer.append();速度高
但是很少有说明原因的。
所以自己做了个小测试
源代码1
public class TestMain {
public static void main(String[] args) {
String test1="测试测试1";
String test2="测试测试2";
String test3=test1+test2;
System.out.println(test3);&&&&
结果编译后的class& 然后再反编译& 发现 代码已经被默认优化成
import java.io.PrintS&&&
public class TestMain
{
public static void main(String args[])
{
String test1 = "测试测试1";
String test2 = "测试测试2";
String test3 = (new StringBuilder(String.valueOf(test1))).append(test2).toString();
System.out.println(test3);
}
}
public static void main(String[] args) {&
String test1="测试测试1";
String test2="测试测试2";
StringBuffer test3=new StringBuffer();
test3.append(test1);
test3.append(test2);
System.out.println(test3);&&&
先编译再反编译
的结果跟以前一样
很明显 连个字符串 相加 最后都优化成了& StringBuilder的.append();
查了一下源代码
StringBuilder 类 和& StringBuffer 类都 继承于 AbstractStringBuilder
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
public synchronized StringBuffer append(String str) {
super.append(str);
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
public StringBuilder append(String str) {
super.append(str);
而且都是调用AbstractStringBuilder

public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
if (len == 0)
int newCount = count +
if (newCount & value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newC
经过以上得到以下结论:
所以 如果用加号相加两个字符串& 比用StringBuffer.append() 方法& 多创建对象而且没有append线程安全。
浏览: 3697 次
来自: 北京
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'Java StringBuffer 和 StringBuilder 类
我的图书馆
Java StringBuffer 和 StringBuilder 类
当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。
由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。
Test.java 文件代码:
public class Test{
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer("菜鸟教程官网:");
sBuffer.append("www");
sBuffer.append(".runoob");
sBuffer.append(".com");
System.out.println(sBuffer);
以上实例编译运行结果如下:
菜鸟教程官网:www.runoob.com
StringBuffer 方法
以下是 StringBuffer 类支持的主要方法:
public StringBuffer append(String s)
将指定的字符串追加到此字符序列。
public StringBuffer reverse()
&将此字符序列用其反转形式取代。
public delete(int start, int end)
移除此序列的子字符串中的字符。
public insert(int offset, int i)
将 int 参数的字符串表示形式插入此序列中。
replace(int start, int end, String str)
使用给定 String 中的字符替换此序列的子字符串中的字符。
下面的列表里的方法和 String 类的方法类似:
int capacity()
返回当前容量。
char charAt(int index)
返回此序列中指定索引处的 char 值。
void ensureCapacity(int minimumCapacity)
确保容量至少等于指定的最小值。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此序列复制到目标字符数组 dst。
int indexOf(String str)
返回第一次出现的指定子字符串在该字符串中的索引。
int indexOf(String str, int fromIndex)
从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
int lastIndexOf(String str)
返回最右边出现的指定子字符串在此字符串中的索引。
int lastIndexOf(String str, int fromIndex)
返回最后一次出现的指定子字符串在此字符串中的索引。
int length()
&返回长度(字符数)。
void setCharAt(int index, char ch)
将给定索引处的字符设置为 ch。
void setLength(int newLength)
设置字符序列的长度。
CharSequence subSequence(int start, int end)
返回一个新的字符序列,该字符序列是此序列的子序列。
String substring(int start)
返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
String substring(int start, int end)
返回一个新的 String,它包含此序列当前所包含的字符子序列。
String toString()
返回此序列中数据的字符串表示形式。
喜欢该文的人也喜欢Java 请教关于StringBuffer类中insert和append方法的小问题_百度知道
Java 请教关于StringBuffer类中insert和append方法的小问题
1.insert方法
char data[]={'a','b','c','d','e'};
StringBuffer buffer=new StringBuffer();
buffer.insert(0,100);
buffer.insert(0,2.5F);
buffer.insert(3,'*');
buffer.insert(0,250.0D);
buffer.insert(5,&is equal to&);
1)最后,bu...
我有更好的答案
按照执行顺序来
结果↓buffer.insert(0,100);
100buffer.insert(0,2.5F);
2.5100 buffer.insert(3,'*');
buffer.insert(0,250.0D);
250.02.5*100buffer.insert(5,&is equal to&);
250.0 is equal to 2.5*100buffer.append('')在字符串尾部加空格其实你只要单步调试就知道为什么了
采纳率:38%
buffer.insert(0,250.0D); buffer.insert(5,&is equal to&); insert第一个参数是插入位置,如果不是在字符串末尾,插入后原有文字会自动向后移动f表示floatD表示Double
为您推荐:
其他类似问题
您可能关注的内容
stringbuffer的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。The append() method concatenates the string representation of any other type of data to the end of the invoking StringBuffer object. It has overloaded versions for all the built-in types and for Object. Here are a few of its forms:
StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)
String.valueOf( ) is called for each parameter to obtain its string representation. The result is appended to the current StringBuffer object. The buffer itself is returned by each version of append(). This allows subsequent calls
to be chained together, as shown in the following example:
// Demonstrate append().
class appendDemo {
public static void main(String args[]) {
int a = 42;
StringBuffer sb = new StringBuffer(40);
s = sb.append(&a = &).append(a).append(&!&).toString();
System.out.println(s);
The output of this example is shown here:
The append( ) method is most often called when the + operator is used on String objects. Java automatically changes modifications to a String instance into similar operations on a StringBuffer instance. Thus, a concatenation invokes append( ) on a
StringBuffer object. After the concatenation has been performed, the compiler inserts a call to toString( ) to turn the modifiable StringBufferback into a constant String. All of this may seem unreasonably complicated. Why not just have one string class and have it behave more or less like StringBuffer? The answer is
performance. There are many optimizations that the Java run time can make knowing that Stringobjects are immutable. Thankfully, Java hides most of the complexity of conversion between Strings
and StringBuffers. Actually, many programmers will never feel the need to use StringBuffer directly and will be able to express most operations in terms of the + operator on String variables.
Most Viewed Articles (in Java )
Latest Articles (in Java)
Comment on this tutorial
Subscribe to Tutorials
Related Tutorials
Archived Comments
&&&&&&&&&By: Shivam at
03:58:512.
&&&&&&&&&By: vij at
16:00:123.
&&&&&&&&&By: Sudhir at
01:45:284.
&&&&&&&&&By: Joshua at
01:13:085.
&&&&&&&&&By: deeeegiiiiiiiiiiii at
02:07:136.
&&&&&&&&&By: prince at
00:18:457.
&&&&&&&&&By: WANT TO KNOW WHY WE USE APPEND at
00:39:488.
&&&&&&&&&By: Mohamed firnaz at
04:54:279.
&&&&&&&&&By: Janshair Khan at
12:36:5110.
&&&&&&&&&By: karthik,meenakshi amman kovil,mdu at
10:57:1411.
&&&&&&&&&By: marios at
16:17:3112.
&&&&&&&&&By: Umair Shuja at
10:53:3513.
&&&&&&&&&By: Arunlal at
11:56:4314.
&&&&&&&&&By: Eligio at
05:26:3515.
&&&&&&&&&By: rathika at
11:02:1616.
&&&&&&&&&By: mayank at
08:35:4817.
&&&&&&&&&By: Priya.d at
10:03:0018.
&&&&&&&&&By: LavinaX at
04:57:3219.
&&&&&&&&&By: GymGuygase at怎么我的StringBuffer对象调用不了append方法_百度知道
怎么我的StringBuffer对象调用不了append方法
我有更好的答案
public class Exercise7 {public static void main(String[] args) {char[] arr = {'a','A','中','雨','共','W','O','1','9','4'};//创建随机数生成类Random random=new Random();//创建字符缓冲区//StringBuffer sb=new StringBuffer();StringBuilder sb=new StringBuilder();for(int x=0; x&4; x++){int index=random.nextInt(arr.length);char temp=arr[index];sb.append(temp);//如果创建StringBuilder对象这里调用append方法会报错,创建StringBuffer 对象调用append方法不会报错}System.out.println(&四位数验证码:&+sb);}}复制代码
采纳率:94%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 java api帮助文档 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信