String is Immutable in Java

preview_player
Показать описание
#java #string
Рекомендации по теме
Комментарии
Автор

You can separate two identical strings by creating them via constructor:
String s1 = "bob";
String s2 = "bob";
String s3 = new String("bob");
s3 would exist in a different pool, no matter if it has the same value. In this case, the == operator would return false for s1 and s3, but true for s1 and s2 because they are compared by address. The .equals() method is used for situations where you need to pass the value.
StringBuilder is good for manipulations, but it's not thread safe as StringBuffer. If you're using a newer version of java, a string treated as StringBuilder will compile as a StringBuilder.

medoncho
Автор

My journey starts with this guy, thanks man

Sharukkhan-kxrw
Автор

Pretty sure stringbuffer is old and now we use StringBuilder :D

DiamondSaberYT
Автор

I think you can explain this concept better than what you have explained in this video.
This is way simpler than what you have explained.

mohammedraza
Автор

If you need to modify your string in runtime, use stringbuffer or stringbuilder. Stringbuffer is more adapted in multi-threading context because it is thread-safe.

Bestfriend-qs
Автор

Actually when we concatenate one string reference with another it creates new string object in the heap and not stores it in the string pool so if next time if you created new string s2="Navin Reddy" it will create a string in the string pool and if you matched by s1== s2 it will give false

abhishekkute
Автор

But if s is changed to "Navin Reddy", how is the variable 's' immutable?

BinuJasim
Автор

I don't know Java but, it looks like the example is reassigning s and not actually mutating it. But, I would imagine doing something like s[0] = 'x' would throw an error?

elw
Автор

I'm a Java Developer with 1 year experience. Never have I needed to know this in my daily job. When does this information become necessary?

peachezprogramming
Автор

One more reason java disobeys the memory limit when I tell it not to use 10GB of RAM.

complexity
Автор

How do we see that it is not changed?
If printing 's' returns full name, what do we print to see the original string?

Didn't fit the right meaning for Immutable concept of strings.

Here is my take,
s[n] where n= 1, 2, 3, 4, 5 etc trying to assign any new value to any position of array doesn't let you change the value.

RK_
Автор

String buffer is faster than the normal string.

adnanhussain
Автор

In first u said it will not change but it changed 😅 how

ItCoursesForAll
Автор

What if that heap memory ran out of slot? Can s variable store more value?

TuLe-nftq
Автор

If two strings that are the same connect to the same pool, why do we have to use .equals() even though == seems to work just fine?

damagee
Автор

Remember, string is a character array internally in most programming languages.

undergraduate
Автор

Coming from javascript I am confused, seems he reassigning s, instead of actually mutating. Ideally we supposed to initalize another variable whose value as s and try to change that variable and make sures s not changing.
Any one explain what he means immutability by that example??

shankar
Автор

in which language exactly strings are mutable?

gordonfreimann
Автор

Navin, i have a doubt, if the String s = "Navin"; s= s+"Reddy"; which creates Navin Reddy, so What will happen for String s= "Navin"; can you explain please

deivasigamanimurugan