What is immutable string in java

0 votes
What does it mean?
Jul 16, 2018 in Java by Akrati
• 3,190 points
245 views

2 answers to this question.

0 votes

In javastring objects are 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="John";  

 s.concat(" Fernandis");

 System.out.println(s);

 }  

}  

answered Jul 16, 2018 by Daisy
• 8,120 points
0 votes

To answer your question, the String is immutable for various ways such as for security, the parameters are represented in the network connections as String and is also represented in usernames/passwords and URLs with database connections. This could have been changed if they were mutable. In order to make the string immutable and in successfully doing so, they are made thread safe which eventually solves the synchronization problems. However, when
the string is used for class loading as arguments, if it is mutable, then this could result in a wrong class being loaded due to the change of state in the objects which are mutable. The immutability of a String states that one cannot alter the public API usage but it can bypass the  normal API by using reflection. Below is an example if the String was mutable:-  



String a="stack"; 
System.out.println(a);//prints stack 
a.setValue("overflow"); 
System.out.println(a);//if mutable it would print overflow
answered Feb 9 by Soham
• 8,740 points

Related Questions In Java

0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,620 points
719 views
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,170 points
808 views
+1 vote
4 answers

What is a simple way to repeat a string in java?

There is already answer wriiten using StringBuilder ...READ MORE

answered Dec 16, 2020 in Java by Rajiv
• 8,910 points
26,252 views
0 votes
1 answer

What is the difference between main(String[] args) and main(String... args) in Java?

String[] is used to accept a single parameter ...READ MORE

answered Sep 6, 2019 in Java by Piya
2,349 views
0 votes
1 answer

How can two strings be concatenated in java?

You can concatenate Strings using the + operator: System.out.println("Your number ...READ MORE

answered Jun 6, 2018 in Java by Daisy
• 8,120 points
220 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,900 points
2,962 views
0 votes
1 answer

Why is char[] preferred over a string?

Strings are immutable. That means once you've ...READ MORE

answered Jun 14, 2018 in Java by scarlett
• 1,290 points
239 views
0 votes
1 answer

Can quotes be added in a java string.how?

In Java, you can escape quotes with \: String ...READ MORE

answered Jun 14, 2018 in Java by Akrati
• 960 points
210 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
2,390 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,120 points
2,526 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP