Câu hỏi phỏng vấn Strings
Câu hỏi

What is strings mutability and immutability?

Câu trả lời

Strings in Java are immutable, which means that once a string object is created, its value cannot be changed. Here are the key points about string immutability in Java:

  • String objects are stored in the String pool in the heap memory. When a new string is created, the JVM checks if an identical string already exists in the pool. If so, it reuses the existing object instead of creating a new one[1][2].

  • Immutability ensures that strings can be safely shared between multiple threads without synchronization issues[1][2][4]. It also enables caching of strings for better performance[2].

  • Immutability provides security benefits. For example, it prevents malicious modification of strings used for database connections, class loading, or other sensitive operations[2][4].

  • When a string operation like concatenation is performed, a new string object is created with the modified value. The original string remains unchanged[1][3][4].

  • The Stri...

junior

junior

Gợi ý câu hỏi phỏng vấn

expert

Compare Strings vs Ropes from the Performance Analysis

expert

What's the rationale for null terminated strings?

middle

Mentions some pros and cons of immutable vs mutable Strings

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào