Results 1 to 8 of 8
Thread: StringBuffer no-arg constructor
- 08-27-2013, 12:43 AM #1
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
StringBuffer no-arg constructor
How do I assign a value to a stringBuffer object if I am using default constructor?
The following code does not compile:
Java Code:public class StringBufferDemo { public static void main(String args[]) { StringBuffer sb1=new StringBuffer(); sb1="1234567890"; } }
- 08-27-2013, 01:57 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: StringBuffer no-arg constructor
I have never needed to do this so I don't know. But I just looked at the API and what I do know is how I could approach it and even test it. Did you read the API and look at the methods? Do you see anything that might provide an answer? If I have to read the API to help answer some of these questions then I expect you and others to make an attempt also.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-27-2013, 10:27 AM #3
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: StringBuffer no-arg constructor
a StringBuffer is not a String, so why are you trying to assign a String to a StringBuffer?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-27-2013, 01:06 PM #4
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
Re: StringBuffer no-arg constructor
There are four constructors for StringBuffer:
StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
If I want to use the first version of the overloaded constructor, how to assign a value? You will be ultimately be using 3rd or 4th version of the constructor to assign the value, so what is the use of no-arg constructor?
- 08-27-2013, 01:15 PM #5
Re: StringBuffer no-arg constructor
Suhaas, you need to get your terminology straight before continuing to post 'questions' here. The line
Java Code:StringBuffer sb = new StringBuffer();
What you are referring to as 'a value' is anybody's guess.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-27-2013, 01:38 PM #6
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
Re: StringBuffer no-arg constructor
Ok, lets use the line:
StringBuffer sb = new StringBuffer();
Now suppose I want to assign "abc" to it. How do I go about it?
- 08-27-2013, 01:40 PM #7
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: StringBuffer no-arg constructor
You're still not reading the API documentation. Do you want to be stuck forever? When are you actually going to LISTEN to the advice you ask for?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-27-2013, 03:27 PM #8
Re: StringBuffer no-arg constructor
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
call default constructor inside of parameterized constructor after conditional check
By Googol in forum New To JavaReplies: 5Last Post: 08-11-2012, 10:50 AM -
Java - Constructor Method versus Constructor
By brocksoffice in forum New To JavaReplies: 1Last Post: 08-01-2012, 10:17 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum Advanced JavaReplies: 3Last Post: 03-04-2009, 07:03 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum New To JavaReplies: 1Last Post: 03-03-2009, 06:27 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 10:10 AM
Bookmarks