String is an immutable object.
Then how can the following code be justified.
String s1 = ?ABC?;
String s1 = s1+?XYZ?;
s.o.p(s1);
The output is ABCXYZ, which is the value of s1 ?
Answer Posted / k.shiva
here we define s1="ABC".That means one new string is
created.And this string value is stored in heap its
refrence variable is s1.when we say s1+"XYZ".Then string s1
i.e ABC and XYZ are concadinated by using concadination
operater.It forms a new string i.e "ABCXYZ" in heap.But
again we assign this value to the refrence variable
s1.Then "ABCXYZ" value is assign to s1 and the string "ABC"
which is garbage collected.
| Is This Answer Correct ? | 12 Yes | 4 No |
Post New Answer View All Answers
What is a class instance variable?
What is the purpose of return statement?
How can we create objects if we make the constructor private ?
State the merge-sort principle and its time complexity.
Why is it called buffering?
What is the use of pattern in java?
How would overload a function based on return type?
Where are variables stored?
What is final int?
If A Class Is Declared Without Any Access Modifiers, Where May The Class Be Accessed?
What is outofmemoryerror in java?
What are the various access specifiers for java classes?
What are the legal operands of the instanceof operator?
What is finalize() function in java?
Which programming language is most secure?