UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Interview Tip : Q1) Why Strings are immutable in java ? What happen if it was mutable in java?

Gaurav
17/02/2017 0 0

As we all know that Strings in java are immutabe in nature, now the question comes why the creator made it immutable in nature, although this field used maximum in any java program.

The answer to this question is very simple.

1) Firstly due to the String pool availability in java.String pool is a space in heap in java where all the string literals are stored, now suppose if there were not such pool then for each String creation we need an brand new Object although the string literal is same, so to avoid the creation of unneccesary objects and can utilize the existing object present in String pool, String made immutable in nature. Also due to this the performace can increase due to not creation of objects which is costly both in terms of space and time.

2) To avoid possible security attacks. Suppose if String won't immutable then in the middle of the some requests, some hacker can change the String and then you know ..... . The same scenario also comes in the picture of during class initialization, now suppose string are not immutable then one can simply inject the malacious code by just replacing the value of the String and jvm will start running the malacous code, so to avoid such cases String are made immutable in nature.

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

How 4 byte float can store 8 byte long values in java ?
long l = 12464545L; float f = l; (fine) int i = l ; (Error) if we see the how this magic happens as we know the answer lies in the floating point representation as in floating points values does not...

Differences Between HashMap vs HashSet In Java.
HashSet HashMap HashSet implements Set interface. HashMap implements Map interface. HashSet stores the data as objects. HashMap stores the data as key-value pairs. HashSet...

ClassNotFoundException vs NoClassDefFoundError
ClassNotFoundException NoClassDefFoundError It is an exception and happens due to programmer’s mistake and can be recovered by updating the code. Thrown when an application tries...

Access Specifiers รข?? Private, Protected, Public, Package-private
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. The following table shows the access to members permitted by each modifier. Access...

JAVA - Object Cloning
JAVA - Object Cloning Is the way of creating the same copy of object without calling the class constructor. It means we can make any class object multiple times without calling its default constructor....
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more