StringBuffer sb = new StringBuffer("Hello World"); int len = sb.length(); System.out.println("Length of StringBuffer: " + len);
StringBuffer sb = new StringBuffer(); sb.append("This is a "); sb.append("test."); int len = sb.length(); System.out.println("Length of StringBuffer: " + len);Output: Length of StringBuffer: 14 In this example, an empty StringBuffer object is created and two strings are appended to it using the append() method. The length of the StringBuffer object is then retrieved using the length() method and printed to the console. Overall, the length() method provides a useful way to retrieve the length of a StringBuffer object in Java.