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
Answers were Sorted based on User's Feedback
Answer / ramaraju
hi
String is a Immutable object,that is
string is an object not a datatype variable.
From the above example
String s=s1 //means it will create a new objet s with the
value raj,not change the value.
Note:whenever u edit or concatinate the string internally
it will create a new object,but not chang that object.
Is This Answer Correct ? | 16 Yes | 4 No |
Answer / jay
First the JVM will creates two objects s and s1 separately
when s=s1 is done JVM creates the new object and stores the
string "raj" in that object.But it does not modify the
contents of string s.After creating the new object the
reference of s is adjusted to refer the new object.
The point we observe here is that the contents of of the
string s is are not modified.This is the reason Strings are
called Immutable.The old object that contains "kapil" has
lost its reference.so it is called "Unreferenced object" the
garbage collector will removes it from memorey.
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / christine
Immutable means if you apply any methods to a String, it
would not affect the original String you created.
For eg,
String s = "test";
s.concat(" two");
System.out.println(s);
The output is still "test" and not "test two";
Java pass by reference. If you use = to assign the string
to another string, then the reference would change.
Therefore your example changes the string.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / 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 |
Answer / gohil
Anil Kumar Khichar is wrong there is no issue in assigning
s1 to s.
In case if you do so. Only a referenced is copied to s.
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / anil kumar khichar
Immutable means the original String never get changed or
replaced by another one. You can simply concate another one
, but beware you can't replace it. Look at the following:-
{
String s="kapil";
String s1="raj";
String s=s1;
see here if you assign s1 to s ,there will you get error.
And it's not allowed here. So we can say Strings are immutable.
Thanks!
Anil
Is This Answer Correct ? | 1 Yes | 4 No |
What is a serializable interface?
Hi Friends, I am beginner in java. what i know about synchonized keyword is,If more that one 1 thread tries to access a particular resource we can lock the method using synchronized keyword. Then after that how the lock is released and how next thread access that.Please explain with example.
What is the purpose class.forname method?
What is finalize() function in java?
What are 3 data types?
How can we achieve thread safety in java?
What is an example of procedure?
Explain jdk, jre and jvm?
What is immutable data?
Can we overload final method in java?
Why is sizeof not a function?
In a my eclipse editor if i want to switch from jdk 1.4 to jdk 1.6 how to do that???? Initially i have jdk1.4