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



String is a immutable objects . it means that string does not change........... But it will be ch..

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

String is a immutable objects . it means that string does not change........... But it will be ch..

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

String is a immutable objects . it means that string does not change........... But it will be ch..

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

String is a immutable objects . it means that string does not change........... But it will be ch..

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

String is a immutable objects . it means that string does not change........... But it will be ch..

Answer / kapil dalke

Thank You ramaraju....

Is This Answer Correct ?    0 Yes 0 No

String is a immutable objects . it means that string does not change........... But it will be ch..

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

String is a immutable objects . it means that string does not change........... But it will be ch..

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

Post New Answer

More Core Java Interview Questions

Can a string be null?

0 Answers  


What is object in java?

0 Answers  


different between exception and error? explaim check and uncheck exception

4 Answers  


What is structure of java heap? What is perm gen space in heap?

0 Answers  


Explain super keyword in java.

0 Answers  






Can you start a thread twice in Java?

0 Answers  


What is an off by one error in java?

0 Answers  


What is the meaning of flag day?

0 Answers  


Is 0 true or false in java?

0 Answers  


What are the two ways you can synchronize a block of code?

5 Answers   Ericsson,


Distinguish method overloading and method overriding

4 Answers   Tech Mahindra,


When try and catch block is used ?

6 Answers  


Categories