Java StringBuffer void trimToSize()方法(带示例)

StringBuffer类void trimToSize() (StringBuffer Class void trimToSize())

  • This method is available in package java.lang.StringBuffer.trimToSize().

    软件包java.lang.StringBuffer.trimToSize()中提供了此方法。

  • This method is used to reduce or save memory space available for the StringBuffer object or in other words, we can say it is used to trim the size of the given object.

    此方法用于减少或节省StringBuffer对象可用的内存空间,换句话说,可以说它用于调整给定对象的大小。

  • If memory space is larger than the holding object, in that case, we need to reduce space for future purpose.

    如果存储空间大于保存对象的空间,则需要减少空间以备将来使用。

Syntax:

句法:

    void trimToSize(){
    }

Parameter(s):

参数:

We don't pass any object in the method of the StringBuffer.

我们没有在StringBuffer的方法中传递任何对象。

Return value:

返回值:

The return type of this method is void that means this method returns nothing.

此方法的返回类型为void ,表示此方法不返回任何内容。

Java程序,演示trimToSize()方法的示例 (Java program to demonstrate example of trimToSize() method)

import java.lang.StringBuffer;

public class StringBufferClass {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer(" StringBuffer class is ");

        // use append(String s) append the string to the StringBuffer object
        sb.append("java.lang");
        sb.trimToSize();

        // Display result after implementing trimToSize()

        System.out.println("The result will be after implementing method trimToSize() is :" + sb);

        sb = new StringBuffer("Current version of java is :");

        // use append(String s) append the string to the StringBuffer object
        sb.append("8");
        sb.trimToSize();

        // Display result after implementing trimToSize()

        System.out.println("The result will be after implementing method trimToSize() is :" + sb);
    }
}

Output

输出量

D:\Programs>javac StringBufferClass.java

D:\Programs>java StringBufferClass
The result will be after implementing method trimToSize() is : StringBuffer class is java.lang
The result will be after implementing method trimToSize() is :Current version of java is :8


翻译自: https://www.includehelp.com/java/stringbuffer-void-trimtosize-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值