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轻松入门经典教程【完整版】

  • 相关阅读:
    MS Office CVE-2015-1641 恶意 Exploit 样本分析
    Qbot回归,已感染5.4万台计算机
    工具推荐:Backdoor-apk,安卓APK文件后门测试工具
    安卓微信、QQ自带浏览器 UXSS 漏洞
    延迟注入工具(python)
    小白欢乐多——记ssctf的几道题目
    使用转义防御XSS
    富文本存储型XSS的模糊测试之道
    k8s故障总结
    CentOS7.6部署k8s环境
  • 原文地址:https://www.cnblogs.com/cenliang/p/5701133.html
Copyright © 2011-2022 走看看