原文地址: http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html
This is one of the most popular interview question on String in Java which starts with discussion of What is immutable object , what are the benefits of immutable object , why do you use it and which scenarios do you use it.
It can also come once interviewee answers some preliminarily strings questions e.g. What is String pool , What is the difference between String and StringBuffer , What is the difference between StringBuffer and StringBuilder etc.
Though there could be many possible answer for this question and only designer of String class can answer this , I think below two does make sense
1)Imagine StringPool facility without making string immutable , its not possible at all because in case of string pool one string object/literal e.g. "Test" has referenced by many reference variables , so if any one of them change the value others will be automatically gets affected i.e. lets say
String A = "Test"
String B = "Test"
Now String B called "Test".toUpperCase() which change the same object into "TEST" , so A will also be "TEST" which is not desirable.
2)String has been widely used as parameter for many java classes e.g. for opening network connection you can pass hostname and port number as stirng , you can pass database URL as string for opening database connection, you can open any file by passing name of file as argument to File I/O classes.
In case if String is not immutable , this would lead serious security threat , I mean some one can access to any file for which he has authorization and then can change the file name either deliberately or accidentally and gain access of those file.
3)Since String is immutable it can safely shared between many threads ,which is very
important for multithreaded programming.
I believe there could be some more very convincing reasons also , Please post those reasons as comments and I will include those on this post.
下面是一些回复:
(1)
sandeep said...
Hi,
I have a question regarding String Constant Pool.
What will happend if the pool size reached its maximum limit?
Wheather it will remove least recently used Strings or it wilol throws out of memory error or some GC will happen over unreferenced String object in the Constant pool
January 18, 2011 2:20 AM
Javin @ Tibco RV Tutorial said...
Hi Sandeep,
As per my knowledge ,String pool gets created in PERM area of Java Heap , so if there are too many String there is good chance of OutOfMemoryError because garbage collection doesn't happen in PERM area of java heap.
Thanks
Javin
January 19, 2011 3:59 AM
(2)
Sandeep said...
Very good. I used to think of only the thread related reason. The other two are also quite valid.
http://extreme-java.blogspot.com
January 20, 2011 5:44 AM
Javin @ Tibco RV Tutorial said...
Thanks Sandeep , Good to see you here back. Given extensive usage of String in any Java application and immutability benefit it provides as you pointed out which make it preferred choice in case of hashmap key or sharing between multiple thread everybody should know more about String Pool and behavior of String class.this is mine initiative
January 20, 2011 6:40 AM
(3)
Dan Bergh Johnsson said...
The absolutely most important reason that String is immutable is that it is used by the class loading mechanism, and thus have profound and fundamental security aspects.
Had String been mutable, a request to load "java.io.Writer" could have been changed to load "mil.vogoon.DiskErasingWriter".
January 21, 2011 12:39 PM
分享到:
相关推荐
java java_leetcode题解之Range Sum Query - Immutable.java
根据提供的信息,我们可以总结出这份Java基础String类选择题练习题主要聚焦于String及StringBuffer类的使用。尽管具体的题目内容未给出,但从所展示的信息中可以推断出该练习题集涵盖了以下几方面的知识点: ### 一...
Java 中的 String 类是一个特殊的类,它是一个 immutable 类,也就是说,一旦创建了 String 对象,它的值就不能被改变。String 类的 immutable 性质是通过 final 关键字实现的。 二、String 对象的创建 Java 中有...
`String`类是不可变的(immutable),这意味着一旦一个`String`对象被创建,它的内容就不能被改变。这种特性使得`String`类非常适合在多线程环境中使用,因为不需要担心其内容被多个线程同时修改。 #### 二、String...
Okio Okio is a library that complements java.io and java.nio to make it ...ByteString is an immutable sequence of bytes. For character data, String is fundamental. ByteString is String's long-lost bro
这种方式创建的String对象位于常量池中,具有不可变性(immutable),即一旦创建后其内容就不能改变。 **1.2 使用new关键字** 另一种常见的初始化方法是使用`new`关键字: ```java String s = new String("hello")...
字符串一旦创建后不可改变,这是因为它被设计成不可变对象(`immutable object`)。Java中主要有两种方式来创建字符串: 1. **使用字符串常量直接初始化**: ```java String s = "hello!"; ``` 这种方式简单...
"Java 中 Object 对象和 String 对象的解析" Java 中的 Object 对象和 String 对象是两个非常重要的概念。在 Java 中,每个对象都继承自 Object 对象,这意味着每个对象都拥有 Object 对象的方法和属性。String ...
Java中的不可变类(immutable)是一种特殊的类,其对象一旦创建后,其状态就不能再发生变化。这类类在Java中有着重要的地位,特别是String类,它是Java中最常用的不可变类之一。不可变类的设计旨在提高安全性、效率...
### Immutable Objects in Java 在Java编程语言中,不可变对象(Immutable Objects)是一个重要的概念,尤其是在构建健壮、易于维护的应用程序时。本篇将基于提供的文件内容来深入探讨不可变对象的概念及其在Java中...
`String`对象是不可变的(immutable),这意味着一旦一个`String`对象创建后,其内容不能被修改。这带来了几个关键的优势: 1. **线程安全**:由于`String`对象不可变,多个线程可以安全地共享同一个`String`对象,...
是不可变的 从公开isImmutable函数,但不包含Immutable库的其余部分。 用法 import isImmutable from 'is-immutable' ; isImmutable ( someObj ) ; // => boolean
`String`类是Java中最基础也是最重要的数据类型之一,在Java中被定义为一个不可变类(immutable class),这意味着一旦一个`String`对象创建之后,其内容就不能再被修改。`String`类提供了丰富的内置方法来操作字符...
例如,使用 Immutable.js 库,可以使用 setIn 和 getIn 方法来更新和获取数据,而不是使用原生的赋值和获取方法。这样可以避免共享的可变状态问题,提高应用的性能和可维护性。 Immutable 是一种编程概念,可以解决...
提供Java不变/持久集合类的库。 尽管集合是不可变的,但它们提供了通过创建自身的新修改副本来添加和删除值的方法。 每个副本与其他副本尽可能共享其结构,以最大程度地减少内存消耗。 该库包括单链接(cons / cddr...
In Java, the `String` class is a classic example of an immutable class. Once a `String` object is created, its value cannot be modified. Immutable objects are useful for ensuring thread safety and ...
The release of Java 9 has brought many subtle and not-so-subtle changes to the way in which Java programmers approach their code. The most important ones are definitely the availability of a REPL, ...
Java中String对象具有不可变性(immutable)的特点,这意味着一旦创建了一个String对象,其内容就不能被更改。这种特性虽然确保了字符串的安全性和线程安全性,但也可能在某些情况下导致额外的内存消耗。因此,理解...
1.Overview of Docker the company and growth 2.Overview of Docker the latest stuff (DCUS announcements) & CaaS;...4.Docker and Immutable infrastructure/Microservices working together.