Which of the following is true about Java’s ‘String’ class?

Java MCQ: Which of the following is true about Java’s ‘String’ class?

a) Strings are immutable
b) Strings can be changed after they are created
c) Strings are a primitive data type
d) Strings are thread-unsafe by default

Answer:

a) Strings are immutable

Explanation:

In Java, the String class represents a sequence of characters. One of the key features of strings is that they are immutable, meaning that once a string object is created, it cannot be changed. Any modification to a string results in the creation of a new string object.

This immutability provides several benefits, including thread safety and security, as the string data cannot be altered once created. Although strings are not a primitive data type (they are objects), they are widely used in Java and have special support in the language, such as the ability to use string literals.

Understanding the immutability of strings is crucial for efficient memory management and avoiding unintended side effects in your Java programs.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top