欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

String类的常用构造方法的小小演示

程序员文章站 2022-04-08 14:50:17
...
public class Demo1 {
	//I'm zhuobinzhou 
	//不加static,将报错:Cannot make a static reference to the non-static field str
	static String str = new String();//使用String()创建一个空字符序列
	static String str1 = new String("I'm");
	static char[] ch = new char[]{' ','z','h','u','o'};
	static String str2 = new String(ch);
	static int[] cp = new int[]{98,105,110};
	static String str3 = new String(cp,0,3);
	static byte[] bytes = new byte[]{97+25,97+7,97+14,97+20};
	static String str4 = new String(bytes,0,4);
	static StringBuffer buffer = new StringBuffer(". " +
			"I want to make friends with you.");
	static String str5 = new String(buffer);
	static StringBuilder builder = new StringBuilder("If you want," +
			"please add my !");
	static String str6 = new String(builder);
	
	public static void main(String args[]){
		
		str = "Hello,";
		System.out.println(str);
		System.out.println(str1);
		System.out.println(str2);
		System.out.println(str3);
		System.out.println(str4);
		System.out.println(str5);
		System.out.println(str6);
		str += str1 + str2 + str3 + str4 + str5 + str6 ;
		System.out.println(str);
		
		
	}
	
}

 效果:

Hello,
I'm
 zhuo
bin
zhou
. I want to make friends with you.
If you want,please add

Hello,I'm zhuobinzhou. I want to make friends with you.If you want,please add my QQ number :750603686!

相关标签: QQ