首页 > 编程语言 > 详细

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

时间:2016-04-27 17:14:59      阅读:211      评论:0      收藏:0      [点我收藏+]

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

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

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

原文:http://blog.csdn.net/thescript_j/article/details/51259929

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!