Forums Register Login

String buffer problems

+Pie Number of slices to send: Send
1 class Q2
2 {
3 public static void main(String arg[])
4 {
5 StringBuffer s[]={"A","B"};
6 System.out.println(s[0]+","+s[1]);
7 }
8 }
The answer was: Comile time error:Casting needed to convert String to StringBuffer in line no. 5.
Firstly what is StringBuffer and why is it giving me this error?
Thank You,
Kamil
+Pie Number of slices to send: Send
Hi,
StringBuffer is a java API class.You can create mutable strings using this class.StringBuffer objects should be created using new operator.
StringBuffer buf = new StringBuffer("Javaranch");
In your code, you r trying to create in a wrong way.
Go thru the Java API specification for more details.
+Pie Number of slices to send: Send
Swati is right!
When using stringBuffer in an array try this code
<CODE>


class Q2 {
public static void main(String arg[]) {
StringBuffer s[]={new StringBuffer( "A" ), new StringBuffer( "B" ) };
System.out.println( s[0].toString() + "," + s[1].toString() );
}
}

</CODE>
Hope this helps!
Something about .... going for a swim. With this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 592 times.
Similar Threads
How many String are created??
Concatenation of Strings using String vs. StringBuffer
I thought it should be "Hello" instead of "Hello how are you"
StringBuffer and String returned from a method doubt
StringBuffer capacity
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:40:55.