Topic: String s=null 和 String s="null"一样吗?

  Print this page

1.String s=null 和 String s="null"一样吗? Copy to clipboard
Posted by: fgh520
Posted on: 2006-05-09 16:33

String s=null 和 String s="null"一样吗?
还有String s="";
他们三个有区别吗?

还有一个String s=" ";是一个空格。有什么用吗?
谢谢斑竹。

2.Re:String s=null 和 String s="null"一样吗? [Re: fgh520] Copy to clipboard
Posted by: jasonhero
Posted on: 2006-05-09 16:51

三者不同,String s=null;将抛出错误
String s="null";表示为字符串s负值为null
String s=" ";表示为字符串s负值为空

3.Re:String s=null 和 String s="null"一样吗? [Re: fgh520] Copy to clipboard
Posted by: jasonhero
Posted on: 2006-05-09 16:52

Post is deleted

4.Re:String s=null 和 String s="null"一样吗? [Re: jasonhero] Copy to clipboard
Posted by: zcjl
Posted on: 2006-05-09 18:01

答案是:都不一样
先看下面一段代码,有什么看不明白的再问:
String s1 = null;
String s2 = "null";
String s3 = "";
String s4 = " ";
System.out.println("====s1====" + s1 + "====");
System.out.println("====s2====" + s2 + "====");
System.out.println("====s3====" + s3 + "====");
System.out.println("====s4====" + s4 + "====");
System.out.println("s1 == null: " + (s1 == null));
System.out.println("s2 == null: " + (s2 == null));
System.out.println("s1 == s2: " + (s1 == s2));
//System.out.println(s1.equals("null")); //will cause NullPointerException
System.out.println("\"null\".equals(s1): " + ("null".equals(s1)));
System.out.println("s2.equals(\"null\"): " + (s2.equals("null")));
System.out.println("\"null\".equals(s2): " + ("null".equals(s2)));
//System.out.println(s1.equals(s2)); //will cause NullPointerException
System.out.println("s2.equals(s1): " + (s2.equals(s1)));
System.out.println("s3.equals(s2): " + (s3.equals(s2)));
System.out.println("s4.equals(s3): " + (s4.equals(s3)));
System.out.println("s4.trim().equals(s1): " + (s4.trim().equals(s3)));

5.Re:String s=null 和 String s="null"一样吗? [Re: jasonhero] Copy to clipboard
Posted by: zcjl
Posted on: 2006-05-09 18:02

jasonhero wrote:
三者不同,String s=null;将抛出错误
String s="null";表示为字符串s负值为null
String s=" ";表示为字符串s负值为空

String s = null;不会抛出什么错误吧?

6.Re:String s=null 和 String s="null"一样吗? [Re: fgh520] Copy to clipboard
Posted by: fgh520
Posted on: 2006-05-10 10:24

谢谢楼上两位朋友。
I SEE!!!!!!!!!!!

7.Re:String s=null 和 String s="null"一样吗? [Re: fgh520] Copy to clipboard
Posted by: jasonhero
Posted on: 2006-05-10 11:08

不好意思,更正一下:
String s = "";
和String s =" ";输出效果一样,都是 空串
String s = null;
和String s = "null";输出效果一样,都是输出 null

8.Re:String s=null 和 String s="null"一样吗? [Re: jasonhero] Copy to clipboard
Posted by: damondeng
Posted on: 2006-05-10 13:19

我想String s="" 和 String s=" "是不一样的, 在内存里一个是 "\0", 一个是" \0", 简单讲就是一个是完全空的字符串, 一个是有一个字符的字符串(只是恰巧这个字符是空格而已)
他们的输出结果看上去一样, 因为空格输出到屏幕上也看不到.
形象一点说, 比如我有两个钱包, 一个钱包里什么都没有, 一个钱包里有张白纸, 虽然两个钱包对我来说是一样的(里面没钱), 但是客观上它们是有区别的.

而String s=null 和 String s="null"也是不一样的, String s=null表示这个字符串指针是个空指针, 而String s="null"表示这个字符串里有四个字符, 分别是n, u, l, l 这四个字符.
他们输出的结果一样是因为System.out.println()方法会将空的指针打印成"null", 恰好和第二个字符串的值一样.
如果同样拿钱包做比喻, 第一种情况是我根本就没有钱包(这时如果有小偷偷我钱包会报空指针异常的), 第二种情况是我有个钱包, 里面有张纸写着"我没有钱包"

9.Re:String s=null 和 String s="null"一样吗? [Re: damondeng] Copy to clipboard
Posted by: zcjl
Posted on: 2006-05-10 13:53

呵呵,楼上的比喻很形象啊
我在前面的代码中,故意给出
String s1 = null;
String s2 = "null";
System.out.println("====s1====" + s1 + "====");
System.out.println("====s2====" + s2 + "====");

这样看上去容易误解的示例,但是在后面也演示了s1和s2不同之处,尤其是被注释但用红色醒目的那两行

10.Re:String s=null 和 String s="null"一样吗? [Re: fgh520] Copy to clipboard
Posted by: needd
Posted on: 2006-05-10 15:13

String s = null 是可以的
因为 String 型在java中是对象
所以s可以做空引用

class A{}

class Test{
public static void main(String args[]) {
A a = null; 道理和他一样
int i = null; 这样就不行;因为i时基本类型其声明的变量不可为 null;
}
}

11.Re:String s=null 和 String s="null"一样吗? [Re: fgh520] Copy to clipboard
Posted by: jasonhero
Posted on: 2006-05-10 18:07

我想String s="" 和 String s=" "是不一样的, 在内存里一个是 "\0", 一个是" \0", 简单讲就是一个是完全空的字符串, 一个是有一个字符的字符串(只是恰巧这个字符是空格而已)
他们的输出结果看上去一样, 因为空格输出到屏幕上也看不到.
形象一点说, 比如我有两个钱包, 一个钱包里什么都没有, 一个钱包里有张白纸, 虽然两个钱包对我来说是一样的(里面没钱), 但是客观上它们是有区别的.

而String s=null 和 String s="null"也是不一样的, String s=null表示这个字符串指针是个空指针, 而String s="null"表示这个字符串里有四个字符, 分别是n, u, l, l 这四个字符.
他们输出的结果一样是因为System.out.println()方法会将空的指针打印成"null", 恰好和第二个字符串的值一样.
如果同样拿钱包做比喻, 第一种情况是我根本就没有钱包(这时如果有小偷偷我钱包会报空指针异常的), 第二种情况是我有个钱包, 里面有张纸写着"我没有钱包"

服了!!!表达能力
我是知道String s=null;肯定会抛出异常,但是上机一试,输出结果一样,唉!还是知识不扎实啊,服了!!!

12.Re:String s=null 和 String s="null"一样吗? [Re: jasonhero] Copy to clipboard
Posted by: scottlai
Posted on: 2006-05-10 18:54


java.io.PrintStream

public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}

public void print(Object obj) {
write(String.valueOf(obj));
}


java.lang.String

public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}


13.Re:String s=null 和 String s="null"一样吗? [Re: jasonhero] Copy to clipboard
Posted by: sunjavaduke
Posted on: 2006-05-10 19:04

[quote][b]jasonhero wrote:[/b]
三者不同,String s=null;将抛出错误
String s="null";表示为字符串s负值为null
String s=" ";表示为字符串s负值为空
[/quote]
String s=null;并不会抛出什么错误,如果说抛出错误也是当使用s,却不允许s为null值时才会抛出NullPointException。在JAVA DOC中是这样说的:
Thrown when an application attempts to use null in a case where an object is required. These include:
只有当需要一个实例对象而却使用了null时才会抛出这个异常。
Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the null object.
还有这句:String s=" ";表示为字符串s负值为空
应该不是赋值为空吧,而是说将s初始化成一个内容为一个空格的字符串对象

四者确实都不相同,还要参照一下前面几位朋友的解释,应该能够明白吧


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923