While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
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 String pool that makes it a special class.
Let’s look at some of the benefits of String immutability, that will help in understanding why String is immutable in Java.
java.sql.Connection class but the referenced value is changed to
myhacked.Connection class that can do unwanted things to your database.
Above are some of the reasons I could think of that shows benefits of String immutability. It’s a great feature of the Java String class and makes it special. Read this post to know how to write your own immutable class.
You can checkout more Java String examples from our GitHub Repository.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.