StringBuffer substring Java Pass By Reference

StringBuffer substring Java Pass By Reference

Many petty methods exist with StringBuffer like substring(), setLength() and trimToSize() etc. These methods are not much used in regular programming.

public class MiscellaneousMethods
  public static void main(String args[])
  {                                                                  // using substring(), to extract part of a string
    StringBuffer buffer1 = new StringBuffer("TajMahal");
    String s1 = buffer1.substring(3);   
    System.out.println("Extracting from middle to last: " + s1);     // Mahal
    String s2 = buffer1.substring(1, 5);                             // extracting 1, 2, 3, 4 (5-1)
    System.out.println("Extracting in between: " + s2);              // prints ajMa
    				                                     // Using setLength()	
    StringBuffer buffer2 = new StringBuffer("GolcondaFort");
    System.out.println("buffer2 before setLength(): " + buffer2);    // GoldcondaFort     
    System.out.println("No. of characters in buffer2 before setLength(): " + buffer2.length());     // 12
    System.out.println("buffer2 capacity before setLength(): " + buffer2.capacity());               // 28  

    System.out.println("buffer2 after setLength(): " + buffer2);     // Goldconda     
    System.out.println("No. of characters in buffer2: " + buffer2.length());                        // 8
    System.out.println("buffer2 capacity after setLength(): " + buffer2.capacity());                // 28
					                             // Using trimToSize() 
    StringBuffer buffer3 = new StringBuffer(100);
    System.out.println("No. of characters in buffer3: " + buffer3.length());                        // length is 11
    System.out.println("Before trimToSize() capacity: " + buffer3.capacity());                      // 100
    System.out.println("After trimToSize() capacity: " + buffer3.capacity());                      // prints 11
    System.out.println("No. of characters in buffer3: " + buffer3.length());                       // prints 11

StringBuffer substring Java Pass By Reference
Output screen of

Output screen of of StringBuffer substring Java Pass By Reference

substring() method works the same way of strings. setLength() and trimTosize() do different jobs. Using setLength() method programmer can delete any extra unwanted text (content) in the buffer. Text will be cut off but not the buffer capacity. To cut the extra buffer capacity than required, use trimToSize() method. The above output clearly explains.


Java supports pass-by-reference through object assignment. Objects are passed as arguments. We know earlier, when objects are assigned one to another, they refer the same location of data. Pass-by-reference is illustrated with StringBuffer.

public class PBRDemo
  public void show(StringBuffer buffer2)
    System.out.println("From show() before append(): " + buffer2);            	// Good
    System.out.println("From show() after append(): " + buffer2);     	        // GoodMorning
  public static void main(String args[])
    PBRDemo pbrd = new PBRDemo();
    StringBuffer buffer1 = new StringBuffer("Good");              
    System.out.println("From main() before calling show(): " + buffer1);        // Good;
    System.out.println("From main() after calling show(): " + buffer1);         // GoodMorning	

StringBuffer substring Java Pass By Reference
Output screen of

Output screen of of StringBuffer substring Java Pass By Reference

Observe, the show() method requires an object of StringBuffer as parameter (not a data type). From the main(), for show() method buffer1 object is passed and is assigned implicitly to buffer2. Now buffer1 and buffer2 refer the same location. When buffer1 value changes buffer2 also gets affected or vice versa. The same thing happended here. buffer2 is appended with Morning which affects buffer2 also (infact they refer the same location). For this reason buffer1 from main() method displayed GoodMorning. It is exactly the same thing we achieved in C/C++ using pass-by-reference or call-by-reference.

Pass-by-reference is also explained with examples of array objects – Passing Arrays to Methods and Passing Arrays to Constructor.


StringBuilder is explained in comparison with String and StringBuffer with an example.


StringTokenizer is explained clearly in java.util package.

Pass your comments and suggestions to improve the quality of this tutorial "StringBuffer substring setLength Pass By Reference"

2 thoughts on “StringBuffer substring Java Pass By Reference”

  1. Hi Sir,
    I am ardent follower of your courses on Core Java and I improve myself every time with them. Thanks a lot in this regard. I am aspiring for Java Certified Associate Java SE 7 Programmer certification. Will you please let me know what to look for more for cracking it?

Leave a Comment

Your email address will not be published.