Why String has been made immutable in Java?

Fabric学习笔记(一)- Centos 7下的Fabric 2.3.2环境搭建 一、 环境准备 (一)、在VMware Workstation 15 安装一个Centos 7的虚拟环境,切换到图形化界面: 参考文章链接:Centos 7切换图形化界面 其中可能在yum groupinstall "X Window System"时会报错:Cannot find a valid baseurl for repo: base/7/x86_6,参考这篇文章即可:安装图形化界面报错解决 (二)、打开终端,安装所需的库: 切换root用户,输入命令:su root yum更新,输入命令:y 阅读详情


Why String has been made immutable in Java?

Though, performance is also a reason (assuming you are already aware of the internal String pool maintained for making sure that the same String object is used more than once without having to create/re-claim it those many times), but the main reason why String has been made immutable in Java is 'Security'. Surprised? Let's understand why.


Suppose you need to open a secure file which requires the users to authenticate themselves. Let's say there are two users named 'user1' and 'user2' and they have their own password files 'password1' and 'password2', respectively. Obviously 'user2' should not have access to 'password1' file.


As we know the filenames in Java are specified by using Strings. Even if you create a 'File' object, you pass the name of the file as a String only and that String is maintained inside the File object as one of its members.


Had String been mutable, 'user1' could have logged into using his credentials and then somehow could have managed to change the name of his password filename (a String object) from 'password1' to 'password2' before JVM actually places the native OS system call to open the file. This would have allowed 'user1' to open user2's password file. Understandably it would have resulted into a big security flaw in Java.
 I understand there are so many 'could have's here, but you would certainly agree that it would have opened a door to allow developers messing up the security of many resources either intentionally or un-intentionally.

With Strings being immutable, JVM can be sure that the filename instance member of the corresponding File object would keep pointing to same unchanged "filename" String object. The 'filename' instance member being a 'final' in the File class can anyway not be modified to point to any other String object specifying any other file than the intended one (i.e., the one which was used to create the File object).
Why String has been made immutable in Java?


Though, performance is also a reason (assuming you are already aware of the internal String pool maintained for making sure that the same String object is used more than once without having to create/re-claim it those many times), but the main reason why String has been made immutable in Java is 'Security'. Surprised? Let's understand why.


Suppose you need to open a secure file which requires the users to authenticate themselves. Let's say there are two users named 'user1' and 'user2' and they have their own password files 'password1' and 'password2', respectively. Obviously 'user2' should not have access to 'password1' file.


As we know the filenames in Java are specified by using Strings. Even if you create a 'File' object, you pass the name of the file as a String only and that String is maintained inside the File object as one of its members.


Had String been mutable, 'user1' could have logged into using his credentials and then somehow could have managed to change the name of his password filename (a String object) from 'password1' to 'password2' before JVM actually places the native OS system call to open the file. This would have allowed 'user1' to open user2's password file. Understandably it would have resulted into a big security flaw in Java.
 I understand there are so many 'could have's here, but you would certainly agree that it would have opened a door to allow developers messing up the security of many resources either intentionally or un-intentionally.

With Strings being immutable, JVM can be sure that the filename instance member of the corresponding File object would keep pointing to same unchanged "filename" String object. The 'filename' instance member being a 'final' in the File class can anyway not be modified to point to any other String object specifying any other file than the intended one (i.e., the one which was used to create the File object).
2025年三级等保新规实战:从零开始搭建企业安全防护体系(附完整配置清单) 本文详细解析2025年三级等保新规的核心变化,提供从零开始搭建企业安全防护体系的实战指南。重点介绍动态防护能力、实战化攻防验证和全链路数据保护等关键要求,并附完整配置清单,帮助企业高效合规。 阅读详情

相关推荐

避免踩坑!WIN11 22H2更新后共享打印机失效的终极解决方案

本文详细解析了WIN11 22H2更新后共享打印机失效的问题,特别是常见的0x00000709错误代码。通过系统更新机制分析、错误代码解读,提供从基础到高级的完整解决方案,包括注册表修复、组策略调整和驱动兼容性处理,帮助用户彻底解决打印机共享问题并预防未来故障。

pp12345的博客 535

Why String is immutable or final in Java

This is one of the most popular String Interview questions in Java, which starts with discussion of, What is String, How String in Java is different than String in C and C++, and then shifted tow

Schen6 Online Life 989

手把手教你用STM32标准库写一个能OTA升级的Bootloader(附完整代码)

本文详细介绍了如何使用STM32标准库开发支持OTA无线升级的Bootloader系统,包括Flash分区规划、Bootloader核心实现、无线升级流程及安全校验机制。通过实战代码示例,帮助开发者快速掌握STM32固件升级技术,提升物联网设备的远程维护能力。

weixin_30181209的博客 263

Why String is immutable in Java ?--reference

Stringis an immutable class in Java. An immutable class is simply a class whose instances cannot be modified. All information in an instance is initialized when the instance is created and the i...

181

手把手教你解决Ubuntu 20.04虚拟机桥接模式下的网络问题:DHCP与静态IP配置全指南

本文详细解析了Ubuntu 20.04虚拟机在桥接模式下的网络配置问题,包括DHCP与静态IP的配置方法及常见故障排查技巧。通过深入讲解桥接模式原理和提供实战操作指南,帮助用户解决无法访问外网、ping不通等典型网络问题,实现虚拟机与物理网络的无缝连接。

weixin_29256771的博客 543

手把手教你用Node.js模拟大麦APP下单请求(含滑块验证处理)

本文详细解析了如何使用Node.js模拟大麦APP下单请求的全流程,包括接口逆向、滑块验证处理和签名算法逆向等关键技术。通过实战案例和代码示例,帮助开发者掌握移动端API调用的核心要素,如动态参数构造、安全验证突破等,提升自动化下单的成功率和效率。

weixin_42569329的博客 353

Java中String为什么被设计成immutable(不可修改的)/final

原文链接:http://www.programcreek.com/2013/04/why-string-is-immutable-in-java/      String是Java中的一个immutable类。immutable类意味着该类的实例将无法被修改。该实例中的所有信息在其被创建时就已经被完全初始化,同时之后也无法再作修改。对于immutable类而言,其有很多优点。 1)字符串常量池

woliuyunyicai的专栏 1497

汽车UDS诊断之诊断会话控制服务(0x10)深度剖析

DiagnosticSessionControl就是诊断会话控制。 请求消息定义 A_Data类型 参数名称 Cvt 字节值 助记符 #1 诊断会话控制请求SID M 0x10 DSC #2 子功能=[诊断会话类型] M 0x00-0xFF LEV_DS_ ...

心骗小白话的博客 7419

Java基础之【String为什么是immutable】

原文: https://www.javatpoint.com/immutable-string In java,string objects are immutable. Immutable simply means unmodifiable or unchangeable. java中,String对象是不可变的,不可变简单理解就是不能修改或者不能改变 Once string obje...

黄三yeah的Java技术空间 1482

046篇:数据统计:分组、求和、平均值、最大最小值

本文介绍了在RPA中实现数据分组统计的方法。通过字典+循环组合,可以完成按单字段或多字段的分组聚合操作,包括求和、计数、求平均值及最大/最小值等。文章详细讲解了分组统计的原理,并提供了单级分组、多级分组的具体实现步骤,最后通过销售数据按月分组的实战案例,演示了如何从原始数据到生成汇总报表的完整流程。这些方法可以灵活应用于各类数据统计分析场景。

weixin_59649563的博客 214

解读(DivideMix)DIVIDEMIX: LEARNING WITH NOISY LABELS AS SEMI-SUPERVISED LEARNING

这是Salesforce在今年年初出的一篇关于半监督学习的文章被ICLR 2020收录,目前还保持在SOTA状态。 文章主要做了两点工作: • 提出了一个co-divide方法。 • 利用label co-refinement 和co-guessing 改善之前的谷歌提出的MixMatch方法 1.co-divide方法 大概思路:同时训练两个网络,让每个网络在样本损失分布上去拟合GMM模型,并利用GMM模型,将训练数据分为标签数据和未标签数据,co-divide 使前面分的标签与未标签数据分别在

so 5042

Why String is immutable in Java?

1、String pool2、缓存hashcode 3、对其他对象的影响4、安全5、immutable在多线程中天然安全

深藏功与名 240

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Java - Why String is Immutable or Final in Java

http://javarevisited.blogspot.de/2010/10/why-string-is-immutable-in-java.html Sometimes I can't open this page without proper proxy, so I have to copy the content here. 1)String is Immutable i

u013248470的专栏 574

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【翻译】Why String is immutable in Java

原文地址: 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 imm...

iteye_19040的博客 214

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Why String is Immutable or Final in Java

博客已经迁移到: Why String is Immutable or Final in Java

weixin_34209851的博客 106

Why string is immutable?

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 Strin...

Just a programer ! 205

why string is immutable and why all wrapper class are immutable?

By default, all Java classes are mutable i.e contents of their instances can be modified. But there are few advantages that immutability offers (http://download.oracle.com/javase/tutorial/essential/

caoguanling2011的专栏 543

Java中String为什么是final或immutable

这是一个在Java面试中经常会被wenji

jinjiating的专栏 2006

为什么Java中的String是不可变的?(Why String is immutable o...

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34278190的博客 185

JAVA —— String is immutable. What exactly is the meaning? [duplicate]

  question:  I wrote the following code on immutable Strings. public class ImmutableStrings { public static void main(String[] args) { testmethod(); } private static void testmet...

weixin_34297300的博客 158

C# String 与 string的问题

今天突然用到C#的string,突然想起来一直没弄明白string和String有什么区别,查了一下电子书、MSDN以及百度和Google,更进一步看看String的一些问题。 0.  问题: 1. C#到底是什么时候传引用?什么时候传值?2. String传值还是传引用3. string和String有什么区别?4. String为什么是Immutable,怎么实现的?以下查询结果以及我的理解:

kiss0kill的专栏 1992
上一篇: Can you override Static Methods in Java?
下一篇: OOP Essentials - Thinking in Java
HelloWesh
博客等级 码龄14年 1粉丝 6原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值