Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

When string class is immutable, why isn't there any error occurred when we try to concatenate a particular string?

user-image
Question added by Advaitha Chidurala , Teacher , Little Masters Abacus
Date Posted: 2013/09/17

because from String class concatenate operation is not perform on perticular string object  but concatenate on new object, same object is not modified  , operation is perforned on new object rather than same object.

Ashok Yadav
by Ashok Yadav , trainee , S.O Infotech Pvt. Ltd.

for eg

String s="hello";

s=s+" Ashok";

in the first line it s will point to address where hello is stored and in2nd line same 's' is now point to hello Ashok

thus the memory buffer wil now have hello and hello ashok two string. earlier s points to hello and later will to hello ashok

By doing string class concatenate operation if current object is going to modify then only  it stores the result in a new object  and  returning it refernce  ,.unlike if we concate using + operator always it returns new object irrespective of current object is modifed or not  

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.