public class StringBuilderConstructors	{		
	public static void main(String args[]) {

	StringBuilder sb3 = new StringBuilder("hello");
	
	
        System.out.printf("buffer3 = %s\n", sb3.toString()); //written in textbook
	System.out.printf(sb3); //i tried
	System.out.println("buffer3=" + sb3); // i tried
}}
o/p:
buffer3 = hello
hello
buffer3=hello

doubt:
The book says toString() gives the string representation an object. That is, according to the first line of System.out....What I want to know is why is that needed when the other two ways of System.out.. gives the same output? I know its used to give string representation of user defined objects but is it really needed for StringBuffer/StringBuilder as they are also string anyways?

when you call
System.out.println(sb3);
a bit hidden by the compiler, this will call the toString method of your Object:
System.out.println(sb3.toString());
so, it's quite normal it gives the same result :)

And a user can overload the .toString() method to take the object in question an display it as you want.

commented: overloading the toString() method is ... ridiculous to say the least. -3
public class StringBuilderConstructors	{		
	public static void main(String args[]) {

	StringBuilder sb3 = new StringBuilder("hello");
	
	
        System.out.printf("buffer3 = %s\n", sb3.toString()); //written in textbook
	System.out.printf(sb3); //i tried
	System.out.println("buffer3=" + sb3); // i tried
}}
o/p:
buffer3 = hello
hello
buffer3=hello

doubt:
The book says toString() gives the string representation an object. That is, according to the first line of System.out....What I want to know is why is that needed when the other two ways of System.out.. gives the same output? I know its used to give string representation of user defined objects but is it really needed for StringBuffer/StringBuilder as they are also string anyways?

They both are strings already thats why... if you use this

public String toString(){
    return "{a:"+ a + ", b:" + b + ", c: " + c +"}";
}

the compiler would compile it into a String builder itself which will look like this

public String toString(){
    StringBuilder sb = new StringBuilder(100);
    return sb.append("{a:").append(a)
          .append(", b:").append(b)
          .append(", c:").append(c)
          .append("}")
          .toString();
}

and im guessing the toString() method is there just to make sure all concacted outputs are in the right formats,but as you have seen its not crucial-although its always better to follow the java programming standards using the toString at the end of a stringbuilder

And a user can overload the .toString() method to take the object in question an display it as you want.

this makes no sense at all.

why would you want to overload the toString object? what you propose to do is:

public class Car{
int nrWheels, nrDoors;
String type;

public Car(int nrWheels, int nrDoors, String type){
  this.nrWheels = nrWheels;
  this.nrDoors = nrDoors;
  this.type = type;
}

public int getNrWheels(){
  return this.nrWheels;
}

public void setNrWheels(int nrWheels){
  this.nrWheels = nrWheels;
}

public int nrDoors(){
  return this.nrDoors;
}

public void setNrDoors(int nrDoors){
  this.nrDoors = nrDoors;
}

public String getType(){
  return this.type;
}

public void setType(String type){
  this.type = type;
}

public void toString(){
  return "Car of type: " + this.type + " with " + this.nrDoors + " doors and " + this.nrWheels + " wheels.";
}

public void toString(Car input){
  return "Car of type: " + input.getType() + " with " + input.getNrDoors() + " doors and " + input.getNrWheels() + " wheels.";
}

public static void main(String[] args){
  Car myCar = new Car(4, 5, "BMW");
  String firstOut = myCar.toString();
  String secondOut = myCar.toString(myCar);
  // here you are running a method from an instance of car, so you know it
  // has access to all the variables there-in, yet you pass that same Object
  // tó that method, to get access to that data?
  // this is what could be referred to as: ye olde chicken or egg question.
  System.out.println(firstOut);
  System.out.println(secondOut);
  // of course they produce the same output, but ... why on earth would you want
  // this? if you call System.out.println(myCar); it will always take the toString();
  // method, never the toString(Car myCar); version anyway
}
}

this makes no sense at all.

why would you want to overload the toString object? what you propose to do is:

public void toString(){
  return "Car of type: " + this.type + " with " + this.nrDoors + " doors and " + this.nrWheels + " wheels.";
}

public void toString(Car input){
  return "Car of type: " + input.getType() + " with " + input.getNrDoors() + " doors and " + input.getNrWheels() + " wheels.";
}

Well I didnt mean the latter of the two, just the former

Well I didnt mean the latter of the two, just the former

what 'former'?

well What I said was

The nice thing is you could do this

this is the former of the two code blocks

public void toString(){
  return "Car of type: " + this.type + " with " + this.nrDoors + " doors and " + this.nrWheels + " wheels.";
}

you could do the the following, but then i wouldnt put that in a normal class, and make it a static method

this is the latter of the two code blocks

public void toString(Car input){
  return "Car of type: " + input.getType() + " with " + input.getNrDoors() + " doors and " + input.getNrWheels() + " wheels.";
}

well What I said was

The nice thing is you could do this

this is the former of the two code blocks

public void toString(){
  return "Car of type: " + this.type + " with " + this.nrDoors + " doors and " + this.nrWheels + " wheels.";
}

you could do the the following, but then i wouldnt put that in a normal class, and make it a static method

this is the latter of the two code blocks

public void toString(Car input){
  return "Car of type: " + input.getType() + " with " + input.getNrDoors() + " doors and " + input.getNrWheels() + " wheels.";
}

you may have explained it wrong then :)

having an own version of toString() is not overloading, it's inheritance.
overloading means: having several methods with identical names, but with different method signatures (which basically means, they don't have the same parameters)
for instance:

methodA()
methodA(int test)
and
methodA(int test, String description)

are overloaded methods.

when you said:

overload the .toString() method to take the object

I took this as:
create a toString method which takes the object as parameter.

you may have explained it wrong then :)

having an own version of toString() is not overloading, it's inheritance.
overloading means: having several methods with identical names, but with different method signatures (which basically means, they don't have the same parameters)
for instance:

methodA()
methodA(int test)
and
methodA(int test, String description)

are overloaded methods.

when you said:

I took this as:
create a toString method which takes the object as parameter.

ahh My apologies then.

Thanks all you guys. Appreciate the help. Have understood my query :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.