Why string class is immutable in java

Give me correct answer with real time example it's very Argent to me


33 Answers
21-33 of  33
33 Answers
  • String  class is immutable in java because ,in java if there are multiple variable refers to the single object then if we change one variables value then it will affect on other variables also ,that is why String is immutable so that we can not change string of an object if we will try to change value it will not affect on exiestance object but it can create new object and then assign new value to variable in new object ...
     

  • When we create a string in java like String s1="hello"; then an object will be created in string pool(hello) and s1 will be pointing to hello.Now if again we do String s2="hello"; then another object will not be created but s2 will point to hello because JVM will first check if the same object is present in string pool or not.If not present then only a new one is created else not. Now if suppose java allows string mutable then if we change s1 to hello world then s2 value will also be hello world so java String is immutable.

  • Immutable simply means unmodifiable or unchangeable. Once string object is created its data or state can't be changed but a new string object is created. class Test{ public static void main(String args[]){ String s="Suhas"; s.concat(" Killedar");//concat() method appends the string at the end System.out.println(s);//will print Suhas because strings are immutable objects } } java uses the concept of string literal.Suppose there are 5 reference variables,all referes to one object "Suhas".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

  • String is immutable because the it increases the security from hacking like not allowing to change the sensitive data like changing username, password. It is also useful to perform multi threading and we don't need to synchronise. It is also used for string pooling.

  • Whenever we create a String Object, Intially two Objects are created : one in the Heap And another in the String Pool. Now you can able to conclude your answers... ;) I've given you a Strong Hint...

  • String creation in Java uses String pool(part of Heap memory in Java)

    string a = "Hello"; // "Hello" is created in string pool.
    string b = "Hello"; // Now this refers a's value instead of creating new one in string pool.

    means when same string is used, they shares the single memory.
    when a is reassinged with another string.
    eg : a = "Bye";
    now b is still pointing towards the same pointer address of "Hello"

    a is just now pointing towards new address in string pool called "Bye".

    Conclusion : A single string can be assigned with any number of string variables.
    if the old string is destroyed when variable is reassigned with new string. all other string variables holding the same memory address looses their value.

    I believe this help.
    sorry If I'm wrong.

  • Were String not immutable, a connection or file would be changed and lead to serious security threat. Mutable strings could cause security problem in Reflection too, as the parameters are strings. Efficiency The hashcode of string is frequently used in Java. For example, in a HashMap.

  • the string is immutable for several reasons
    1. Security
    2. Synchronization 
    3. Caching
    4. Class loading

  • because we cant modified after execution the string object

  • String object is nonchangable in java onces it's created we can't change any modifications in that

  • String class is FINAL it mean you can't create any class to inherit it and change the basic structure and make the Sting mutable.
    Another thing instance variable and methods of String class that are provided are such that you can't change String object once created.
    -1

21-33 of  33
Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here