欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java StringBuffer类实现字符串相加,javastringbuffer,使用StringBuff

来源: javaer 分享于  点击 11550 次 点评:1

java StringBuffer类实现字符串相加,javastringbuffer,使用StringBuff


使用StringBuffer类实现字符串相加,要比使用String类直接相加高效的多。因为使用字符串相加每次相加都需要在内存中生成一个新的字符串变量。而使用StringBuffer则不会。

在使用StringBuffer对字符串相加后,可以使用其toString()方法返回相加后的完整字符串

package cn.outofmemory.examples;public class Append {   public static void main(String args[]) {     // Create a StringBuffer object named buf.     StringBuffer buf = new StringBuffer();     // Checks for the number of arguments on      // the command line, then loops through them     for (int i=0; i < args.length; i++) {     // For each argument besides the first,      // append comma to end of buffer       if (i != 0) {         buf.append(", ");       }       // Add all arguments to the       // StringBuffer object buf.       buf.append(args[i]);     }     // Converts the StringBuffer arguments     // to simple strings and prints to screen.     System.out.println(buf.toString());   }}
相关栏目:

用户点评