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

how can you take care of mutual exclusion using java threads? : Java thread

605


How do you compare characters in java?

521


What are the ways to instantiate the class class?

595


What is json parser in java?

613


Can a class be final?

496






Can we sort array in java?

525


what is singleton in java?

598


What does file separator do in java?

534


If we don’t want some of the fields not to serialize how to do that?

563


How do you use compareto?

547


Why is the main method static?

595


What is the difference between call by reference and call by pointer?

497


Write a java program to generate fibonacci series ?

568


Can we override constructors?

541


What is prefix of a string?

571