Why does a null value appear in string output?

后端 未结 4 1167
旧时难觅i
旧时难觅i 2020-12-04 02:49

When I execute the following code the output is \"nullHelloWorld\". How does Java treat null?

import java.util.*;
import java.lang.*;
import java.io.*;

/* N         


        
4条回答
  •  一个人的身影
    2020-12-04 03:11

    my two cent:

        String str = null;
        str = str.concat("Hello World"); // Exception in thread "main" java.lang.NullPointerException
    

    but

    str += "Hello World";
    System.out.println(str); // Hello World
    

提交回复
热议问题