aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer    & String ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer    & String ?? " Watch "StringBuffer    & String ?? " New topic
Author

StringBuffer & String ??

Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Why the following code give the compile error. Any comments ...
================================================================
class Q2
{
public static void main(String arg[])
{
StringBuffer s[]={"A","B"};
System.out.println(s[0]+","+s[1]);
}
}
================================================================
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Ashish Jaiman
Ranch Hand

Joined: May 01, 2001
Posts: 47
Cause U have to create StringBuffer there is no constructor as
StringBuffer s="A";
hence this would work
StringBuffer s[]={new StringBuffer("A"),new StringBuffer("B")};
System.out.println(s[0]+","+s[1]);

-AJ<br />Sun Certified Java Programmer<br />Microsoft Certified Software Developer
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
You will have to use s[].append method to add data into the stringbuffer
Originally posted by Muhammad Farooq:
Why the following code give the compile error. Any comments ...
================================================================
class Q2
{
public static void main(String arg[])
{
StringBuffer s[]={"A","B"};
System.out.println(s[0]+","+s[1]);
}
}
================================================================
--Farooq

Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Here's my addition Muhammad
Get your clue from the error message -
---------- javac ----------
Q2.java:8: Incompatible type for array. Can't convert java.lang.String to java.lang.StringBuffer.
StringBuffer s[]={"A","B"};
^
1 error
Normal Termination
Output completed (2 sec consumed).
A String object is implicitly made for string literals . You are trying to assign String objects to
an array of StringBuffer type . Besides their individual class hierarchies are such that they will not
support a conversion(narrowing/widening/casting etc.) aswell .
I don't think s[].append will add items as individual array elements .
Correct me if i am wrong .
Ashish
[This message has been edited by Ashish Hareet (edited July 25, 2001).]
 
jQuery in Action, 2nd edition
 
subject: StringBuffer & String ??