Loading

JAVA_SE基础——62.String类的构造方法

下面我先列出初学者目前用到的构造方法

String 的构造方法:
 
  String()  创建一个空内容 的字符串对象。
  String(byte[] bytes)  使用一个字节数组构建一个字符串对象
  String(byte[] bytes, int offset, int length) 
  bytes :  要解码的数组
  offset: 指定从数组中那个索引值开始解码。
  length:要解码多个元素。
 
  String(char[] value)  使用一个字符数组构建一个字符串。
  String(char[] value, int offset, int count)  使用一个字符数组构建一个字符串, 指定开始的索引值,与使用字符个数。
String(int[] codePoints,int offset,int count)
String(String original) 

public class Demo2 {
	
	public static void main(String[] args) {
		String str = new String();//创建一个空内容的字符串对象  构造函数:String() 
		byte[] buf = {97,98,99};
		
		str = new String(buf); //使用一个字节数组构建一个字符串对象    构造函数:String(byte[] bytes) 
		str = new String(buf,1,2);   //使用一个字节数组构建一个字符串对象,指定开始解码 的索引值和解码的个数。
		//使用的构造函数是:String(byte[] bytes, int offset, int length) 
		char[] arr = {'明','天','是','圣','诞'};
		str = new String(arr); //使用字符数组构建一个字符串    String(char[] value)
		str = new String(arr,3,2);//使用了String(char[] value, int offset, int count)  构造函数
		
		int[] buf2 = {65,66,67};
		str = new String(buf2,0,3);//使用了 	String(int[] codePoints,int offset,int count) 构造函数
		
		str = new String("abc");//使用了	String(String original)   构造函数
		
		System.out.println("字符串的内容:"+str);
			
	}
	
}



交流企鹅:654249738,和自学者交流群:517284938

posted @ 2016-04-27 12:13  脚本叔叔  阅读(208)  评论(0编辑  收藏  举报