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 parameters in a method?

587


What environment variables do I need to set on my machine in order to be able to run java programs?

609


What are streams in java 8?

551


What is the use of singleton?

514


Why stringbuilder is not thread safe?

544






In the below example, what will be the output?

597


What is boolean example?

529


what is meant by HQL?

644


Can memory leak in java?

589


What is double word?

536


What are the drawbacks of reflection?

565


Do we have pointers in java?

535


What is the byte range?

583


Is java hard to learn?

465


What is blank final variable?

567