Java String Literal vs String Object (Java Collections)

Опубликовано: 28 Сентябрь 2024
на канале: Programming Line
259
4

► Do you have any Query? Kindly message us on WhatsApp https://wa.me/message/VLQEBSCSNL73P1 . We won't pick calls, just WhatsApp Messages.


► Download Our Programming Line Mobile App To Get Complete Courses For Low Fee: https://bit.ly/3xuPnK1

******************** 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")

For more details check out: https://programmingline.com/2019/03/j...

#StringLiteralvsStringObject #Java #programmingline #rakesh #rake

*******************************************************

Java Course:    • Java tutorials in English | Java cour...  
Software Design Patterns Course:    • Software Interview tips in English  
OOPs Course:    • OOPs concepts in English | Object Ori...  
NLP Course:    • Natural Language Processing 101 in En...  
Software Interview Tips:    • Software Interview tips in English  

Important Links:
Skillshare - Best Online Learning Platform (2 Free Months of Premium Membership): https://programmingline.com/skillshare
Java Course: https://programmingline.com/java-course
Software Design Patterns Course: https://programmingline.com/software-...
Object Oriented Programming (OOPs) Course: https://programmingline.com/oops
Best Books for Software Courses: https://programmingline.com/best-book...
Campus Interview: https://programmingline.com/campus-in...
How to crack the programming aptitude round?: https://programmingline.com/how-to-cr...


*******************************************************

Subscribe my YouTube channels:
► Subscribe https://www.youtube.com/programmingli... channel for watching Software Related courses like Java, Software Design Patterns, OOPs, and etc in Tamil, Telugu, and English.

► Subscribe    / @roadtoinvestment7954   channel for watching Income Tax saving schemes and Stock Market related videos.

► Subscribe    / @rakesh-ms7tn   channel for watching Terrace Gardening Tips, Unboxing products, and Random videos in Tamil, Telugu, and English.

Website: https://www.programmingline.com
FaceBook page:   / programminglineofficial  
Instagram:   / programmingline  
Twitter:   / programmingline  

*******************************************************