[!INDEX]

  1. Immutability concept
  2. String immutability ( Cannot Change) + concat mt("python")
  3. image explain
  4. why string object are immutable-practical example + agra + goa
  5. String Immutable? security(url), SCP( one chane-all change)
  6. Difference between final( mt, field class) and immutability(string obj.)
  7. Table of difference

1. Immutability concept

[!NOTE]

  1. is used for "String Objects" i.e. String objects are immutable.
  2. Once Created, its data/state can't be changed but a new string object is created.
  3. 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]

  1. String s1 = new String("Deepak");
  2. 2 object, heap,scp, JMV refer - SCP's object
  3. s1.concat("java"); ->
  4. Will not change the value. Create new literals ->
  5. save = "Deepak java" in heap ->
  6. and java in SCP area, ->
  7. original value will not change.
  8. s1 = s1.concat("python"); ->
  9. create python in SCP, JVM's internal variable refers it ->
  10. create "Deepak python" in heap area ->
  11. and s1 now pointing to Deepak python , ->
  12. but original value ("Deepak") will not change.

4. why string object are immutable-practical example + agra + goa

[!NOTE]

  1. what is the use
  2. String city1 = "delhi";
  3. String city2 = "delhi";
  4. String city3 = "delhi";
  5. String city4 = "delhi";
  6. String city5 = "delhi"; 6.
  7. all points to delhi in SCP's area.
  8. 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.
  9. 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]


6. Difference between final( mt, field class) and immutability(string obj.)

[!NOTE]

  1. final is use for class and variable so that no body can extends and overwrite the method.
  2. 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