Home / Java Programming / Java.lang Class :: Discussion

Discussion :: Java.lang Class

  1. What will be the output of the program?

     String a = "ABCD";  String b = a.toLowerCase();  b.replace('a','d');  b.replace('b','c');  System.out.println(b); 

  2. A.
    abcd
    B.
    ABCD
    C.
    dccd
    D.
    dcba

    View Answer

    Workspace

    Answer : Option A

    Explanation :

    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.

    System.out.println(b.replace('a','d'));

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

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


Be The First To Comment