Menu
Question Index
...

What will be the output of the following program?

public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer firstBuffer = new StringBuffer("Merit Campus");
        System.out.println("First Buffer = " + firstBuffer);
        System.out.println("Old Capacity = " + firstBuffer.capacity());
        firstBuffer.ensureCapacity(27);
        System.out.println("New Capacity = " + firstBuffer.capacity());
        StringBuffer secondBuffer = new StringBuffer("Your Online Java School");
        System.out.println("Second Buffer = " + secondBuffer);
        System.out.println("Old Capacity = " + secondBuffer.capacity());
        secondBuffer.ensureCapacity(40);
        System.out.println("New Capacity = " + secondBuffer.capacity());
    }
}


First Buffer = Merit Campus
Old Capacity = 16
New Capacity = 27
Second Buffer = Your Online Java School
Old Capacity = 39
New Capacity = 40
First Buffer = Merit Campus
Old Capacity = 28
New Capacity = 28
Second Buffer = Your Online Java School
Old Capacity = 39
New Capacity = 80
Compilation Error
Runtime Error

Doubts

Problems

Topic: Java StringBuffer ensureCapacity() Method With Example

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App