Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: StringBuffer no-arg constructor

  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default 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";
    	}
    }
    PS: I know we can use the constructor StringBuffer(String str), but what is the use of the no-arg constructor if we can assign values only by using StringBuffer(str)?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,029
    Rep Power
    6

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default 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

  4. #4
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default 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?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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();
    declares a variable sb of type StringBuffer and assigns a value to it, a reference to a newly created StringBuffer.

    What you are referring to as 'a value' is anybody's guess.

    db
    PhHein likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default 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?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default 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

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: StringBuffer no-arg constructor

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    Ok, lets use the line:

    StringBuffer sb = new StringBuffer();

    Now suppose I want to assign "abc" to it. How do I go about it?
    There isn't any way you can assign a String literal to a StringBuffer variable in Java.

    Maybe you'd be happier with a language that isn't type safe.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 10:50 AM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 10:17 AM
  3. Replies: 3
    Last Post: 03-04-2009, 07:03 AM
  4. Replies: 1
    Last Post: 03-03-2009, 06:27 AM
  5. Replies: 0
    Last Post: 12-19-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •