Please Comment, Subscribe and Click Bell🔔🔔🔔 Icon for More Updates. To learn software course from our experts please register here for online training: goo.gl/HIB0wL
(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
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.
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).
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.
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.
HK sir didn't mentioned that "HK" will store in s1, they just making us clear that s1.toUppercase(); will not change the already existing value "hk" in String constant pool. If you want to create an object in string constant pool then need to create an string again like String s2 = s1.toUpperCase(); And then you can store in Hashtable as ht.put (s2, 4); If you perfect at Java then you will get it now atleast. Otherwise you can only do it from your side and execute it. String s2 = s1.toUpperCase(); this one HK sir didn't written so you confused I think, indirectly Sir telling that it should be stored in one more string and we can put in map.
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
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();
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 System.out.println(ht.get("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.
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..
In Java 1.0 we have a class called Hashtable collection, it will store objects in Key,Value pair format. So, for this class String and wrapper classes are created as immutable objects. In Java 1.2 more classes are added as alternative to Vector and Hashtable classes. I hope you got my point :-)
Reason is same Generally strings and numbers are used as keys hash table for maintaining values. So String object and Wrapper classes(Byte, Short, Integer, Long, Float, Double, Character, Boolean) classes are created as immutable object classes.
@@JavaHariKrishna when we use string as key in hashtable , value for that key is actually stored against hashcode of key not against key, so i think your explaination is wrong
(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
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
Hi Hari Krishna, This is a one good reasonfor String Immutability. I found few more good reasons for string immutability in StackOverflow thread. stackoverflow.com/questions/22397861/why-is-string-immutable-in-java 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.
Dear Sir, Thanks for the Video, I want to know that if we will use s1.toUpperCase(), it will change to HK but this object will be lost because s1.toUpperCase() is not referenced by same reference s1 or some other new reference.
Hi Amit, That's not the case since the HashTable will still hold the reference to the lower case "hk" when we call hashTable.put("hk", 1), so it won't be garbage collected. Best Wishes.
I will release Collection videos very soon. Actually these string concept videos are the prerequisites to Collection concept. In collection programming I will use all these String concepts, so first I recoded these concepts videos.
thanks Sir, I face this question in interview, now i can give the answer, I forget this answer because of long time. Ultimate video. eagerly waiting for your next videos.
Thanks for explanations - why string is immutable -by HasMap.Please see belwo code import java.util.HashMap; public class MutableDemo { public static void main(String[] args) { HashMap m = new HashMap(); StringBuffer sb1 = new StringBuffer("jj"); StringBuffer sb2 = new StringBuffer("kk"); m.put(sb1, 1); m.put(sb2, 5); System.out.println(m); sb1.append('u');//performing operation on sb1 System.out.println(sb1);//it will print jju System.out.println(m.get(sb1));// it is giving output as=1 which initially inserted in map } } out put = {jj=1, kk=5} jju 1 Note = i am able to print value of key sb1 after performing operation on it. StringBuffer is mutable.
wrapper classes represent primitive types, and primitive types are mutable. No they're not (and String isn't a primitive). But since primitive types aren't objects anyway, they can't really be called mutable / immutable in the first place.
We can create our custom class and use it as a key by overriding hashCode() and equals() method. What you said might be part of the reason but I don't believe it's true. Do you have any reference to SUN Micro systems claiming what you're saying? Did you even google the answer to this question ?
Dear Piyush Chaudhary, you are talking about custom object, I am talking about String object. Did you find is there any wrong in my explanation or did you find any explanation from sun docs about why strong is immutable, pls share us we are ready to learn😊
@Hari Krishna By that logic "Integer" class is also mutable because of this reason. But wait, there is no Integer pool like String. I think your explanation is hilarious. Try putting your logic for this answer on StackOverflow and see how many up-votes you get.
Hi Piyush Chaudhary, I did not say Sting is immutable because for supporting String pooling. I gave different explanation. And thanks for referring stackoverflow site, pls check other comments one of the viewer also given comment on this video by supporting my explanation is correct with proof from stackoverflow site in this video I want to address pooling is not the main reason for making objects immutable, if so other objects wrapper classes and File class, no need to be created as immutable.
Perhaps you did not understand what I said. I know you said String pooling is there because String is immutable. So by that logic they should have also implemented Integer pooling because Integer class is also immutable. Please read that guy's comment again. That guy never gave any proof. He just gave a URL which contains good reasons for string being immutable.
@@piyush12121 I don't think it is just a coincidence, but it is funny. The reason that Integer io = new Integer( intValue ); is deprecated in modern versions of Java, in favor of Integer io = Integer.valueOf( intValue ); is precisely because they now implement.....INTEGER POOLING!
A few people seem to be confused by the fact that he is showing "Here's what would happen if String wasn't immutable and .concat() or .toUpperCase() worked in place, instead of the world we do live in where String IS immutable and those methods make a new string somewhere else and give you back a reference to it (unless it already existed in String Pool of course)." On television and in movies, when they show something different from reality they make the screen all blurry or wavy to show it is just imagination, then clear it away when they come back to reality. Java Strings are immutable, functions that work on them make and return new objects. All kinds of weird and bad things that never happen to us in Java would happen otherwise. You should be very careful when using Maps to use keys that make sense. This little video is pretty important, and the fact that people came up with a few additional reasons is good but doesn't detract from this presentation.
*your theory is wrong sir,* get () method can pass only key type parameter. Even if string is immutable, get() method will return always null value until it passes a key-integer type value. get(2) = this will print "Ravi" get("hk") = will always print null
what is key type parameter? but key doesn't need to be integer only, it can be of any type including string, in fact you could even have a user defined object as a key.
Arun Singh how can get method pass, you will jave to pass na. and ill have to only pass a string to it if at declaration of collection is made in such a way hashmap a = new hashmap(); a.put("aap",1); a.put("bjp",2); a.put("cong",3); sysout(a.get("aap")); //should print 1
your logic right but its different in MAP INTERFACE.. instead of commenting straight away you should check it once.. in map if you are using get () method you need to give KEY which will give the value of the key.. Mr Hari krishna is very experienced java trainer in naresh it
Yes Suresh, I agreed with you, but in all cases you addressing are internally uses (key,value) only. So for easy understanding I just took Hashtable as an example.
Krishna Gangaraju can you pls share your thought on this topic, so that all we will learn from your ideas too. Just leaving one comment may not justify, try to correct my mistake if any and Help others to learn 😊
Please Comment, Subscribe and Click Bell🔔🔔🔔 Icon for More Updates. To learn software course from our experts please register here for online training: goo.gl/HIB0wL
(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
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.
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).
I totally agree with you
Super explanation
Awesome buddy..! (y)
i have lots of doubt in String chapter can anyone help me go get best way to understand this chapter, i need in very urgently.
To the point....
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.
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.
HK sir didn't mentioned that "HK" will store in s1, they just making us clear that s1.toUppercase(); will not change the already existing value "hk" in String constant pool.
If you want to create an object in string constant pool then need to create an string again like String s2 = s1.toUpperCase();
And then you can store in Hashtable as ht.put (s2, 4);
If you perfect at Java then you will get it now atleast. Otherwise you can only do it from your side and execute it.
String s2 = s1.toUpperCase(); this one HK sir didn't written so you confused I think, indirectly Sir telling that it should be stored in one more string and we can put in map.
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
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();
Really it's very good nd clearly explained, thank you sir.
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
System.out.println(ht.get("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.
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..
Hashmap uses generics HashMap.So,keys can be of different datatype too.It is not a strong reason for String Immutability.
Other object equivalents of primitive datatypes like Integer, Long, Boolean, etc are also immutable.
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
In Java 1.0 we have a class called Hashtable collection, it will store objects in Key,Value pair format. So, for this class String and wrapper classes are created as immutable objects.
In Java 1.2 more classes are added as alternative to Vector and Hashtable classes.
I hope you got my point :-)
thankyou sir for your video..but i have a question that..we use integer also as a key in hastable but it is immutable..why?
plz explan about this sir?
Reason is same
Generally strings and numbers are used as keys hash table for maintaining values. So String object and Wrapper classes(Byte, Short, Integer, Long, Float, Double, Character, Boolean) classes are created as immutable object classes.
@@JavaHariKrishna when we use string as key in hashtable , value for that key is actually stored against hashcode of key not against key, so i think your explaination is wrong
(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
After watching this video ...My soul is saying only one word awesome Hari sir............
your soul has malfunction
Great explaination
Super sir.no one can teach this point.this make your students unique from other.
Great explanation Sir!!
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
Hi Hari Krishna,
This is a one good reasonfor String Immutability. I found few more good reasons for string immutability in StackOverflow thread.
stackoverflow.com/questions/22397861/why-is-string-immutable-in-java
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.
Thank you for the valuable information Hareesh :-)
Dear Sir, Thanks for the Video, I want to know that if we will use s1.toUpperCase(), it will change to HK but this object will be lost because s1.toUpperCase() is not referenced by same reference s1 or some other new reference.
Hi Amit, That's not the case since the HashTable will still hold the reference to the lower case "hk" when we call hashTable.put("hk", 1), so it won't be garbage collected. Best Wishes.
Sir, since String is final and immutable, why we allowed to modified with its new object,even though we can't use previous data
good explain sir
i u dont mind why hashcode()method and equals() override andsuppose not overriding what is problam.please explain sir
Thanq sir..your explained very neatly..it is easy to understand non IT students...please add collection videos also..
I will release Collection videos very soon. Actually these string concept videos are the prerequisites to Collection concept.
In collection programming I will use all these String concepts, so first I recoded these concepts videos.
thanks Sir, I face this question in interview, now i can give the answer, I forget this answer because of long time. Ultimate video. eagerly waiting for your next videos.
i need to say your conscious is very neat
Great explanation sir. Thank you sir for deep explanation
Very nice explanation. You sound like that funny Indian Guru, Mr. Nithyananda.
HI Sir, Other than string we can also use all wrapper classes as key for storing in hashmap.
Yes
but we always do string x="raju"; than raju is a key but where is the value to stored in hash-table can you please explain me this
very clear explanation. thanku!
sir plz once talk about singlton and its creation, uses and importance...............thank you sir
Excellent
Very well explained.
superb explanation Sir..
proud to be your student sir
Thanks a lot ..one doubt. Can we modify string by +=operator ??
Mostly no
This is Gud way to learn Java but sir Can u plz upload MultiThreading Videos plzz Sir
Sir can you please make the videos on threading concept in java
Thanks for explanations - why string is immutable -by HasMap.Please see belwo code
import java.util.HashMap;
public class MutableDemo {
public static void main(String[] args) {
HashMap m = new HashMap();
StringBuffer sb1 = new StringBuffer("jj");
StringBuffer sb2 = new StringBuffer("kk");
m.put(sb1, 1);
m.put(sb2, 5);
System.out.println(m);
sb1.append('u');//performing operation on sb1
System.out.println(sb1);//it will print jju
System.out.println(m.get(sb1));// it is giving output as=1 which initially inserted in map
}
}
out put = {jj=1, kk=5}
jju
1
Note = i am able to print value of key sb1 after performing operation on it. StringBuffer is mutable.
haha. did you figure it out, or did you forget about this comment?
Sir,
Integer i = 20;
i = 30;
System.out.println(i);
Output is 30 so how can Integer is immutable ?
wrapper classes represent primitive types, and primitive types are mutable. No they're not (and String isn't a primitive). But since primitive types aren't objects anyway, they can't really be called mutable / immutable in the first place.
thanku so much sir....plz make these type of videos,..all the best sir...
good explanation. thank u sir
please can provide the why eaquals() and hashcode() method and why override both methods
you are one of the best in Ameerpet Institutes, Nice explanation , not only explanation, also you are trying to motivate
We can create our custom class and use it as a key by overriding hashCode() and equals() method. What you said might be part of the reason but I don't believe it's true. Do you have any reference to SUN Micro systems claiming what you're saying?
Did you even google the answer to this question ?
Dear Piyush Chaudhary,
you are talking about custom object, I am talking about String object. Did you find is there any wrong in my explanation or did you find any explanation from sun docs about why strong is immutable, pls share us we are ready to learn😊
@Hari Krishna By that logic "Integer" class is also mutable because of this reason. But wait, there is no Integer pool like String. I think your explanation is hilarious. Try putting your logic for this answer on StackOverflow and see how many up-votes you get.
Hi Piyush Chaudhary,
I did not say Sting is immutable because for supporting String pooling. I gave different explanation. And thanks for referring stackoverflow site, pls check other comments one of the viewer also given comment on this video by supporting my explanation is correct with proof from stackoverflow site
in this video I want to address pooling is not the main reason for making objects immutable, if so other objects wrapper classes and File class, no need to be created as immutable.
Perhaps you did not understand what I said. I know you said String pooling is there because String is immutable. So by that logic they should have also implemented Integer pooling because Integer class is also immutable.
Please read that guy's comment again. That guy never gave any proof. He just gave a URL which contains good reasons for string being immutable.
@@piyush12121 I don't think it is just a coincidence, but it is funny. The reason that Integer io = new Integer( intValue ); is deprecated in modern versions of Java, in favor of Integer io = Integer.valueOf( intValue ); is precisely because they now implement.....INTEGER POOLING!
this is absurd , cant we directly pass s1 while getting the value , that way even if it has changed , will give the correct value ,
what if we write s1=s1.toUpperCase() ??
He is best
Good Explanation sir..
Superb explanations sir
thanks sir plz upload collection class video
great explanation!!
How can we use object as key in Map?
thank you so much.. you are awesome sir..
Perfect explanation
But we can pass s1 in the get() method
A few people seem to be confused by the fact that he is showing "Here's what would happen if String wasn't immutable and .concat() or .toUpperCase() worked in place, instead of the world we do live in where String IS immutable and those methods make a new string somewhere else and give you back a reference to it (unless it already existed in String Pool of course)." On television and in movies, when they show something different from reality they make the screen all blurry or wavy to show it is just imagination, then clear it away when they come back to reality. Java Strings are immutable, functions that work on them make and return new objects. All kinds of weird and bad things that never happen to us in Java would happen otherwise. You should be very careful when using Maps to use keys that make sense. This little video is pretty important, and the fact that people came up with a few additional reasons is good but doesn't detract from this presentation.
Thank you sir provide this video
*your theory is wrong sir,*
get () method can pass only key type parameter. Even if string is immutable, get() method will return always null value until it passes a key-integer type value.
get(2) = this will print "Ravi"
get("hk") = will always print null
what is key type parameter?
but key doesn't need to be integer only, it can be of any type including string, in fact you could even have a user defined object as a key.
i didnt say that key can only be integer type. I said
"get() method will return always null value unless it passes a key-integer type value."
Arun Singh how can get method pass, you will jave to pass na.
and ill have to only pass a string to it if at declaration of collection is made in such a way
hashmap a = new hashmap();
a.put("aap",1);
a.put("bjp",2);
a.put("cong",3);
sysout(a.get("aap")); //should print 1
your logic right but its different in MAP INTERFACE.. instead of commenting straight away you should check it once.. in map if you are using get () method you need to give KEY which will give the value of the key.. Mr Hari krishna is very experienced java trainer in naresh it
Nice explanation sirr
Amazing explanation
You are discussed only one point of string class Immutable. there are other reasons also like
Yes...agree to many of you....this is only adavatage of string being Immutable.....string class did not become due to Hashmap
Yes Suresh, I agreed with you, but in all cases you addressing are internally uses (key,value) only. So for easy understanding I just took Hashtable as an example.
Thank you so much sir
Super sir.....vvvvvvvnice.....
Nice sir good explanation
Sir plzz upload video on java.util.regex
Super explanation Sir tq
Awesome !!
What about int
Muy bueno, gracias (Y)
Nice explanation sir :)
We want you to upload all core java tutorials.
Thank you sir ji
You rocks sir.never expected this kind of superb explanation..Great..
Thank u sir. Very helpful
Hello
Where did u completed ur b.tech
who r u and y r u asking
I think ur completed ur b.tech in kurnool
Is it right....
no no
nice sir
Before attending core Java I were hate with core Java BT now I m fan of u sir
because of u guys lot of scrap in it from Hyderabad
Krishna Gangaraju
can you pls share your thought on this topic, so that all we will learn from your ideas too. Just leaving one comment may not justify, try to correct my mistake if any and Help others to learn 😊
everything ends with a with this person ... personA stringA...etc
no offence but why dont you teach in your native tongue or in a language you are good
Nice explanation sir
Awesome explanation sir
Awesome explanation
this is absurd , cant we directly pass s1 while getting the value , that way even if it has changed , will give the correct value ,
Unique explanation.