Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

public class Main {
    public static void main(String[] args) {
        // Create an empty StringBuffer
        StringBuilder sb = new StringBuilder();
        printDetails(sb);

        // Append "good"
        sb.append("good");
        printDetails(sb);

        // Insert "Hi " in the beginning
        sb.insert(0, "Hi ");
        printDetails(sb);

        // Delete the first o
        sb.deleteCharAt(1);
        printDetails(sb);

        // Append "  be  with  you"
        sb.append(" be  with  you");
        printDetails(sb);

        // Set the length to 3
        sb.setLength(3);
        printDetails(sb);

        // Reverse the content
        sb.reverse();
        printDetails(sb);
    }

    public static void printDetails(StringBuilder sb) {
        System.out.println("Content: \"" + sb + "\"");
        System.out.println("Length: " + sb.length());
        System.out.println("Capacity: " + sb.capacity());

        // Print an empty line to separate results
        System.out.println();
    }
}