Java字符串String详解

来源:互联网 发布:js 种修改input的宽度 编辑:程序博客网 时间:2024/06/06 10:54

Java字符串String


1.实例化String对象

  直接赋值

  使用关键字new

String str1="Hello";String str2=new String("hello"); //在内存中有两个hello

2.String内容的比较


int a=10;int b=10;System.out.println(a==b);String str1="Hello";String str2=new String("Hello");System.out.println(str1==str2);System.out.println(str1.equals(str2));


输出:

true
false
true


"=="比较的是地址

equals比较的是内容


3.字符串的内容不可更改

String str="hello";  //String str="hello"String str=str+" world";  //"world"要开辟堆内存System.out.println(str);  //str+"world"

str=str+"world";  改变的是堆内存地址的指向。

















原创粉丝点击