String解析

String的比较有两种==和equals()方法:

==:比较的是在堆内存中地址

equals():比较的是值

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. String s1="hello";  
  2. String s2="hello";  
  3. String s3=new String("hello");  
  4.   
  5. s1==s2;//结果为true  
  6. s1==s3;//结果为false  
  7. s1.equals(s2);//结果为true  
  8. s1.equals(s3);//结果为true  

 

 

原理图:

 

String的内容不可改变:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class StringDemo{  
  2.     public static void main(String args[]){  
  3.         String str = "hello" ;      // 声明字符串  
  4.         str = str + " world!!!" ;   // 修改字符串  
  5.         System.out.println("str = " + str) ;  
  6.     }  
  7. };  

 

 

原理图:

常用方法:

posted @ 2016-12-30 14:48  天涯海角路  阅读(118)  评论(0)    收藏  举报