Java interview questions 2 for freshers. Why String in Java is Immutable #shorts #coding #interview

preview_player
Показать описание
Java interview question 2
Why String in java is immutable
String variable can be changed to point at any object but the string object itself is immutable in java.
In this example the name = “salow” and another_name is also = “salow”
So string salow is created once in the string pool and both the variables point towards the string.
When java creates a string object it stores in a string pool, whenever a string is created java will check if the string is available in the string pool.
If it is not available in string pool it will create the literal in String Pool and point to it.
String pool is a place where all the literals of string are stored and this requires string to be immutable or else the shared reference can be changed from anywhere.
The next main reason is due to security in java makes string immutable hence no one can change reference of string once created.

Using Visual Studio Code editor

#shorts #JavaDevelopment #Coding #Java #tutorial #VSCode #freshers #2021
Рекомендации по теме
Комментарии
Автор

Why are strings immutable because string class is final. You cannot modify in that class

niteshpandey