Why is string final in java?

String in java is Immutable(final). That is it’s value shouldn’t be change. people who have seen X-Men series would easily understand the word mutation.

Different people have answered this answer relating to memory complication,performance and all other things. So I will simply tell you what I know . I hope it help you.

Here String s=”javaguys”; and String s1=”javaguys”;

So, let ‘s’ created memory on string pool is s=101 and s1=103 , s and s1 point to same object.

Now if I change String s=”guys”;

s1 should also change as both pointing to same object . But that shouldn’t be happen. So, String is decided to Immutable.

Now, if we declare another string as:

String s2=new String(“javaguys”);

As it is called by new operator it will create a new memory in heap memory for s2 as let’s say s2=201. So one object in two different memory area 101 and 201. That is not possible. So memory at 201 also pointed to 101 memory area.(201 ->101)

But string immutable property can be modified by StringBuffer and StringBuilder.

Download Temple Run 2 new version.

Started by shimyleukemia at May 12, 2019 - 11:40 PM