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

JAVA Online Training
What is Java? Java is a technology developed by James Gosling at Sun Microsystems, Sun Microsystems is now a part of Oracle Corporation. This is one of the most powerful & securable language used to...

Overloading in JAVA
When a class contains more than one method with the same method name but different argument types, then it is called Overloading. Methods are said to be Overloaded methods. Also, know as Compile time...

Why Should We Learn Java Language?
Most of starters has confusion today that which of the programming language should be chosen to work on. It totally depends upon your need. As per my opinion, if you want to grow your career in IT industry...

Internet of Things, Social Media Becoming Part of E-Discovery Landscape
The days when e-discovery consisted of handing over copies of e-mails to address Freedom of Information Act (FOIA) requests, compliance regulations or other legal obligations are over. Now, it's just as...

Java Program Sample Application
/* WAP to print given message on the console using java*/ // Class declaration in java class sample { // Main function public static void main(String args) { // function for printing on the console System.out.println("Hello...
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