#35 Mutable vs Immutable String in Java

  Рет қаралды 101,483

Telusko

Telusko

Жыл бұрын

Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Udemy Courses:
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
Java For Programmers:- bit.ly/javaProgrammers
For More Queries WhatsApp or Call on : +919008963671
website : 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 : github.com/navinreddy20/Javac...
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
JavaScript :- bit.ly/3tiAlHo
Node JS :- bit.ly/3GT4liq
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Blockchain Tutorial :- bit.ly/3NSbOkc
Corda Tutorial:- bit.ly/3thbUKa
Hyperledger Fabric :- bit.ly/38RZCRB
NoSQL Tutorial :- bit.ly/3aJpRuc
Mysql Tutorial :- bit.ly/3thpr4L
Data Structures using Java :- bit.ly/3MuJa7S
Git Tutorial :- bit.ly/3NXyCPu
Donation:
PayPal Id : navinreddy20
www.telusko.com

Пікірлер: 23
@dineshsm3823
@dineshsm3823 Жыл бұрын
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.
@King-bj8km
@King-bj8km Жыл бұрын
this comment need to be pinned :)
@abhijithtm1318
@abhijithtm1318 Жыл бұрын
pin this sir
@SudipNeupane42
@SudipNeupane42 10 ай бұрын
hands down you're the best java teacher on youtube.RESPECT
@heycreatives
@heycreatives Жыл бұрын
Good series for making total java tutorial. it's very easy revise total java concepts.. Awesome Explanation Navin sir...
@krishnapatel-ys7nk
@krishnapatel-ys7nk 6 ай бұрын
The way he is explaining everything in detail is ❤️...my mind is blessed with such an amazing amount of knowledge...thank you sir
@muralikrishna2607
@muralikrishna2607 Жыл бұрын
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.
@HypRGK
@HypRGK 9 ай бұрын
wow this was so easy to understand , thank you sir for such an easy explanation
@AnuragRawat01
@AnuragRawat01 9 ай бұрын
Fantastic sirji ❤️
@duastech1532
@duastech1532 Жыл бұрын
Underrated channel 😢
@vigneshk5193
@vigneshk5193 Жыл бұрын
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
@NM_Krishna
@NM_Krishna 9 ай бұрын
waaaoooooooo sir you are the best ever
@raman_reddy
@raman_reddy 11 ай бұрын
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
@kamranalam2002
@kamranalam2002 Жыл бұрын
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"?
@Regr310
@Regr310 Жыл бұрын
great question, where is the replies 😅
@CoderboyAkshay
@CoderboyAkshay 11 ай бұрын
@@Regr310 i think he didn't check comment section😄 just uplode videos
@anonymous-li9ts
@anonymous-li9ts 8 ай бұрын
Yes, It should
@nishchaygupta9988
@nishchaygupta9988 9 ай бұрын
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 ?
@gowrigandikota8521
@gowrigandikota8521 9 ай бұрын
Can mutable string be created with "new String()" ?
@yogithacirigiri4882
@yogithacirigiri4882 2 ай бұрын
how are you able to compile and run without typing
@harishraj-zm5hh
@harishraj-zm5hh 7 ай бұрын
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
@ritikyadav9378
@ritikyadav9378 5 ай бұрын
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.
@puruagni1927
@puruagni1927 2 ай бұрын
Why are you not updating VS Code? Please update VS Code.
#36 StringBuffer and StringBuilder in Java
5:41
Telusko
Рет қаралды 153 М.
#34 What is String in Java
7:24
Telusko
Рет қаралды 101 М.
Khóa ly biệt
01:00
Đào Nguyễn Ánh - Hữu Hưng
Рет қаралды 21 МЛН
ОДИН ДЕНЬ ИЗ ДЕТСТВА❤️ #shorts
00:59
BATEK_OFFICIAL
Рет қаралды 8 МЛН
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,5 МЛН
#26 Stack And Heap in Java
12:37
Telusko
Рет қаралды 202 М.
What is String Immutable || Why String Object Is Immutable In Java (Hindi)
17:36
Functional Interface | Lambda Expression in Java
13:56
Telusko
Рет қаралды 138 М.
#53 Packages in Java
12:20
Telusko
Рет қаралды 158 М.
Java Strings are Immutable - Here's What That Actually Means
7:06
Coding with John
Рет қаралды 605 М.
#32 Array of Objects in Java
8:52
Telusko
Рет қаралды 113 М.
.equals() vs. == in Java - The Real Difference
8:48
Coding with John
Рет қаралды 180 М.
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 428 М.
Main filter..
0:15
CikoYt
Рет қаралды 13 МЛН