首页 > Java > java教程 > 正文

Java 中的 StringBuffer

WBOY
发布: 2024-08-30 16:01:56
原创
547 人浏览过

今天我们将看到java中的String Buffer。首先,我们来谈谈java。 Java 是一种面向对象的编程语言。此前,我们一直在使用c、c++语言。对于这些语言,我们遇到了平台问题。 Java 是一种独立于平台的语言。这意味着 java 可以在任何操作系统上运行,如 Linux、MAC、Windows 等。在本主题中,我们将学习 Java 中的 StringBuffer。

当今世界上有近 30 亿台设备在 Java 上运行。似乎是永无止境的技术。下面列出了一些功能。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

  • Java 有很多可重用的代码。
  • 大量的库
  • 平台独立
  • 自动垃圾收集。
  • 安全
  • 强大的社区支持

在开始了解字符串Buffer类之前,我们必须了解java在幕后是如何工作的。

Java 拥有我们所知的编译语言。到底是什么意思?我们都知道计算机以二进制形式识别语言。即,o 和 1。我们需要一个编译器来理解用java编写的程序。 Java 编译器借助以下命令将此程序转换为字节码。

Javac Someprogram.java

此命令创建 .class 文件。此文件正在计算机上运行。

现在我们知道 java 程序到底发生了什么。

现在让我们从 StringBuffer 开始。

Java中的StringBuffer是什么?

在java中,字符串一旦创建就无法更改。如果我们想改变字符串或为字符串分配新值是不可能的。它创建一个字符串对象,然后分配值。这里,String Buffer 类就派上用场了。借助String Buffer,我们可以修改字符串。

字符串是什么?

然后一个明显的问题出现在你的脑海中,那么字符串是什么?该字符串是字符.

的组合

看下面的例子。

String city = new String("Mumbai");
登录后复制

我们可以创建一个空字符串,如下所示:

String city = new String();
登录后复制

我们还有字符串数组。看下面的例子:

String myArray1 = new String[3];
登录后复制

上面的示例创建了一个包含三个常量的字符串数组。

字符串缓冲区在java中是如何工作的?

在 StringBuffer java 的帮助下,字符串变得可变。

这种可变性到底意味着什么?看下面的例子。如果我们重新给java中的字符串变量赋值,就会抛出错误,因为java中的字符串是不可变的。

public class Str {
public static void main(String args[]){
//We are creating object here of StringBuffer class
StringBuffer city = new Stringbuffer("mumbai");
//let’s try to change the value of city here
City =" Delhi"
}
}
登录后复制

上面的代码将获取错误,因为字符串是不可变的。

解释 StringBuffer 类

java.lang.StringBuffer 类是一个线程安全类。这意味着多个线程无法同时访问它们。它具有可变的字符序列。

StringBuffer类与字符串相同,但它是可变的,而字符串是不可变的。每个 StringBuffer 的容量为 16 个字符,无需重新分配。它允许将项目添加到字符串或字符串开头、中间或结尾处的子字符串。 StringBuffer 是一个可增长的类。

StringBuffer 方法按顺序工作。我们将看到这些方法以及它们是如何工作的。

StringBuffer 构造函数:

StringBuffer r = new StringBuffer();
登录后复制

这将创建一个 StringBuffer 类类型的空对象 r。

Java中StringBuffer类的方法(含示例)

String Buffer 类为我们提供了不同的方法来克服这个问题。

下面列出了一些方法。

StringBuffer Methods StvStringBuffer method use
void ensureCapacity() It sets the limit for the character.
str1.append(str2) It appends the str2 into string str1
str1.setCharAt(n, ‘x’) Modifies the nth character to x
str1.setLength(n) This length of str1 to the n characters
str1.insert(n, str2) This inserts str2 at the nth position in str1
Int capacity() This returns the total allocated capacity
Int length() It returns the current length of the object
StringBuffer 方法 StvStringBuffer 方法使用 无效的ensureCapacity() 它设置字符的限制。 str1.append(str2) 它将 str2 附加到字符串 str1 str1.setCharAt(n, ‘x’) 将第 n 个字符修改为 x str1.setLength(n) str1 的长度到 n 个字符 str1.insert(n, str2) 这会将 str2 插入到 str1 中的第 n 个位置 整数容量() 这将返回分配的总容量 整数长度() 它返回对象的当前长度 表>

The above methods help the programmer to make the string mutable.

StringBuffer Methods in detail

Let’s look at the below program:

Save the below program with Example.java

class Example{
public static void main(String args[]){
StringBuffer abc = new StringBuffer("Hello World");
System.out.println("Welcome!!!"+ abc);
System.out.println("Total length of string is" + abc.length());
for(int i=0;i<=abc.length();i++){
int n = i+1;
System.out.println("We are using charAt function here :" + " " + n + " " + abc.charAt(i));
}
}
}
登录后复制

To run the above program, open the command prompt. Go to the location where the file is saved.

Then type the following two programs:

Javac Example.java

Java Example

ensureCapacity()

ensureCapacity() is the measure of the capacity to equal the specified minimumCapacity. That means the current capacity of StringBuffer is less than the argument of minimumCapacity so that the new internal array will get allocated with greater capacity.

class Ex2{
public static void main(String[] args)
{
StringBuffer abc = new StringBuffer();
// print string capacity
System.out.println("Before ensureCapacity the value is: "
+ "method capacity = "
+ abc.capacity());
abc.ensureCapacity(20);
System.out.println("After ensureCapacity the value is: + " method capacity = "
+ abc.capacity());
}
}
登录后复制

append() Method

append() method is used to append the value at the specified location.

The following example describes how we can append the string at the end of the existing string. This is one way to modify the string.

Example

class Ex2{
public static void main(String[] args)
{
StringBuffer abc = new StringBuffer("Welcome to the world ");
System.out.println("The string is:"  + abc );
abc.append("of java");
System.out.println("The string after appending is:"  + abc );
}
}
登录后复制

By appending a new string into the same variable, in the end, we modified the value of the string successfully.

In java, there are several predefined functions. We cannot write all the functions in a single article. But we can practice these functions one at a time. With the StringBuffer class, there are many methods to a string also.

Conclusion

StringBuffer class is used to make string mutable. This is an added advantage for us. If you are in the learning phase of java, you should learn how to play with strings. This is the initial level to start practicing programs written in java.

以上是Java 中的 StringBuffer的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!