#35 Mutable vs Immutable String in Java

Описание к видео #35 Mutable vs Immutable String in Java

Check out our courses:

Enterprise Java Spring Microservices: https://go.telusko.com/enterpriseJava
Coupon: TELUSKO10 (10% Discount)

Master Java Spring Development : https://bit.ly/java-spring-cloud
Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

Java:- https://bit.ly/JavaUdemyTelusko
Spring:- https://bit.ly/SpringUdemyTelusko
Java For Programmers:- https://bit.ly/javaProgrammers

For More Queries WhatsApp or Call on : +919008963671

website : https://courses.telusko.com/

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--
System.out.println(name1==name2); //true

using new --
String name3=new String("navin");
System.out.println(str1==str3); //false
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";
System.out.println(str);//here we get Mathura 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

Github repo : https://github.com/navinreddy20/Javac...

Java:- https://bit.ly/JavaUdemyTelusko
Spring:- https://bit.ly/SpringUdemyTelusko

More Learning :

Java :- https://bit.ly/3x6rr0N
Python :- https://bit.ly/3GRc7JX
Django :- https://bit.ly/3MmoJK6

JavaScript :- https://bit.ly/3tiAlHo
Node JS :- https://bit.ly/3GT4liq

Rest Api :-https://bit.ly/3MjhZwt
Servlet :- https://bit.ly/3Q7eA7k
Spring Framework :- https://bit.ly/3xi7buh
Design Patterns in Java :- https://bit.ly/3MocXiq
Docker :- https://bit.ly/3xjWzLA

Blockchain Tutorial :- https://bit.ly/3NSbOkc
Corda Tutorial:- https://bit.ly/3thbUKa
Hyperledger Fabric :- https://bit.ly/38RZCRB

NoSQL Tutorial :- https://bit.ly/3aJpRuc
Mysql Tutorial :- https://bit.ly/3thpr4L

Data Structures using Java :- https://bit.ly/3MuJa7S
Git Tutorial :- https://bit.ly/3NXyCPu

Donation:
PayPal Id : navinreddy20
https://www.telusko.com

Комментарии

Информация по комментариям в разработке