Why is String immutable in Java

0 votes
When interviewed, I was questioned as to why String is immutable.

I responded as follows:

A string pool object will be formed in Java when a string like String s1="hello" is produced, and s1 will point to hello.

Now, if we execute String s2="hello" again, no new object will be created; instead, s2 will point to hello since JVM will first determine whether or not the identical object is already there in the string pool.

Only a new one is created if none already exists.

Now, if Java were to permit string mutability, changing the value of s1 to "hello world" would likewise alter the value of s2 to "hello world," proving that java strings are immutable.

Would someone kindly let me know if my response is correct?
Sep 22, 2022 in Java-Script by Tejashwini
• 3,820 points
297 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java-Script

0 votes
0 answers

What is difference between mutable and immutable String in java?

As far as I know, An immutable string ...READ MORE

Sep 22, 2022 in Java-Script by Tejashwini
• 3,820 points
302 views
0 votes
0 answers

Why am I getting a NoClassDefFoundError in Java?

When I execute my Java application, I ...READ MORE

Sep 22, 2022 in Java-Script by Tejashwini
• 3,820 points
296 views
0 votes
0 answers

Sort a single String in Java

Is there a primary way to sort a ...READ MORE

Sep 23, 2022 in Java-Script by Abhinaya
• 1,160 points
346 views
0 votes
1 answer

What is callback function in JavaScript?

callback is not a keyword, its just ...READ MORE

answered Jan 30, 2020 in Java-Script by Niroj
• 82,880 points
17,726 views
0 votes
1 answer

How do I turn a string to a json in Node.js?

Hello Kartik, Use the JSON function  JSON.parse(theString) ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,880 points
779 views
0 votes
1 answer

Switch statement for string matching in JavaScript

Hello @kartik, Just use the location.host property switch (location.host) ...READ MORE

answered Jun 2, 2020 in Java-Script by Niroj
• 82,880 points
616 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
644 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,910 points
4,786 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
465 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
460 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP