首页 > 代码库 > StringBuffer&Runtime demo

StringBuffer&Runtime demo

public class StringBufferDemo02 {
    public static void main(String[] args) {
        StringBuffer bf = new StringBuffer("I am bf,");
        // bf="I am a bf,";
        fun1(bf);
        String str = "I am str,";
        fun2(str);
        String str1 = fun3(str);
        System.out.println(bf);
        System.out.println(str);
        System.out.println(str1);
    }

    public static void fun1(StringBuffer sb) {
        sb.append(" has changed!");
    }

    public static void fun2(String str) {
        str += " HAS CHANGED!";
    }

    public static String fun3(String str) {
        str += " has changeeeeeeed!";
        return str;
    }
}

Output:

I am bf, has changed!
I am str,
I am str, has changeeeeeeed!

 

 

public class RunTimeDemo {
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        System.out.println("JVM‘S MAX MEMORY:" + runtime.maxMemory());
        System.out.println("JVM‘S FREE MEMORY:" + runtime.freeMemory());
        String string = "Hello" + "wORLD" + "!!!" + "\t" + "wellcomde" + "tO"
                + "VON" + "~~";
        System.out.println(string);
        for (int x = 0; x < 1000; x++) {
            string += x;
        }
        System.out.println("After String‘s operate,JVM‘S FREE MEMORY:"
                + runtime.freeMemory());
        runtime.gc();
        System.out.println("After Garbage collection,JVM‘S FREE MEMORY:"
                + runtime.freeMemory());
    }
}