String的概述

来源:互联网 发布:九品网络电视好不好用 编辑:程序博客网 时间:2024/04/29 09:21
String字符串:
1、实例化String对象:直接赋值、使用关键字new
例子:。。。public static void main(String[]args){
        //    String str="Hello";
    String str=new String("Hello");
    System.out.println(str);
}
上面的例子中被注释的与没有被注释的有相同的作用,
但是一般推荐第一种方法(被注释的),节省内存。
又一个例子:
public static void main(String[] args);{
    int a=10;
    int b=10;
    System.out.println(a==b);
}结果为:true。(int型不是字符型)
public static void main(String[] args);{
    String str="Hello";
    String str1=new String("Hello");
    //System.out.println(str==str1);
    System.out.println(str.equals(str1));
}被注释的运行结果为:false;没有注释的运行结果是:
true。(这次比较的是字符串)
说明:“==”比较的是地址;“equals”比较的是内容。
0 0