String is mutable or immutable?

Answer Posted / deep

String is neither immutable nor mutable, String is a class Lol.. and string objects are immutable cause you can't change the data which is stored inside in a object..

Let's take a example..
String a = "Dark Knight"; or String a = new String("Dark Knight");

now let's perform operation on String object..
there are some method you can perform on string object so i am gonna perform replace operation..
a.replace("Dark","Blue");

now try to display what's inside String object which is 'a'..
System.out.println(a); // Ans. Dark Knight

System.out.println(a.replace("Dark,"Blue"); // Ans. Blue Knight

but value inside string object is still Dark Knight so to get over this you have to create another object and you have to perform same action again..

String a = "Dark Knight" or String a = new String("Dark Knight");
String b = a.replace("Dark","Knight");

System.out.println(a); // Ans. Dark Knight
System.out.println(b); // Ans. Blue Knight

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the main differences between notify and notifyAll in Java?

856


When do we use synchronized blocks and advantages of using synchronized blocks?

930


What is fail first in java?

836


Explain the usage of this with constructors?

774


What is string builder in java?

783


How many java versions are there?

769


Why is a singleton bad?

764


What is illegal identifier in java?

785


What is a java predicate?

750


What is run time allocation?

801


What restrictions are placed on method overriding in java programming?

781


How do you convert boolean to boolean?

811


What is use of inner class in java?

777


What is type conversion in java?

809


What is the purpose of abstract class?

740