#35 Mutable vs Immutable String in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing:
1) What happen when we create same character sequence ?
is it form multiple object for same character sequence?
a) using new keyword
b) String literal
2)What is String constant pool in heap?
3)what happen when we assign different object to reference variable of String class?
4)What is mutable and immutable String?

#1
Using the new keyword, you can create multiple objects from the same character sequence.
This is useful when you want to save time by creating multiple objects from the same data source. However, when using a string literal,
you can only create one object. String literals are read-only memory locations, so they cannot be changed and can only be used to create a
single object. This makes string literals less flexible than the new keyword.

String name1="navin";
String name2="navin";

Since "navin" is same character sequence and created by string literal way.
so name1 and name2 pointing same object created in string constant pool.

check--

using new --
String name3=new String("navin");
Since new can create new string object which can contain navin.

#2
what is string constant pool?
-- The String constant pool is an area in the Java heap that stores immutable strings.
It is used to optimize the storage of strings in the program by providing a single location
for string literals. When a string is created in the program, the Java Virtual Machine first
checks if the same string exists in the constant pool and if so, it is returned instead of creating
a new string. This helps to reduce the memory footprint and also improve performance since the string
comparison operation is also optimized.

#3
what happen when we assign different value to String reference variable?
suppose we create
String str="Mathura";
now str=str+" city";

-- do you think we change in existing "Mathura" object
then answer is no because String is immutable type which cannot further changed.
-- here reference variable str pointing to new object that is "Mathura city";
-- and "Mathura" object ready for garbage collection because no variable pointing the "Mathura" object.

#4
What is difference between mutable and immutable String?
--Mutable and immutable strings are two different types of strings in programming.
Mutable strings are strings which can be modified once they are created.
-- Immutable strings, on the other hand, cannot be changed after they have been created.

String is immutable
StringBuffer and StringBuilder are mutable in nature

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

Such a great lecture. One small mislead observed in the last min of the video is that you referred string buffer and string builder to immutable string. Actually its for mutable string.

dineshsm
Автор

hands down you're the best java teacher on youtube.RESPECT

SudipNeupane
Автор

Great video sir...At the end by mistake you said that the string buffer and string builder classes can be used for creating Immutable string.

muralikrishna
Автор

The way he is explaining everything in detail is ❤️...my mind is blessed with such an amazing amount of knowledge...thank you sir

krishnapatel-ysnk
Автор

Good series for making total java tutorial. it's very easy revise total java concepts.. Awesome Explanation Navin sir...

heycreatives
Автор

5:14 All the objects in string pool area are implicit objects, which are maintained by JVM. So those are not eligible for garbage collection

raman_reddy
Автор

Hello.
You literally cleared my confusion regarding the concepts of immutable and mutable strings.
But after seeing the full video my only question is what will be case for "hello" that you printed with the name variable? Will the heap memory also create a new object for "hello"?

kamranalam
Автор

wow this was so easy to understand, thank you sir for such an easy explanation

HypRGK
Автор

Good teaching Sir. I'm very thankful for java course. It is very useful to me. And i get more interested to listen your class. And please continue teaching like this. 😊
My humble request to you that to teach Data science, Artificial intelligence courses

vigneshk
Автор

3:34 - Can we say that String Constant Pool is nothing but an implementation of Set which is of String type in the Heap memory ?

nishchaygupta
Автор

String s1="Navin";
String s2="Navin";
s.o.p(s1==s2);
this above example gives true as its output as u explained.

but y we r getting false output with case of using new keyword for creating a string
String s1=new String("Navin");
String s2=new String("Navin");
s.o.p(s1==s2);
now the out put is false.

pls sir explain this

harishraj-zmhh
Автор

how are you able to compile and run without typing

yogithacirigiri
Автор

Can mutable string be created with "new String()" ?

gowrigandikota
Автор

Why are you not updating VS Code? Please update VS Code.

puruagni