Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5
Java String objects are immutable i.e. you can not alter a string object once it is created at the same memory address. for e.g., we have String s="sololearn"; now, if you want to make use reverse of this string, then s.reverse(); wont work(i.e. s will not be updated with reverse of string) , as you can not update the current string as it is immutable, you will have to assign the newly created string to the refernce variable again i.e. s=s.reverse(); now, s.reverse() will reverse the string and it will create string in string pool and refernce of new string i.e. reverse of sololearn will be assigned to the same refernce variable. so, we didnt update sololearn at the same memory address, we created new string which was reverse of sololearn and then assigned it to the reference variable again. StringBuffer and StringBuilder are mutable so we can alter the object, so in this case, s.reverse() will work, i.e. it will reverse the content of string and will update the reference object.
20th Jul 2018, 11:38 AM
Jain Rishav Amit
Jain Rishav Amit - avatar