13-01 Java StringBuffer class, StringBuilder class

@H_ 301_ 0@StringBuffer Class construction method

<span style="color: #008000;">/*<span style="color: #008000;">

  • 线程安全(多线程讲解)

  • 安全 -- 同步 -- 数据是安全的

  • 不安全 -- 不同步 -- 效率高一些

  • 安全和效率问题是永远困扰我们的问题。

  • 安全:医院的网站,银行网站

  • 效率:新闻网站,论坛之类的

  • StringBuffer:

  • 线程安全的可变字符串。

  • StringBuffer和String的区别?

  • 前者长度和内容可变,后者不可变。

  • 如果使用前者做字符串的拼接,不会浪费太多的资源。

  • StringBuffer的构造方法:

  • <span style="color: #ff0000;"> public StringBuffer():无参构造方法

  • public StringBuffer(int capacity):指定容量的字符串缓冲区对象

  • public StringBuffer(String str):指定字符串内容的字符串缓冲区对象

  • StringBuffer的方法:

  • <span style="color: #ff0000;">public int capacity():返回当前容量。 理论值

  • public int length():返回长度(字符数)。 实际值
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> StringBufferDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> public StringBuffer():无参构造方法
    StringBuffer sb = <span style="color: #0000ff;">new<span style="color: #000000;"> StringBuffer();
    System.out.println("sb:" +<span style="color: #000000;"> sb);
    System.out.println("sb.capacity():" +<span style="color: #000000;"> sb.capacity());
    System.out.println("sb.length():" +<span style="color: #000000;"> sb.length());
    System.out.println("--------------------------"<span style="color: #000000;">);

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public StringBuffer(int capacity):指定容量的字符串缓冲区对象</span>
     StringBuffer sb2 = <span style="color: #0000ff;"&gt;new</span> StringBuffer(50<span style="color: #000000;"&gt;);
     Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"sb2:" +<span style="color: #000000;"&gt; sb2);
     Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"sb2.capacity():" +<span style="color: #000000;"&gt; sb2.capacity());
     Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"sb2.length():" +<span style="color: #000000;"&gt; sb2.length());
     Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"--------------------------"<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; public StringBuffer(String str):指定字符串<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>的字符串缓冲区对象</span>
     StringBuffer sb3 = <span style="color: #0000ff;"&gt;new</span> StringBuffer("hello"<span style="color: #000000;"&gt;);
     Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"sb3:" +<span style="color: #000000;"&gt; sb3);
     Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"sb3.capacity():" + sb3.capacity());<span style="color: #ff0000;"&gt;//16+5</span>
     Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("sb3.length():" +<span style="color: #000000;"&gt; sb3.length());

    }
    }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>