filmov
tv
Java String Literal vs String Object (Java Collections)
![preview_player](https://i.ytimg.com/vi/_X1bGmad9mc/maxresdefault.jpg)
Показать описание
******************** String literal vs new keyword:
String
String //will not create new instance.
Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool.
In the above example only one object will be created. Firstly JVM will not find any string object with the value "Java" in string constant pool, so it will create a new object. After that it will find the string with the value "Java" in the pool, it will not create new object but will return the reference to the same instance.
Why java uses concept of string literal? To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).
When you create String object using new() operator, it always create a new object in heap memory.
String A=new String("Java")
String B=new String("Java")
#StringLiteralvsStringObject #Java #programmingline #rakesh #rake
*******************************************************
Important Links:
*******************************************************
Subscribe my YouTube channels:
*******************************************************
Комментарии