---
title: "Discussion on \"Is a String in Java really immutable?\" | Hashnode"
description: "Discussion on \"Is a String in Java really immutable?\". We all know of the famous java.lang.String class in Java. The very next thing that comes to our mind when we see this class is, \"oh, yeah. Strings are immutable.\"\nBut...are they, really? Let's take a closer look.\nConsider the following snippet of cod..."
url: "https://hashnode.com/posts/is-a-string-in-java-really-immutable/6292125ed97f80b5091d9277"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Is a String in Java really immutable?](https://bhattacharya.hashnode.dev/is-a-string-in-java-really-immutable?utm_source=hashnode&utm_medium=feed)

We all know of the famous java.lang.String class in Java. The very next thing that comes to our mind when we see this class is, "oh, yeah. Strings are immutable." But...are they, really? Let's take a closer look. Consider the following snippet of cod...

May 28, 2022 · 1 min read · bhattacharya.hashnode.dev  
Read the article

-   [#java](https://hashnode.com/tag/java)
-   [#string](https://hashnode.com/tag/string)

## Written by

[Sourya Bhattacharya](https://hashnode.com/@BlackCoffeeIsEpic)

@BlackCoffeeIsEpic

  
[View profile](https://hashnode.com/@BlackCoffeeIsEpic)

Full Stack Engineer

## Responses 2

Markdown supported · ⌘↵ to post

[Markos Korvesis](https://hashnode.com/@MarkosK)

[May 29, 2022](https://hashnode.com/posts/is-a-string-in-java-really-immutable/6292125ed97f80b5091d9277/comment/62932fef0989e6b52feb0be3)

Interesting find, however, I agree with [Shai Almog](https://hashnode.com/@talktotheduck).

Reflection API will grant you access to do various things, but that does not mean that the language's rules are debunked, nor does it make it ok to do said things.

[Shai Almog](https://hashnode.com/@talktotheduck)

[May 28, 2022](https://hashnode.com/posts/is-a-string-in-java-really-immutable/6292125ed97f80b5091d9277/comment/62923e31d97f80b5091d97e3)

Circumventing the Java language rules via reflection isn't "really" mutability. By that measure immutability is impossible in the JVM.

If your app was running under a security manager it would fail in runtime in any version of the JVM which is why String is immutable. This is actually enforced by the TCK since string mutability can be used for various attacks by untrusted code.

[Sourya Bhattacharya](https://hashnode.com/@BlackCoffeeIsEpic)

[May 30, 2022](https://hashnode.com/posts/is-a-string-in-java-really-immutable/6292125ed97f80b5091d9277/comment/62923e31d97f80b5091d97e3#reply-629431b1d97f80b5091dc1e1)

I see. Thanks for reviewing my post :)
