zoukankan      html  css  js  c++  java
  • java 12-1 StringBuffer类


      线程安全(多线程讲解)
          安全 -- 同步 -- 数据是安全的--效率低一些
          不安全 -- 不同步 -- 数据不安全--效率高一些
          安全和效率问题是永远困扰我们的问题。
            安全:医院的网站,银行网站
            效率:新闻网站,论坛之类的

      StringBuffer:
          线程安全的可变字符串。

      StringBuffer和String的区别?
          前者长度和内容可变,后者不可变。
          如果使用前者做字符串的拼接,不会浪费太多的资源。

      StringBuffer的构造方法:
        1、public StringBuffer():无参构造方法
        2、public StringBuffer(int capacity):指定容量的字符串缓冲区对象
        3、public StringBuffer(String str):指定字符串内容的字符串缓冲区对象

      StringBuffer的方法:
        1、public int capacity():返回当前容量 理论值
        2、public int length():返回长度(字符数) 实际值

     1 public class StringBufferDemo {
     2 public static void main(String[] args) {
     3 // public StringBuffer():无参构造方法
     4 StringBuffer sb = new StringBuffer();
     5 System.out.println("sb:" + sb);
     6 System.out.println("sb.capacity():" + sb.capacity());
     7 System.out.println("sb.length():" + sb.length());
     8 System.out.println("--------------------------");
     9 
    10 // public StringBuffer(int capacity):指定容量的字符串缓冲区对象
    11 StringBuffer sb2 = new StringBuffer(50);
    12 System.out.println("sb2:" + sb2);
    13 System.out.println("sb2.capacity():" + sb2.capacity());
    14 System.out.println("sb2.length():" + sb2.length());
    15 System.out.println("--------------------------");
    16 
    17 // public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
    18 StringBuffer sb3 = new StringBuffer("hello");
    19 System.out.println("sb3:" + sb3);
    20 System.out.println("sb3.capacity():" + sb3.capacity());
    21 System.out.println("sb3.length():" + sb3.length());
    22 }
    23 }

    StringBuffer一部分功能和String一样,后面说的是StringBuffer特有的功能,详情看API

    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    微信二维码 场景二维码 用于推送事件,关注等 注册用户 ,经过测试
    简单的 Helper 封装 -- CookieHelper
    简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
    Java反射机制
    Windows Azure Web Site (13) Azure Web Site备份
    Windows Azure Virtual Machine (1) IaaS用户手册
    Windows Azure Web Site (1) 用户手册
    Windows Azure Web Site (12) Azure Web Site配置文件
    Windows Azure Web Site (11) 使用源代码管理器管理Azure Web Site
    Windows Azure Web Site (10) Web Site测试环境
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5877352.html
Copyright © 2011-2022 走看看