Java Programming - Java.lang Class - Discussion


What will be the output of the program?

String a = "ABCD"; 
String b = a.toLowerCase(); 

[A]. abcd
[C]. dccd
[D]. dcba

Answer: Option A


String objects are immutable, they cannot be changed, in this case we are talking about the replace method which returns a new String object resulting from replacing all occurrences of oldChar in this string with newChar.

b.replace(char oldChar, char newChar);

But since this is only a temporary String it must either be put to use straight away i.e.


Or a new variable must be assigned its value i.e.

String c = b.replace('a','d');

Prabu said: (May 30, 2011)  
Option A is correct because

a=ABCD immutable means cannot be modified,
b=abcd it means we are converting upper to lower and storing in b

After that replace cannot be work, because immutable means we cannot modified.

Ami said: (Sep 24, 2013)  
But Why new Object doesn't change their value? if not create object then.

Bedada Beyene said: (Sep 15, 2015)  
Why the answer not ABCD OR DCCD?

Vino said: (Oct 18, 2015)  
Why the answer is abcd?

Manoj said: (Oct 25, 2016)  
Because b variable declared first.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.