This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String buffer problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String buffer problems" Watch "String buffer problems" New topic
Author

String buffer problems

Kamil Dada
Ranch Hand

Joined: Aug 07, 2001
Posts: 43
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
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
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.

Swati Kale
SCJP
SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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!
 
jQuery in Action, 2nd edition
 
subject: String buffer problems
 
Similar Threads
StringBuffer capacity
StringBuffer and String returned from a method doubt
Concatenation of Strings using String vs. StringBuffer
I thought it should be "Hello" instead of "Hello how are you"
How many String are created??