关于String.concat()方法和StringBuffer.append()方法的学习:方法是如何追加字符到源字符串的
问题分析:
首先,看看两段代码的运行结果,两段代码分别是:
第一段代码,关于String.concat()方法的测试:
public static void main(String[] args)
{
//String stringA = "hello";
String stringA = new String("hello");
testConcat test = new testConcat(); test.change(stringA); System.out.println(stringA);
}
public void change(String stringA)
{
stringA = stringA.concat("world");
}
第二段代码,StringBuffer.append()方法的测试:
public static void main(String[] args)
{
StringBuffer stringBufferA = new StringBuffer("hello");
testAppend test = new testAppend(); test.change(stringBufferA); System.out.println(stringBufferA);
}
public void change(StringBuffer stringBufferA)
{
stringBufferA.append("World");
}
在实际的运行这两段代码后,得到的结果是:
第一段代码结果:hello
第二段代码结果:helloWorld
由此,可以看出StringBuffer.append()所改变的是源引用的值,不会依赖于方法返回值,而String.concat()在进行字符串拼接的时候,会产生很多的临时对象来保存,最后在拼接结束后,需要把这个结果临时对象进行返回给接收值进行再指向,需要依赖于方法的返回值,执行的效率也会随着字符数的增加而降低,不是真正的引用源
总结:
在所使用的字符串需要经常变更的情况下,使用StringBuffer效率更高,可以使用StringBuffer进行字符串的变更操作,操作完成后再还给String,操作方法:String -> StringBuffer -> 更改字符串 -> String
待续...
关于String.concat()方法和StringBuffer.append()方法的学习:方法是如何追加字符到源字符串的的更多相关文章
- 在php中分别使用curl的post提交数据的方法和get获取网页数据的方法
在php中分别使用curl的post提交数据的方法和get获取网页数据的方法整理分享一下额,具体代码如下: (1)使用php curl获取网页数据的方法: $ch=curl_init(); //设置选 ...
- object的wait()、notify()、notifyAll()、方法和Condition的await()、signal()方法
wait().notify()和notifyAll()是 Object类 中的方法 从这三个方法的文字描述可以知道以下几点信息: 1)wait().notify()和notifyAll()方法是本地方 ...
- File类获取功能的方法和File类判断功能的方法
File类获取功能的方法-public String getAbsolutePath() :返回此file的绝对路径名字符串 -public String getPath() :将此File转换为路径 ...
- 关于 jQuery 中的 $.data() 方法和 jQuery 对象上的data 方法
参见文章:http://www.it165.net/pro/html/201404/11922.html
- 为什么要重写hashcode方法和equals方法
我们可能经常听到说重写equals方法必须重写hashcode方法,这是为什么呢?java中所有的类都是Object的子类,直接上object源码 /* * Copyright (c) 1994, 2 ...
- Spark Rdd coalesce()方法和repartition()方法
在Spark的Rdd中,Rdd是分区的. 有时候需要重新设置Rdd的分区数量,比如Rdd的分区中,Rdd分区比较多,但是每个Rdd的数据量比较小,需要设置一个比较合理的分区.或者需要把Rdd的分区数量 ...
- 【js】数组的splice方法和slice方法的区别
1.splice方法和slice都是数组的方法 2.splice方法有3种作用 例如,arr=[1,2,3,4,5,6]; splice方法 删除 arr.splice(1, 1); 返回值为删除的 ...
- Java学习笔记20(String类应用、StringBuffer类、StringBuilder类)
1.获取指定字符串中大小写和数字的个数: package demo; public class StringTest { public static void main(String[] args) ...
- 浅析String、StringBuilder、StringBuffer
谈谈我对 String.StringBuilder.StringBuffer 的理解 StringBuilder.StringBuffer 和 String 一样,都是用于存储字符串的. 1.那既然有 ...
随机推荐
- CCTableView 简单样例
非常像android中的listview #pragma once; #include "cocos2d.h" using namespace cocos2d; //使用CCTab ...
- 自己动手写处理器之第一阶段(3)——MIPS32指令集架构简单介绍
将陆续上传本人写的新书<自己动手写处理器>(尚未出版).今天是第四篇.我尽量每周四篇 1.4 MIPS32指令集架构简单介绍 本书设计的处理器遵循MIPS32 Release 1架构,所以 ...
- if...else if...else
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prog ...
- 一些ASP.NET的小知识点
DataFormatString="{0:格式字符串}" 我们知道在DataFormatString 中的 {0}表示数据本身,而在冒号后面的格式字符串代表所们希望数据显示的格式; ...
- Objective-c 深浅复制
深浅复制的定义: 浅复制:在复制时,对于被复制对象的每一层都是指针复制. 深复制:在复制时,对于被复制的对象至少有一层是对象复制. 完全复制:在复制时,对于被复制对象的每一层都是完全复制. retai ...
- iOS 中多线程的简单使用
iOS中常用的多线程操作有( NSThread, NSOperation GCD ) 为了能更直观的展现多线程操作在SB中做如下的界面布局: 当点击下载的时候从网络上下载图片: - (void)loa ...
- Hibernate中,left join、inner join以及left join fetch区别(转)
标签: hibernate hql inner join left right 杂谈 分类: SQL 原文地址:http://m33707.iteye.com/blog/829725 Select F ...
- Maven Spring JUnit 在Maven Clean Install时报
问题: Maven Clean Install时, 遇到报错package org.junit does not exist 明显, Unit Test在Compile阶段就被检查了. 而POM.xm ...
- easyui form提交文件(上传图片和文件)
<div id="dialogBtn"> <a class="easyui-linkbutton" href="#" on ...
- Python 城市菜单详解(超详解)
print("--------城市查询系统---------") print("--------按数值进行查询--------") menu={" ...