ct answerWhat will s2 contain after following lines of Java code?StringBuffer s1 = "Ram";StringBuffer s2 = s1.append(" Ali")
Question
What will s2
contain after following lines of Java code?
StringBuffer s1 = "Ram";
StringBuffer s2 = s1.append(" Ali");
Solution
The code you provided will not compile in Java because you are trying to assign a String to a StringBuffer. The correct way to declare and initialize a StringBuffer in Java is:
StringBuffer s1 = new StringBuffer("Ram");
After this correction, the code would look like this:
StringBuffer s1 = new StringBuffer("Ram");
StringBuffer s2 = s1.append(" Ali");
In this case, both s1
and s2
will contain "Ram Ali". This is because the append
method in Java modifies the original StringBuffer (s1
in this case) and also returns the same StringBuffer reference. So, s2
is just another reference to the modified s1
.
Similar Questions
What is the output of the following code snippet? String str1 = "Java"; String str2 = new String("Java"); System.out.println(str1 == str2);
Which of the following parameters is taken by the buffer.toString method?*2 pointsEncodingStartEndAll of the above
Consider the following code,string s=new String(); will create an instance of string with
What will be the output of the program?String s = "ABC"; s.toLowerCase(); s += "def"; System.out.println(s);ABCabcABCdefCompile Error
String myString = new String("This is a string!");System.out.println(myString.toString());What will be returned by this snippet of code?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.