zoukankan      html  css  js  c++  java
  • JavaSE 高级 第04节 StringBuffer类

    2016-07-24

    1,StringBuffer简介

             StringBuffer sb=new StringBuffer();

             System.out.println(sb.capacity());        

             StringBuffer sb2=new StringBuffer("abc");

             System.out.println(sb2.capacity());

             StringBuffer sb3=new StringBuffer(100);

             System.out.println(sb3.capacity());

    2,StringBuffer类的方法

             线程安全的。

    package com.java1995;
    
    public class TestStringBuffer {
    
        public static void main(String[] args) {
            StringBuffer sb = new StringBuffer();
            System.out.println(sb.capacity());
    
            StringBuffer sb2 = new StringBuffer("abc");
            System.out.println(sb2.capacity());
    
            StringBuffer sb3 = new StringBuffer(100);
            System.out.println(sb3.capacity());
    
            StringBuffer sb4 = new StringBuffer("Hello");
            sb4.append(",");
            sb4.append("world!
    ");
            sb4.append("welcome to ");
            sb4.append("www.java1995.com");
    
            System.out.println(sb4.toString());
        }
    }

    package com.java1995;
    
    public class TestStringBuffer2 {
    
        public static void main(String[] args) {
            StringBuffer sb = new StringBuffer("helloworld");
            System.out.println(sb);
            for (int i = 0; i < sb.length(); i++) {
                char c = sb.charAt(i);
    
                c = (char) (c - 32);
                sb.setCharAt(i, c);
            }
            System.out.println(sb);
        }
    }

    package com.java1995;
    
    import java.util.Date;
    
    public class TestStringBuffer3 {
    
        public static void main(String[] args) {
            // System.out.println(new Date());
            // String str="";
            // for(int i=0;i<100000;i++){
            // str+=i;
            // }
            // System.out.println(new Date());
            // 18秒
            System.out.println(new Date());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 100000; i++) {
                sb.append(i);
            }
            System.out.println(new Date());
        }
    }

    【参考资料】

    [1] Java轻松入门经典教程【完整版】

  • 相关阅读:
    robot framework 文本校验,文本与关键字重复的处理
    python pip切本地源
    sqlalchemy 多个数据库地址配置
    python SQLAlchemy的简单配置和查询
    根据列表中字典中的某个value排序
    python SQLAlchemy中子查询subquery的使用
    python SQLAlchemy中query与query()
    weekly paper read
    C++调用windowsAPI
    English 邮件
  • 原文地址:https://www.cnblogs.com/cenliang/p/5701133.html
Copyright © 2011-2022 走看看