Question
Why is String Immutable in Java?

Answers

1) String Pool

When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object. If string is not immutable, changing the string with one reference will lead to the wrong value for the other references.

2) To Cache its Hashcode

If string is not immutable, One can change its hashcode and hence not fit to be cached.

3) Security

String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment.   Your Comment




More Software,Java Questions..
What is the inputsplit in map reduce software?

Can variables be used in Java without initialization?

What is the difference between java bean and enterprise java bean?

What is software configuration management?

How does the java database connectivity (jdbc) work?

What are the advantages of threads in java?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

What is object cloning in Java?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?



Search
Can you Answer!!
  • Q Explain Python functions.
  • Q Explain the types of linked lists?
  • Q Difference between stored procedure and function?
  • Q Near Field Communication technology (NFC) is a set of standards for: Hot-air ballooning; Smartphones; Border control; or Satnav?
  • Q What is the use of ‘grep’ command?
  • Q Plants that grow in saline water are called
  • Q HR Security: What are the objects for HR and what is the importance of each HR object
  • Q The calculation of electronegativities was first done by--
  • Q Describe a big problem in your life and how you coped with it?
  • Q How can a '::' operator be used as unary operator?
  • Q Srikanth Kidambi is associated with?