
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Why String class is immutable or final in Java?
The string is immutable means that we cannot change the object itself, but we can change the reference to the object. The string is made final to not allow others to extend it and destroy its immutability.
- Security Parameters are typically represented as String in network connections, database connection URLs, usernames/passwords, etc. If it was mutable, these parameters could be changed easily.
- Synchronization and Concurrency making String immutable automatically makes them thread safe thereby solving the synchronization issues.
- Caching when compiler optimizes our String objects, it seems that if two objects have the same value (a =" test", and b =" test") and thus we need only one string object (for both a and b, these two will point to the same object).
- Class loading String is used as arguments for class loading. If mutable, it could result in the wrong class being loaded (because mutable objects change their state).
Example:
public class StringImmutableDemo { public static void main(String[] args) { String st1 = "Tutorials"; String st2 = "Point"; System.out.println("The hascode of st1 = " + st1.hashCode()); System.out.println("The hascode of st2 = " + st2.hashCode()); st1 = st1 + st2; System.out.println("The Hashcode after st1 is changed : "+ st1.hashCode()); } }
Output:
The hascode of st1 = -594386763 The hascode of st2 = 77292912 The Hashcode after st1 is changed : 962735579
- Related Articles
- Why string is immutable in Java?
- Why Final Class used in Java?
- Why string objects are immutable in java?
- Immutable String in Java
- Final class in Java
- How to create immutable class in Java?
- How to create the immutable class in Java?
- How to create an immutable class in Java?
- Make a class final in Java
- Check whether String is an interface or class in Java
- Why String class is popular key in a HashMap in Java?
- Why can't a Java class be both abstract and final?
- Do all properties of an Immutable Object need to be final in Java?
- What is blank or uninitialized final variable in Java?
- What is the meaning of immutable in term of String in java?

Advertisements