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 / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a finally block? Is there a case when finally will not execute?

544


is there a separate stack for each thread in java? : Java thread

535


What is a top level class in java?

525


Why do we use threads in java?

561


Write a program to print fibonacci series up to count 10.

518






What is a bubble sort in java?

543


How to use Media tracker Class.

642


Can a set contain duplicates?

515


Can inner class final?

631


What is data string?

493


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

569


Explain the difference between abstract class and interface in java?

533


Define immutable object?

577


What is a final class in java?

539


Explain about class in java?

608