473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Why String is Immutable in Java?

2
Why String is Immutable in Java?
Sep 5 '20 #1
5 7721
dev7060
633 Expert 512MB
Something related https://bytes.com/topic/java/answers...ng#post3826024

Consider two string objects with the same content, JVM would allocate the same memory space for em in the pool and enable what is called "object sharing". If you want to see the proof that it's happening behind the scenes, verify the hashcode of both objects. Now try changing the content of one of the objects and print the hashcode. It'd be different. Why immutable? Since they both are using the same memory area; imagine one object manipulating the content. The changes would be reflected in the other one too, which is something you wouldn't want. The garbage collector isn't usually allowed in this area because of the same reason. It wouldn't want to free the memory space just when one object is done using it.
Sep 6 '20 #2
The string is Immutable in Java because String objects are cached in the String pool. ... Another reason why String class is immutable could die due to HashMap. Since Strings are very popular as the HashMap key, it's important for them to be immutable so that they can retrieve the value object which was stored in HashMap.
Sep 7 '20 #3
sarbjitgrewal
19 16bit
In Java, string is immutable since string objects are cached in the String pool.
Sep 23 '20 #4
Naheedmir
62 32bit
The String is Immutable when String literals are shared among multiple customers; there is a risk where one customer's action would influence all other customers. A string is widely used as a parameter for various java classes. If String is not immutable, a file would be changed and lead to a serious security threat.
Sep 25 '20 #5
Riya Bajpai
18 16bit
In Java, the string is immutable because of security, synchronization and concurrency, caching and class loading. The String objects are cached in the String Pool and it makes String immutable.
Mar 20 '23 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dan | last post by:
Hello. I have recently tried upgrading the MySql connector for my servlet from 2.0.4 to 3.0.9. I have found a minor limitation in 2.0.4 and was hoping that 3.0.9 would fix it. However, now I...
1
by: samikshya | last post by:
Hi Could anyone tell me how does a java code gets invoked when a xml string is passed to the system. is there any listener or not etc. Its urgent !!!!!!!!!!!! waiting 4 replies. :)
4
by: bawa | last post by:
pls give an idea about how to write string search and replace program in java without using any built in functions.. pls help me out
14
by: prasath03 | last post by:
Dear All, I have one doubt for how to insert the java.sql.Date into ms-sql server. I tried the below code but it couldn't be inserted. I did wrong in my code that is ps.setDate(1,full_date_time)...
4
by: sheelaprabu | last post by:
I Knew Java String is Immutable. Because we cannt change the value of String object when it was created. explain with one code . thank you
1
by: eldadno1 | last post by:
I'm actually a Java programmer, and I'm messing with JNI a bit, so I've managed to pass an int from java to c++ and to c# and back. no problems cause it gets serialized automatically. but when...
0
by: techbytes | last post by:
hi, I have written a xml string for my project.I have to convert the xml string to java objects. help me. XML string is as follows. <?xml version="1.0" encoding="UTF-8" ?> -...
6
by: Dina Khaled | last post by:
This code is supposed to take in string input from the user and concatenate it into one string and print it but it doesn't print out any output HELP please : import java.util.*; public class...
2
by: praver235 | last post by:
The string is Ian A. Wood is name of first person followed by a space and then name of second person P. M. Visscher. Similarly for the next string.I want to split the string person wise...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.