`
kimi0912
  • 浏览: 13006 次
  • 性别: Icon_minigender_1
  • 来自: 天津
最近访客 更多访客>>
社区版块
存档分类
最新评论

Why string is immutable?

    博客分类:
  • Java
阅读更多

From : http://sureshk37.wordpress.com/2007/12/18/why-string-is-immutable/

 

String in a class it is used to holding the array of characters. The difference between String and StringBuffer is String is immutable where as StringBuffer is mutable. Means we can not change the value of the string.

Why it so?

Actually in java, Strings are handling in Pool format.

For example:

String str1 = “xyz”;

This string(str1) will be stored into memory in particular address. When we defining new String with same array of characters like

String str2 = “xyz”;

Now JVM will check in String Pool where there is same characters are available or not. If two Strings are match the JVM will refer str1 address to str2. Now the str1 and str2 referring the same characters in same memory location. This is a good idea for increasing memory efficiency.
When we change the str1 characters, the changes will be reflected to str2. Because both str1 and str2 variables are referring the same memory location. For avoiding this we are keeping String as immutable. However we can use StringBuffer if you want to do modifications in the string.

From:http://www.velocityreviews.com/forums/t146183-why-string-is-immutable.html

Java has this thing called a SecurityManager, and it is responsible for checking that
unprivileged code (for example, an applet) is allowed to do stuff. To
pick one example, applets are typically allowed to contact their server
of origin with sockets, but not other servers. So let's say I write
this:

String hostName = "my.origin.server.com";
Socket s = new Socket(hostName, 1234);

Looks fine. In the second line, the SecurityManager will first check to
be sure that "my.origin.server.com" really is the origin server. Then
it will establish a connection to that server. But this only works if
the server name can't change between the security check and establishing
the connection. If String were not immutable, I could do this:

 

String fakeHostName = "my.origin.server.com";
String realHostName = "evil.server.com";

String hostName = new String(fakeHostName);

new Thread() {
	public void run()
	{
		while (true)
		{
			hostName.mutateTo(realHostName);
			hostName.mutateTo(fakeHostName);
		}
	}
}.start();

boolean success = false;
while (!success)
{
	try
	{
		Socket s = new Socket(hostName, 1234);
		// try talking to evil.server.com

		success = true;
	}
	catch (SecurityException e) { }
}
 



It might take a while, but eventually, a context switch would happen
between the security check and establishing the connection. When that
happens, security is compromised.

See how that works? It's not impossible to code around, but if String
were mutable it would be a lot bigger pain to write methods that make
proper use of java.security.AccessController.doPrivileged.

分享到:
评论

相关推荐

    前端开源库-immutable-core

    **一、什么是Immutable?** “Immutable”在编程中意味着一旦创建的数据对象就不能被修改。这种特性在JavaScript这样的动态语言中尤为重要,因为它可以防止意外的数据篡改,提高代码的预测性和安全性。Immutable.js...

    前端大厂最新面试题-immutable.docx

    一、什么是 Immutable? Immutable 是指一旦创建,就不能再被更改的数据对象。任何对 Immutable 对象的修改或添加删除操作都会返回一个新的 Immutable 对象,而不是修改原来的对象。Immutable 实现的原理是 ...

    is-immutable

    是不可变的 从公开isImmutable函数,但不包含Immutable库的其余部分。 用法 import isImmutable from 'is-immutable' ; isImmutable ( someObj ) ; // => boolean

    Android代码-okio

    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

    Immutable详解及React中实践.pdf

    "Immutable 详解及 React 中实践" Immutable 是一种编程概念,指的是一旦创建,就不能再被更改的数据。它可以解决共享的可变状态问题,提高应用的性能和可维护性。本文将对 Immutable 的概念、实现原理、常见库和在...

    java基础String类选择题练习题

    - **知识点**:解释为什么`String`类是不可变的(immutable),以及不可变性带来的好处。 - **例题**:选择题中可能会问到关于字符串池的概念以及如何利用这一点来提高程序性能。 #### 2. 字符串比较 - **知识点**...

    Docker and Immutable Infrastructure

    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.

    正确使用String类的几点注意

    这种方式创建的String对象位于常量池中,具有不可变性(immutable),即一旦创建后其内容就不能改变。 **1.2 使用new关键字** 另一种常见的初始化方法是使用`new`关键字: ```java String s = new String("hello")...

    attrtools:小型命令行应用程序,用于更改ext fs家族文件的“不可变”属性

    Is Immutable?attrtools + i <路径>为文件添加“不可变”标志attrtools -i <路径>删除文件的“不可变”标志attrtools 来查询文件标志例子须藤./attrtools + i libe2.o File: libe2.o ./attrtools libe2.o ...

    前端开源库-immutable-ai

    **前端开源库 Immutable.js** Immutable.js 是一个 JavaScript 库,由 Facebook 的 React 团队开发并维护,它为开发者提供了一种在前端开发中高效处理数据的方法。该库的核心概念是不可变数据,即一旦创建,数据就...

    技术分享之immutable.js.pptx

    简单介绍Immutable.js的一个ppt,主要用于技术分享,内容为imuutabe常用的数据类型和常用的基础api,还有immutable的原理、优缺点的分析。

    java英文笔试

    What is immutable? **Answer**: An object is considered immutable if its state cannot be changed after it is created. In Java, the `String` class is a classic example of an immutable class. Once a `...

    Mastering Immutable.js epub

    Mastering Immutable.js 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    react-react面试题之对immutable的理解.zip

    二、为何需要使用Immutable? 1. **优化性能**:由于React组件默认是浅比较(shallow compare),如果对象或数组引用没有改变,React会认为组件未发生改变,从而跳过不必要的渲染。使用不可变数据结构可以确保每次...

    Android代码-kotlinx.collections.immutable

    Immutable Collections Library for Kotlin Immutable collection interfaces and implementation prototypes for Kotlin. For further details see the proposal. Prototype implementation is based on ...

    immutable在redux中的使用

    什么是immutable Data? Immutable Data 就是一旦创建,就不能再被更改的数据。对 Immutable 对象的任何修改或添加删除操作都会返回一个新的 Immutable 对象。Immutable 实现的原理是 Persistent Data Structure...

    String容量大小区分

    Java中String对象具有不可变性(immutable)的特点,这意味着一旦创建了一个String对象,其内容就不能被更改。这种特性虽然确保了字符串的安全性和线程安全性,但也可能在某些情况下导致额外的内存消耗。因此,理解...

    深入探讨Java中的String类.pdf

    Java 中的 String 类是一个特殊的类,它是一个 immutable 类,也就是说,一旦创建了 String 对象,它的值就不能被改变。String 类的 immutable 性质是通过 final 关键字实现的。 二、String 对象的创建 Java 中有...

Global site tag (gtag.js) - Google Analytics