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

Android : Support multiple themes in an android application
If you are developing a theme based application to enhance user experience, then following steps needs to follow.We are taking example of an android application having 2 themes white and black.Step 1:Define...

Basic Concepts of Web Designing
An introduction to domain names, web servers, and website hosting 1)What is the web? In a nutshell, the web is a whole bunch of interconnected computers talking to one another. The computers (on the...

Crux Of The Basic Interview Question Asked Around It Professionals
Question: Explain PSVM() in Java. Answer: This question is asked from a differnet persctive. Person asking this question is looking for deep java concepts. If answer in your mind is something like...

Thread Class In JAVA
Thread Class: Thread class is the main class on which Java's Multithreading system is based. Thread class, along with its companion interface Runnable will be used to create and run threads for utilizing...

Java : Compile-time Versus Runtime optimization
While designing and development, one should think in terms of compile-time and run-time.It helps in understanding language basics in a better way.Let's understand this with a question below : What...
S
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