博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为什么在Java中String是不可变的?
阅读量:2532 次
发布时间:2019-05-11

本文共 3014 字,大约阅读时间需要 10 分钟。

Why String is immutable in Java is one of the popular interview questions. The string is one of the most used classes in any programming language. We know that String is immutable and final in Java. Java runtime maintains a that makes it a special class.

为什么String在Java中是不可变的,这是受欢迎的面试问题之一。 字符串是任何编程语言中最常用的类之一。 我们知道String在Java中是不变的且是最终的。 Java运行时维护一个使它成为特殊类的 。

为什么在Java中String是不可变的? (Why String is immutable in Java?)

Let’s look at some of the benefits of String immutability, that will help in understanding why String is immutable in Java.

让我们看一下String不变性的一些好处,这将有助于理解为什么String在Java中是不变的。

  1. is possible only because String is immutable in Java. This way Java Runtime saves a lot of because different String variables can refer to the same String variable in the pool. If String would not have been immutable, then String interning would not have been possible because if any variable would have changed the value, it would have been reflected in the other variables too.

    仅在Java中字符串是不可变的,所以才可能使用 。 这样,Java运行时可以节省大量因为不同的String变量可以引用池中的同一String变量。 如果String不会是不可变的,那么String实习将是不可能的,因为如果有任何变量更改了值,那么它也将反映在其他变量中。
  2. If String is not immutable then it would cause a severe security threat to the application. For example, database username, password are passed as String to get database connection and in host and port details passed as String. Since String is immutable, its value can’t be changed otherwise any hacker could change the referenced value to cause security issues in the application.

    如果String不可更改,那么它将对应用程序造成严重的安全威胁。 例如,数据库用户名,密码作为String传递以获取数据库连接,并在以String形式传递主机和端口详细信息。 由于String是不可变的,因此无法更改其值,否则任何黑客都可以更改引用的值,从而在应用程序中引起安全问题。
  3. Since String is immutable, it is safe for . A single String instance can be shared across different threads. This avoids the use of synchronization for thread safety. Strings are implicitly thread-safe.

    由于String是不可变的,因此是安全的。 单个String实例可以在不同线程之间共享。 这避免了为了线程安全而使用同步。 字符串是隐式线程安全的。
  4. Strings are used in and immutability provides security that correct class is getting loaded by Classloader. For example, think of an instance where you are trying to load java.sql.Connection class but the referenced value is changed to myhacked.Connection class that can do unwanted things to your database.

    字符串用于并且不变性提供了确保正确的类正在由类加载器加载的安全性。 例如,考虑一个您尝试加载java.sql.Connection类,但引用值更改为myhacked.Connection类的实例,该实例可能会对数据库造成不良影响。
  5. Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for the key in a Map and its processing is faster than other HashMap key objects. This is why String is the most widely used as HashMap keys.

    由于String是不可变的,因此其哈希码将在创建时进行缓存,因此无需再次计算。 这使其成为Map中密钥的理想选择,并且其处理比其他HashMap密钥对象更快。 这就是String最广泛用作HashMap键的原因。

Above are some of the reasons I could think of that shows benefits of String immutability. It’s a great feature of the and makes it special.

以上是我想到的一些原因,这些原因表明了字符串不可变性的好处。 它是的一个强大功能,并且使其与众不同。

Read this post to know how to write your own .

阅读这篇文章,了解如何编写自己的 。

. 签出更多Java String示例。

翻译自:

转载地址:http://gmozd.baihongyu.com/

你可能感兴趣的文章
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>