[!INDEX]
- Immutability concept
- String immutability ( Cannot Change) + concat mt("python")
- image explain
- why string object are immutable-practical example + agra + goa
- String Immutable? security(url), SCP( one chane-all change)
- Difference between final( mt, field class) and immutability(string obj.)
- Table of difference
1. Immutability concept
[!NOTE]
- is used for "String Objects" i.e. String objects are immutable.
- Once Created, its data/state can't be changed but a new string object is created.
- immutability are always associated with strings objects
2. String immutability ( Cannot Change) + concat mt("python")
public class M03 {
public static void main(String[] args) {
String s1 = new String("Deepak");
System.out.println(s1); // Deepak
System.out.println(s1.concat("java")); // Deepak java,
//above create java in scp and make object deepak.java in hcp and start pointing it
s1=s1.concat("python"); //same s1 points to another object
System.out.println(s1); // Deepak python
}
}
3. image explain
[!NOTE]
- String s1 = new String("Deepak");
- 2 object, heap,scp, JMV refer - SCP's object
- s1.concat("java"); ->
- Will not change the value. Create new literals ->
- save = "Deepak java" in heap ->
- and java in SCP area, ->
- original value will not change.
- s1 = s1.concat("python"); ->
- create python in SCP, JVM's internal variable refers it ->
- create "Deepak python" in heap area ->
- and s1 now pointing to Deepak python , ->
- but original value ("Deepak") will not change.
4. why string object are immutable-practical example + agra + goa
[!NOTE]
- what is the use
- String city1 = "delhi";
- String city2 = "delhi";
- String city3 = "delhi";
- String city4 = "delhi";
- String city5 = "delhi"; 6.
- all points to delhi in SCP's area.
- let person 1 is move to goa , tab keywal ek ki city he change hogi baki logo ki city par koi farak naeh padna chaheye. isee karan se string immutable hote hai.
- Strings are Immutable in Java because String objects are cached in String pool. Since cached String literals are shared between multiple persons there is always a risk, where one persons's action would affect all another persons For example, if one person changes its city from "Mohali" to "Delhi" all then other persons will also get affected. */
5. String Immutable? security(url), SCP( one chane-all change)
[!NOTE]
- Security: Strings are used widely for parameters such as file paths, URLs, and network connections. Immutability ensures that these parameters cannot be changed once created.
- Caching and Performance: Java uses a string pool for memory efficiency. If
Stringwere mutable, a change in one instance could affect other instances in the pool, leading to unpredictable results.- Thread Safety: Immutability ensures that
Stringobjects can be safely shared across threads without needing synchronization.
6. Difference between final( mt, field class) and immutability(string obj.)
[!NOTE]
- final is use for class and variable so that no body can extends and overwrite the method.
- but immutability is use for object in strings
7. table of difference
| Aspect | final |
Immutability |
|---|---|---|
| Scope | Applied to variables, methods, and classes |
Applied to objects and their state |
| Meaning | Prevents reassignment (for variables), overriding (for methods), or subclassing (for classes) |
Ensures that an object's state cannot be changed after creation |
| Variable Behavior | A final variable's reference cannot be changed, but the object it refers to may still be mutable |
Immutability ensures that no changes can be made to the object’s internal state |
| Class Behavior | A final class cannot be extended |
An immutable class prevents any change to its state after creation |
| Typical Use | Used to prevent reassignment or inheritance |
Used to create thread-safe or constant objects |