动手动脑

动手动脑:

1.源代码:

public class StringPool {
    
    public static void main(String args[])
    {
        
        String s0="Hello";
        
        String s1="Hello";
        
        String s2="He"+"llo";
        
        System.out.println(s0==s1);//true
        
        System.out.println(s0==s2);//true
        
        System.out.println(new String("Hello")==new String("Hello"));//false
        
    }
}

Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0s1s2实际上引用的是同一个对象。编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字串(“Hello”)。这种优化工作由Java编译器自动完成。当直接使用new关键字创建字符串对象时,虽然值一致(都是“Hello”),但仍然是两个独立的对象。

2.String.equals()源代码:

public class StringEquals {

    
/**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
        
        String s1=new String("Hello");
        
        String s2=new String("Hello");

        
        System.out.println(s1==s2);
        
        System.out.println(s1.equals(s2));

        
        String s3="Hello";
        
        String s4="Hello";

          
        System.out.println(s3==s4);
        
        System.out.println(s3.equals(s4));
        
    
    }


}

当直接使用new关键字创建字符串对象是,虽然值一致,但仍然是两个独立的对象,所以输出falseString.equals()方法比较的是两个字符串的内容,s1s2的内容都是“Hello”所以是true。在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s3s4实际上引用的是同一个对象,所以最后两个都是true

3.String类型当比较不同对象内容是否相同时,应该用equals,因为“==”用于比较引用类型和比较基本数据类型时具有不同的功能。

(1)当对象不同,内容相同,"=="返回falseequals返回true

String s1=new String(java);

String s2=new String(java);

System.out.println(s1==s2);//false

System.out.println(s1.equals(s2));//true

(2)当同一对象,"=="equals结果相同

String s1=new String(java);

String s2=s1;

System.out.println(s1==s2);//true

System.out.println(s1.equals(s2));//true

(3)如果值不相同,对象就不相同,所以"==" equals结果一样

String s1=java;

String s2=java;

System.out.println(s1==s2);//true

System.out.println(s1.equals(s2));//true

 String类的Length(),charAt(),getChars(),replace(),toUpperCase(),toLowerCase(),trim(),toCharArray()使用说明:

length():public int length()//求字符串长度

         String s=dwfsdfwfsadf;

         System.out.println(s.length());

charAt():public charAt(int index)//index 是字符下标,返回字符串中指定位置的字符

        String s=Hello;

        System.out.println(s.charAt(3));

getChars():public int getChars()//将字符从此字符串复制到目标字符数组

        String str = "abcdefghikl";

        Char[] ch = new char[8];

        str.getChars(2,5,ch,0);

replace():public int replace()//替换字符串

        String s=\”;

        System.out.printlns.replace(\,///);

        结果///;

toUpperase():public String toUpperCase()//将字符串全部转换成大写

         System.out.println(new String(hello).toUpperCase());

toLowerCse():public String toLowerCase()//将字符串全部转换成小写

         System.out.println(new String(HELLO).toLowerCase());

trim():public String trim()

         String x=ax  c;

         System.out.println(x.trim());//是去两边空格的方法

toCharArray(): String x=abcd;// 将字符串对象中的字符转换为一个字符数组

           char myChar[]=x.toCharArray();

          System.out.println(myChar[1]+myChar[1]);

原文地址:https://www.cnblogs.com/zyldbk/p/4906648.html