Why String is Immutable | Java Tutorial

preview_player
Показать описание
Free Demo On Core Java Online Training By Mr. Hari Krishna.
Demo on : 5th April @ 4:00 PM (IST).

Why String is Immutable | Java Tutorial

💡 Visit Our Website for Classroom Training:
💡 For Online Training:

--------------------------

💡 About NareshIT:

"Naresh IT is having 14+ years of experience in software training industry and the best Software Training Institute for online training, classroom training, weekend training, corporate training of Hadoop, Salesforce, AWS, DevOps, Spark, Data Science, Python, Tableau, RPA , Java, C#.NET, ASP.NET, Oracle, Testing Tools, Silver light, Linq, SQL Server, Selenium, Android, iPhone, C Language, C++, PHP and Digital Marketing in USA, Hyderabad, Chennai and Vijayawada, Bangalore India which provides online training across all the locations

--------------------------

💡 Our Online Training Features:
🎈 Training with Real-Time Experts
🎈 Industry Specific Scenario’s
🎈 Flexible Timings
🎈 Soft Copy of Material
🎈 Share Videos of each and every session.

--------------------------

💡 Please write back to us at

--------------------------

💡 Check The Below Links

Рекомендации по теме
Комментарии
Автор

Sorry, but your theory and example does not make any sense. Below is the reason for String being immutable

Security: parameters are typically represented as String in network connections, database connection urls, usernames/passwords etc. If it were mutable, these parameters could be easily changed.
Synchronization and concurrency: making String immutable automatically makes them thread safe thereby solving the synchronization issues.
Caching: when compiler optimizes your String objects, it sees that if two objects have same value (a="test", and b="test") and thus you need only one string object (for both a and b, these two will point to the same object).
Class loading: String is used as arguments for class loading. If mutable, it could result in wrong class being loaded (because mutable objects change their state).

hireme
Автор

Excellent sir.Good to see you on youtube. please do Keep posting videos like this to enjoy your lecture. 4 years back when I was a fresher I have learnt Java from you and now I am working for MNC. Thanks a lot for your dedicated teaching and inspiration.

anudeepyendrapalli
Автор

Watching this, I realize I once failed an interview by suggesting that a mutable object could be used as a key in a map!! The question wasn't posed in such an obvious way, I never would have gotten that wrong. While discussing a solution they asked "So, what else could we have used as a key here?" and might have suggested the mutable object as a possibility. I said "Yeah, sure" because I was nervous and wanted to get whatever else in. They had a lot of candidates and someone got to say "Yay, we just narrowed down." I really didn't realize it until just now watching this discussion. Knowing something well enough to answer a question phrased directly is different from knowing it well enough to see it and deal with it in the wild. I see how preparing one for these kinds of things that affect both ones code and interviews is being considered by these teachers.

jvsnyc
Автор

First of all the way you used toUpperCase() is wrong. It will change the value and create another object in String constant pool, but it will not return the converted value to s1. The basic String Class operation.

anandashok
Автор

Hello sir Today in one interview i have said this answer for string immutable but then interviewer asked me how is it possible string introduced in java Early and hashmap introduced later. So is it possible

karunkausal
Автор

Hi Hari Krishna,

This is a one good reasonfor String Immutability. I found few more good reasons for string immutability in StackOverflow thread.


String is immutable for several reasons, here is a summary:

Security: parameters are typically represented as String in network connections, database connection urls, usernames/passwords etc. If it were mutable, these parameters could be easily changed.
Synchronization and concurrency: making String immutable automatically makes them thread safe thereby solving the synchronization issues.

Caching: when compiler optimizes your String objects, it sees that if two objects have same value (a="test", and b="test") and thus you need only one string object (for both a and b, these two will point to the same object).

Class loading: String is used as arguments for class loading. If mutable, it could result in wrong class being loaded (because mutable objects change their state).
That being said, immutability of String only means you cannot change it using its public API. You can in fact bypass the normal API using reflection. See the answer here.

hareesh
Автор

Really it's very good nd clearly explained, thank you sir.

singam
Автор

One small correction,


String s1 = "hk";
Hashtable ht = new Hashtable();
ht.put(s1, "1");
ht.put("Ravi", "2");
ht.put("Raju", "3");
s1.toUpperCase();
--> this should be s1 = s1.toUppercase(); to make s1 as HK


Your program has no effect on s1 as mentioned reason above.
Moreover you need to pass ht.get(s1) instead of ht.get("hk") to see if calling s1.uppercase() has really changed the value from hk to HK.


Just to make it clear, calling string methods on string value will not have affect untill you say s1 = s1.toUpperCase(); and observe that this is new object and that old object with s1 = hk is destroyed.

arunchalla
Автор

Thanks Sir Hari sir means Hari sir only whole team trying to convince that for pooling purpose string is immutable but Hari Krishna sir prove that why string is immutable

manashranjan
Автор

If you try to get Object obj= ht.get("HK"); means upper case then you will get null ..it means value 1 is not created for after making it s.toUpperCase();

amitclick
Автор

Hashmap uses generics HashMap<K, V>.So, keys can be of different datatype too.It is not a strong reason for String Immutability.

lawanutech
Автор

(wrong) ( not immutable 00:10:51 )
[{there actually I confused}]
in the previous diagram if the string is given as mutable
then only we get NULL
and only IM-MUTABLE WE GET 1 Value.

[{ Finally whole Explanation is GOOD }]..
:) i am here to help you by mentioning that and then concept will get helpful

adaylightsmile
Автор

string class introduced in Java 1.0 where as collection are introduced in 1.2 and what is the relation ship between string class and collection

manishanker
Автор

I think string is a primitive so it should get passed by value in java( and all non- primitive obj are passed as references) and thus the map key shouldn't change.... Would be glad if you clarify me on this sir..

himeshgupta
Автор

Super sir.no one can teach this point.this make your students unique from other.

prangshumaity
Автор

you are one of the best in Ameerpet Institutes, Nice explanation, not only explanation, also you are trying to motivate

girinani
Автор

Great explanation sir. Thank you sir for deep explanation

rajeshgupta
Автор

After watching this video ...My soul is saying only one word awesome Hari

noth
Автор

good explanation. thank u sir
please can provide the why eaquals() and hashcode() method and why override both methods

challarajesh
Автор

thanku so much sir....plz make these type of videos, ..all the best sir...

digitallearning-