java中null+““和null+null会等于什么鬼呢?

小灰灰 2022-11-06 03:59 349阅读 0赞

在java中,null+””会等于什么鬼?

null+””=?

null+null又会等于什么鬼呢?

null+null=?

直接上代码和截图。

注意:看如下代码,看看null+null等于什么?

  1. System.out.println(null + null);//报错,连编译都通不过
  2. String n = null;
  3. String m = null;
  4. System.out.println(n + m);//nullnull

2021030801105677.png

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N6aDUwMA_size_16_color_FFFFFF_t_70

以下代码,大家也可以亲自去测试一下,大家自己多动手去写,印象会更深刻。

  1. public static void main(String[] args) {
  2. //null只能赋值给引用数据类型的变量,不能赋值给基本数据类型的变量
  3. //下面一行代码语法报错,连编译都通不过,因为得益于Eclipse的强大,Eclipse会帮助我们智能的检查语法
  4. // int a = null;
  5. Integer b = null;
  6. //下面一行代码欺骗过了Eclipse的语法检查,变相的把null赋值给了基本数据类型的变量
  7. //int c = b;//报错NullPointerException(是在程序运行时报的错,但是编译可以通过,即编译时不会报错,运行时才会报错)
  8. System.out.println(b); //null
  9. // System.out.println(c);
  10. Integer d = new Integer(97);
  11. String str1 = null;
  12. System.out.println(str1);//null
  13. System.out.println(str1 + "");//null
  14. String str2 = null;
  15. System.out.println((str2 + "").length());//4
  16. String str3 = null;
  17. System.out.println(str3 + 567);//null567
  18. String str4 = null;
  19. System.out.println((str4 + 567).length());//7
  20. String str5 = null;
  21. String str6 = null;
  22. System.out.println(str5 == null);//true
  23. System.out.println(str6 == null);//true
  24. String str7 = str5 + str6;
  25. System.out.println(str7);//nullnull
  26. System.out.println(str7 == null);//false
  27. System.out.println(str7.length());//8
  28. String str8 = null;
  29. System.out.println(str8 + 666);//null666
  30. System.out.println(str8 + 999.88);//null999.88
  31. System.out.println(str8 + b);//nullnull
  32. System.out.println(str8 + true);//nulltrue
  33. System.out.println(str8 + false);//nullfalse
  34. System.out.println(str8 + d);//null97
  35. System.out.println(str8 + 'a');//nulla
  36. System.out.println("**************");
  37. String s1 = null;
  38. System.out.println("".equals(s1));//false
  39. //报错java.lang.NullPointerException
  40. // System.out.println(s1.equals(""));
  41. System.out.println("**************");
  42. String s2 = null;
  43. System.out.println("null".equals(s2));//false
  44. System.out.println("".equals(s2));//false
  45. System.out.println("null".equals((String)s2));//false
  46. System.out.println(((String)s2) == null);//true
  47. System.out.println(s2 == null);//true
  48. System.out.println("**************");
  49. String s3 = "";
  50. String s4 = "null";
  51. System.out.println(s3.equals(null));//false
  52. System.out.println(s3.equals((String)null));//false
  53. System.out.println(s4.equals(null));//false
  54. System.out.println(s4.equals((String)null));//false
  55. System.out.println("".equals(null));//false
  56. System.out.println("".equals((String)null));//false
  57. String str9 = "江西省赣州市于都县";
  58. System.out.println(str9 + 6666);//江西省赣州市于都县6666
  59. System.out.println("==============");
  60. String s5 = null;
  61. String s6 = null;
  62. System.out.println(s5 == s6);//true
  63. System.out.println(((String)s5) == ((String)s6));//true
  64. System.out.println("##################");
  65. // System.out.println(null); //语法报错
  66. System.out.println((String)null);//null
  67. //下面一行代码会报异常,java.lang.NullPointerException
  68. System.out.println(((String)null).length());
  69. }

运行结果如下:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N6aDUwMA_size_16_color_FFFFFF_t_70 1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N6aDUwMA_size_16_color_FFFFFF_t_70 2

发表评论

表情:
评论列表 (有 0 条评论,349人围观)

还没有评论,来说两句吧...

相关阅读