为什么我们的String类,如果StringBuilder的或StringBuffer的可以做一个String做什么? [英] Why do we have String class, if StringBuilder or StringBuffer can do what a String does?

查看:119
本文介绍了为什么我们的String类,如果StringBuilder的或StringBuffer的可以做一个String做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道为什么Java和C#有字符串(不变&安培;线程)类,如果他们有的StringBuilder (可变&安培;没有线程)或的StringBuffer (可变&安培;线程)类。是不是字符串类的<​​code>的StringBuilder / 的StringBuffer 集?我的意思是,我为什么要使用字符串类,如果我使用的选项的StringBuilder / StringBuffer的



例如,而不是使用以下的,

 字符串str; 



为什么我不能总是用下面的?



 的StringBuilder STRB; //或
的StringBuffer strbu;

在总之,我的问题是,如何将我的代码得到,如果我取代生效字符串的StringBuffer 类?此外,的StringBuffer 增添优势的可变性。


解决方案

我的意思是,我为什么要使用String类的,如果我使用的StringBuilder / StringBuffer的问题选项?




正因为这是不可改变的。不变性有一个整体的的好处主机的,主要是,它使得它更容易推理你的代码,而无需以防万一无处不在创建数据的复制的东西决定变异的价值。例如:

 私人只读字符串名称; 

公众人物(字符串名称)
{
如果(string.IsNullOrEmpty(名))//或任何
{
//抛出一些例外
}
this.name =名称;
}

//所有代码的其余部分可以依靠的名字是一个非空
//引用一个非空字符串。没有任何东西可以变异它,留下
//邪恶反射一边。



不变性,使共享简单高效。这对于多线程代码特别有用。它使修改(即创建具有不同数据的新实例)更痛苦的,但在那精绝,因为值通过系统而没有被修改的许多情况。



不变性的尤其的为简单的类型,比如字符串,日期,数字(用的BigDecimal 的BigInteger 等)。这使他们能映射内更容易被使用,它可以让一个简单的定义平等,等等。


I've always wondered why does JAVA and C# has String (immutable & threadsafe) class, if they have StringBuilder (mutable & not threadsafe) or StringBuffer (mutable & threadsafe) class. Isn't StringBuilder/StringBuffer superset of String class? I mean, why should I use String class, if I've option of using StringBuilder/StringBuffer?

For example, Instead of using following,

String str;

Why can't I always use following?

StringBuilder strb; //or
StringBuffer strbu;

In short, my question is, How will my code get effected if I replace String with StringBuffer class? Also, StringBuffer has added advantage of mutability.

解决方案

I mean, why should I use String class, if I've option of using StringBuilder/StringBuffer?

Precisely because it's immutable. Immutability has a whole host of benefits, primarily that it makes it much easier to reason about your code without creating copies of the data everywhere "just in case" something decides to mutate the value. For example:

private readonly String name;

public Person(string name)
{
    if (string.IsNullOrEmpty(name)) // Or whatever
    {
        // Throw some exception
    }
    this.name = name;
}

// All the rest of the code can rely on name being a non-null 
// reference to a non-empty string. Nothing can mutate it, leaving
// evil reflection aside.

Immutability makes sharing simple and efficient. That's particularly useful for multi-threaded code. It makes "modifying" (i.e. creating a new instance with different data) more painful, but in many situations that's absolutely fine, because values pass through the system without ever being modified.

Immutability is particularly useful for "simple" types such as strings, dates, numbers (BigDecimal, BigInteger etc). It allows them to be used within maps more easily, it allows a simple equality definition, etc.

这篇关于为什么我们的String类,如果StringBuilder的或StringBuffer的可以做一个String做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆