几种常见的string 构造方法
/* * 主要介绍几种string类的构造方法 * 1、string() * 2.string(byte[] bytes),传的是byte类型的数组 * 3.string(byte[] bytes,int index,int length) * 4.string(char[] value),传的是char类型的数组 * 5.string(char[] value,int index,int length) * 6.string(string str) ,传的是字符串作为参数 */ package cn.string; public class StringDemo { public static void main(String[] args) { // TODO Auto-generated method stub //第一种方法 ,string(),没有意义 String s1 = new String (); System.out.println("s1.String():"+s1); System.out.println("s1.String():"+s1.length()); //第二种方法,string(byte[] bytes) byte [] byts = {97,98,99,100,101}; String s2 = new String(byts); System.out.println("s2.String():"+s2); System.out.println("s2.String():"+s2.length()); //第三种方法,string(byte[] bytes,int index,int length) String s3 = new String(byts,2,3); System.out.println("s3.String():"+s3); System.out.println("s3.String():"+s3.length()); //第四种方法 string(char[] value) char [] by = {'a','b','c','d','e','f'}; String s4 = new String(by); System.out.println("s4.String():"+s4); System.out.println("s4.String():"+s4.length()); //第五种方法 string(char[] value,int index,int length) String s5 = new String(by,3,3); System.out.println("s4.String():"+s5); System.out.println("s4.String():"+s5.length()); //第六种方法,string(string str) String str = "abcdef"; String s6 = new String (str); System.out.println("s6.String():"+s6); System.out.println("s6.String():"+s6.length()); } }