package com.lovecoding.day16.api04;

import org.junit.Test;

public class Test01 {

    @Test
    public void test11(){
        //空字符串
        String str1 = "";
        String str2 = new String();
        String str3 = new String("");

        if("".equals(str1)){
            System.out.println(1);
        }
        if(str2!=null  && str2.isEmpty()){
            // public boolean isEmpty() {
            //     return value.length == 0; //value 数组 length属性
            //}
            System.out.println(2);
        }
        if(str3!=null && str3.equals("")){
            System.out.println(3);
        }

        if(str2!=null && str2.length()==0){
            System.out.println(4);
        }
    }

    @Test
    public void test10(){
        String str1 = "hello";
        String str2 = "hello";
        System.out.println(str1.equals(str2));//true

        /* 字符的比较
        for (int i = 0; i < value.length; i++) {
            if (value[i] != other[i]) {
                return false;
            }
        }
        */

        String str3 = new String("hello");
        String str4 = new String("hello");
        System.out.println(str1.equals(str3));//true
        System.out.println(str3.equals(str4));//true
    }

    @Test
    public void test09(){
        String str1 = "hello";
        String str2 = "hello";
        System.out.println(str1 == str2);

        String str3 = new String("hello");
        String str4 = new String("hello"); //堆中
        System.out.println(str1 == str4);
        System.out.println(str3 == str4);
    }

    @Test
    public void test08(){
        String str = "hello";
        String str2 = "world";
        String str3 ="helloworld";

        String str4 = "hello".concat("world");
        String str5 = "hello"+"world";

        System.out.println(str3 == str4);//false
        System.out.println(str3 == str5);//true
    }

    @Test
    public void test07(){

        String str = "0";
        for (int i = 0; i <= 5; i++) {
            str += i; // "+"+1 = 01 创建  012 创建
        }
        System.out.println(str);

    }

    @Test
    public void test06(){
        String s1 = "hello";
        String s2 = "world";
        String s3 = "helloworld";

        String s4 = (s1 + "world").intern();//把拼接的结果放到常量池中 helloworld
        String s5 = (s1 + s2).intern(); //helloworld

        System.out.println(s3 == s4); //t
        System.out.println(s3 == s5); //t
    }
    @Test
    public void test05(){
        final String s1 = "hello"; //常量 常量池
        final String s2 = "world"; //常量 常量池
        String s3 = "helloworld";  //常量 常量池

        String s4 = s1 + "world";
        String s5 = s1 + s2;
        String s6 = "hello" + "world";

        System.out.println(s3 == s4); //t
        System.out.println(s3 == s5); //t
        System.out.println(s3 == s6); //t
    }
    @Test
    public void test04(){
        String s1 = "hello";
        String s2 = "world"; //常量
        String s3 = "helloworld"; //常量

        String s4 = s1 + "world"; //常量+变量 = 结果在堆中
        String s5 = s1 + s2; //堆中
        String s6 = "hello" + "world"; //常量池

        System.out.println(s3 == s4); //f
        System.out.println(s3 == s5); //f
        System.out.println(s3 == s6); //t
    }
}
