How to prove that a String object in Java is immutable programmatically?
Shathana. S.R. Answered question May 29, 2023
Since strings with the same content share storage in a single pool to reduce the need to duplicate the same value, Java specifies strings as being immutable, as can be seen above.
You Can Write This code Which proves that strings are immutable in Java
- public class ProveStringImmutable {
- public static void reference Check(Object x, Object y) {
- if (x == y) {
- System. out.println(“Both pointing to the same reference”);
- } else {
- System. out.println(“Both are pointing to different reference”);
- }
- }
Shathana. S.R. Answered question May 29, 2023
Since strings with the same content share storage in a single pool to reduce the need to duplicate the same value, Java specifies strings as being immutable, as can be seen above.
You Can Write This code Which proves that strings are immutable in Java
- public class ProveStringImmutable {
- public static void reference Check(Object x, Object y) {
- if (x == y) {
- System. out.println(“Both pointing to the same reference”);
- } else {
- System. out.println(“Both are pointing to different reference”);
- }
- }
Vishalini.R Answered question May 25, 2023
