String类的构造方法

来源:互联网 发布:新疆干部网络培训学院 编辑:程序博客网 时间:2024/05/29 00:32
字符串是常量,一旦被赋值,其值不能在改变!
常用的构造方法:

public String():无参构造
public String(byte[] bytes):将字节数组转换成字符串
public String(byte[] bytes,int offset,int length):将字节数组的一部分转换成字符串
public String(char[] value):将字符数组转换成字符串
public String(char[] value,int offset,int count):将字符数组的一部分转换成字符串
public String(String original):将一个常量转换字符串:String 是不可变的,所以无需使用此构造方法

public class StringDemo {    public static void main(String[] args) {        //无参构造:        String s = new String() ;        System.out.println(s);//空字符序列        System.out.println(s.length());//0        System.out.println("-----------------------");        //public String(byte[] bytes):将字节数组转换成字符串        //构造一个字节数组,静态初始化        byte[] bys = {97,98,99,100,101} ;//abcde        String s2 = new String(bys) ;        System.out.println(s2);        System.out.println(s2.length());//5        //public String(byte[] bytes,int offset,int length):将字节数组的一部分转换成字符串        String s3 = new String(bys, 0, 3) ;        System.out.println(s3);//abc        System.out.println(s3.length());        System.out.println("-------------------------");        //创建一个字符数组        char[] ch = {'a','b','c','d','e','爱','高','圆','圆'} ;        //public String(char[] value):将字符数组转换成字符串        String s4 = new String(ch) ;        System.out.println(s4);     //abcde爱高圆圆        System.out.println(s4.length());        System.out.println("-------------------------");//      public String(char[] value,int offset,int count):将字符数组的一部分转换成字符串:char[] value,int index,int length        String s5 = new String(ch, 5, 4) ;        System.out.println(s5);         //爱高圆圆        System.out.println(s5.length());        //public String(String original):将一个常量转换字符串:由于String 是不可变的,所以无需使用此构造方法        String s6 = new String("hello") ;        System.out.println(s6);         //hello        System.out.println(s6.length());        //创建字符串对象s6相当于 s7字符串对象的创建方式        String s7 = "hello" ;        System.out.println(s7);       //hello        System.out.println(s7.length());    }}
/* * 面试题: *      String s1 = new String("hello");创建了几个对象 *      String s2 = "hello" ;创建了几个对象 *       *  s1创建了两个对象 *  s2创建了一个对象,直接指向常量池中的! *  * ==默认比较的是地址值 * equals:默认的比较的是地址值,String类型底层已经重写了Object类中equals方法,比较的是内容是否相同! * */public class StringDemo2 {    public static void main(String[] args) {        //创建字符串对象        String s1 = new String("hello") ;        String s2 = "hello" ;        System.out.println(s1==s2);//false        System.out.println(s1.equals(s2));//true    }}
public class StringDemo {    public static void main(String[] args) {        String s1 = new String("hello");        String s2 = new String("hello");        System.out.println(s1 == s2);//false        System.out.println(s1.equals(s2));//true        String s3 = new String("hello");        String s4 = "hello";        System.out.println(s3 == s4);// false        System.out.println(s3.equals(s4));// true        String s5 = "hello";    // 进入了对象池中        String s6 = "hello";        System.out.println(s5 == s6);//   true          System.out.println(s5.equals(s6));//   true    }}
/* * 看程序,写结果! *  * 字符串变量相加,先开辟空间,在相加 *  * 字符串常量相加,先相加,然后在常量池中,是否有这个值,由直接返回地址值,没有就创建 * */public class StringDemo4 {    public static void main(String[] args) {        String s1 = "hello";        String s2 = "world";        String s3 = "helloworld";        System.out.println(s3 == s1 + s2);// false        System.out.println(s3.equals((s1 + s2)));//true        System.out.println(s3 == "hello" + "world");// true        System.out.println(s3.equals("hello" + "world"));//true    }}