String is a immutable objects . it means that string does
not change...........
But it will be chang.........
{
String s="kapil";
String s1="raj";
String s=s1;
then print(.......)
The String has been changed ..
how it is possible and why its called immutable objects

Answer Posted / mintu una

String is Immutable : means when we did String
certficateFileName =” CheckSecurity” ; now one object of
String type is created in memory with
value “CheckSecurity” where as certficateFileName is mere
reference to that, now let say some another program or
user wants ”CheckSecurity” and they create String object
with file name String myCertFileName =”CheckSecurity”.
At this point certficateFileName and myCertFileName there
are two references but String object is same
i.e. ”CheckSecurity”, as java decided to make this String
class Immutable because if second program does like
myCertFileName = myCertFileName + “.cer” then new String
object is created in memory with vale “CheckSecurity.cer”
and reference to this is myCertFileName. myCertFileName
reference is removed from String object having
value “CheckSecurity”.Note that “CheckSecurity” String
object valuedid not changed it is same as earlier because
String class is immutable and its reference at this point
is only “certficateFileName” not the myCertFileName .
So String class also immutable for Security reason so that
once String object is declared its value will not be
changed, if changed then new object is created and moreover
if let us say 10 references in memory to same String
object then only one object is there so memory is also
managed with this… Hope it will help to understand the
concept

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference between path and classpath?

564


Why does java have two ways to create child threads?

466


What do you understand by java virtual machine?

709


What is early binding and late binding in java?

595


What about method local inner classes or local inner classes in java?

557






What is the role of garbage collector in java?

493


What is namespace in java?

545


Is binary a low level language?

509


How do you convert an int to a string in java?

546


What is the purpose of the return statement?

519


What is the relationship between class and object?

527


Does windows 10 need java?

599


What is difference between next () and nextline () in java?

540


What is jar?

635


What is the purpose of static methods and static variables?

543