Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Advaitha Chidurala , Teacher , Little Masters Abacus
Date de publication: 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
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?