Java String Handling Question and Answers

1. Why String is immutable or final in Java?
 
  • There are several benefits of String because it's immutable and final.
  • String Pool is possible because String is immutable in java.
  • It increases security because any hacker can't change its value and it's used for storing sensitive information such as database username, password etc.
  • Since String is immutable, it's safe to use in multi threading and we don't need any synchronization.
  • Strings are used in java classloader and immutability provides security that correct class is getting loaded by Classloader.
 
Your Name Your Email-ID
Your Answer
2. What is String in Java?
 
  • String is a Class in java and defined in java.lang package. It's not a primitive data type like int and long. String class represents characters Strings.
  • String is used in almost all the Java application and there are some interesting facts that we should know about String.
  • String is immutable and final in Java and JVM uses String Pool to store all the String Objects.
  • String is the way we can instantiate a String object using double quotes and overloading of "+" operator for concatenation.
 
Your Name Your Email-ID
Your Answer
3. Does String is thread safe in Java?
 
  • Strings are immutable, so we can't change its value in program.
  • Hence it's thread safe and can be safely used in multi-threaded environment.
 
Your Name Your Email-ID
Your Answer
4. What is String Pool?
 
  • String Pool is a pool of strings stored in Java heap memory.
  • We know that Strings is special class in java and we can create String object using new operator as well as providing values in double quotes.
 
Your Name Your Email-ID
Your Answer
12345 Page 1 of 5