os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)
code:
package jizuiku0; public class Demo00 { public static void main(String[] args) { //无参构造 StringBuffer sb = new StringBuffer();//看API手册+源码可得,初始容量为16 System.out.println(sb.toString()); System.out.println(sb.length());//实际长度:0 System.out.println(sb.capacity());//capacity容量:16 System.out.println("-------------------------"); //指定capacity构造 StringBuffer sb1 = new StringBuffer(50);//看API手册+源码可得,初始容量为50 System.out.println(sb1.toString()); System.out.println(sb1.length());//实际长度:0 System.out.println(sb1.capacity());//capacity容量:50 System.out.println("-------------------------"); //指定字符串构造 StringBuffer sb2 = new StringBuffer("jizuiku");//看API手册+源码可得,初始容量为str.length()+16 System.out.println(sb2.toString()); System.out.println(sb2.length());//实际长度:7 System.out.println(sb2.capacity());//capacity容量:7+16 } }
result:
Java优秀,值得学习。
学习资源:API手册+Java源码+清净的心地。
时间: 2024-11-07 02:20:59