数据运算中关于字符串""的拼接问题

  例子中准备了3种类型数据,分别针对是否在运算存在空字符串参与运算进行了演示,结果如下:

 1         int x = 10;
 2         double y = 20.2;
 3         long z = 10L;
 4         System.out.println(x + y); // 30.2
 5         System.out.println(x + y * z); // 212.0
 6         System.out.println("" + x + y * z); // 10202.0
 7         System.out.println("" + y * z + x); // 202.010
 8         System.out.println(x + y * z + ""); //212.0
 9         System.out.println("" + x + y); // 1020.2
10         System.out.println("" + (x + y * z)); // 212.0    

结论:(在未添加括号情况下)

  空字符串""参与数据运算时:若""在数据运算的表达式中出现(除末尾),其后面的数据运算直接进行简单拼接实现(如:6、7、8、9行)。

原文地址:https://www.cnblogs.com/moreforests/p/13221582.html