当字符串与null连接时会发生什么?(What happens when string is concatenated with null? [duplicate])

这个问题在这里已有答案:

在Java 5答案中 连接空字符串

null运算符在字符串中的作用是什么,在串联时输出为null + string为什么? prg是。

public static void main2() { String s1=null; String s2=""; System.out.println(s1); System.out.println(s2); s1=s1+"jay"; s2=s2+"jay"; System.out.println(s1); System.out.println(s2); }

这里发生了什么事?

This question already has an answer here:

Concatenating null strings in Java [duplicate] 5 answers

What's the work of null operator in string, on concatenation the output is null+string why? the prg is.

public static void main2() { String s1=null; String s2=""; System.out.println(s1); System.out.println(s2); s1=s1+"jay"; s2=s2+"jay"; System.out.println(s1); System.out.println(s2); }

what's happening here?

最满意答案

null不是运算符。 null是表示null引用的文字,不引用任何对象。 null是引用类型变量的默认值。 这意味着字符串变量或您的另一个对象类型变量不指向内存中的任何位置。

当你用另一个字符串连接它时。 它将添加到该字符串。 为什么? 因为如果引用为null ,则将其转换为字符串"null" 。

String s1=null; String s2=""; System.out.println(s1); System.out.println(s2); s1=s1+"jay"; s2=s2+"jay"; // compiler convert these lines like this, // s1 = (new StringBuilder()).append((String)null).append("jay").toString(); // s2 = (new StringBuilder()).append((String)"").append("jay").toString(); System.out.println(s1); System.out.println(s2);

它将打印nulljay

null is not an operator. null is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. It's mean that the string variable or your another object type variable is not pointing to anywhere in the memory.

And when you concatenate it with another string. It will added to that string. why? because If the reference is null, it is converted to the string "null".

String s1=null; String s2=""; System.out.println(s1); System.out.println(s2); s1=s1+"jay"; s2=s2+"jay"; // compiler convert these lines like this, // s1 = (new StringBuilder()).append((String)null).append("jay").toString(); // s2 = (new StringBuilder()).append((String)"").append("jay").toString(); System.out.println(s1); System.out.println(s2);

It will print nulljay

当字符串与null连接时会发生什么?(What happens when string is concatenated with null? [duplicate])

这个问题在这里已有答案:

在Java 5答案中 连接空字符串

null运算符在字符串中的作用是什么,在串联时输出为null + string为什么? prg是。

public static void main2() { String s1=null; String s2=""; System.out.println(s1); System.out.println(s2); s1=s1+"jay"; s2=s2+"jay"; System.out.println(s1); System.out.println(s2); }

这里发生了什么事?

This question already has an answer here:

Concatenating null strings in Java [duplicate] 5 answers

What's the work of null operator in string, on concatenation the output is null+string why? the prg is.

public static void main2() { String s1=null; String s2=""; System.out.println(s1); System.out.println(s2); s1=s1+"jay"; s2=s2+"jay"; System.out.println(s1); System.out.println(s2); }

what's happening here?

最满意答案

null不是运算符。 null是表示null引用的文字,不引用任何对象。 null是引用类型变量的默认值。 这意味着字符串变量或您的另一个对象类型变量不指向内存中的任何位置。

当你用另一个字符串连接它时。 它将添加到该字符串。 为什么? 因为如果引用为null ,则将其转换为字符串"null" 。

String s1=null; String s2=""; System.out.println(s1); System.out.println(s2); s1=s1+"jay"; s2=s2+"jay"; // compiler convert these lines like this, // s1 = (new StringBuilder()).append((String)null).append("jay").toString(); // s2 = (new StringBuilder()).append((String)"").append("jay").toString(); System.out.println(s1); System.out.println(s2);

它将打印nulljay

null is not an operator. null is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. It's mean that the string variable or your another object type variable is not pointing to anywhere in the memory.

And when you concatenate it with another string. It will added to that string. why? because If the reference is null, it is converted to the string "null".

String s1=null; String s2=""; System.out.println(s1); System.out.println(s2); s1=s1+"jay"; s2=s2+"jay"; // compiler convert these lines like this, // s1 = (new StringBuilder()).append((String)null).append("jay").toString(); // s2 = (new StringBuilder()).append((String)"").append("jay").toString(); System.out.println(s1); System.out.println(s2);

It will print nulljay