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 is :: operator in java 8?

796


What is udp in java?

747


What are latest features introduced with java 8?

818


What are the differences between checked exception and unchecked exception?

749


Is int primitive data type?

741


How to retrieve data from database in java using arraylist?

751


What does pointer mean?

755


What is the use of a copy constructor?

769


What the difference is between execute, execute Query, execute Update?

586


Explain the use of shift operator in java. Can you give some examples?

733


What is the difference between the paint() and repaint() methods in java programming?

891


How do you use wildcards?

753


Can extern variables be initialized?

685


what is recursion in java

811


How can we make a class virtual?

830