Tuesday, July 22, 2014

Strings are immutable?




Q4-8. What is the output of the following code?



class EJavaGuruString2 {



public static void main(String args[]) {



String ejg = "game".replace('a', 'Z').trim().concat("Aa");



ejg.substring(0, 2);



System.out.println(ejg);



}



}



a gZmeAZ



b gZmeAa



c gZm



d gZ



e game



Answer: b




Q4-9. What is the output of the following code?



class EJavaGuruString2 {



public static void main(String args[]) {



String ejg = "game";



ejg.replace('a', 'Z').trim().concat("Aa");



ejg.substring(0, 2);



System.out.println(ejg);



}



}



a gZmeAZ



b gZmeAa



c gZm



d gZ



e game



Answer: e




WHY? the first one when u use methods such as replace, it changes the string, but the second example still uses replace but nowwww it saying that stringes are immutable!







No comments:

Post a Comment