在Java中扩展原始转换 .这里给出一个小的代码片段尝试猜测输出
public class Test { public static void main(String[] args) { System.out.print("Y" + "O"); System.out.print(‘L‘ + ‘O‘); } }
乍一看,我们预计印刷“YOLO”。
实际输出:
“YO155”。
说明:
当我们使用双引号时,文本被视为一个字符串并打印“YO”,但当我们使用单引号时,字符‘L‘和‘O‘被转换为int。这被称为拓宽原始转换。转换为整数后,将添加数字(‘L‘为76,‘O‘为79),打印155。
现在尝试猜测下面的输出:
public class Test { public static void main(String[] args) { System.out.print("Y" + "O"); System.out.print(‘L‘); System.out.print(‘O‘); } }
输出:??YOLO
说明:现在将打印“YOLO”而不是“YO7679”。这是因为只有当‘+‘运算符存在时才会扩展原始转换。
应用扩展原始转换来转换以下规则中指定的一个或两个操作数。添加Java字符,短语或字节的结果是int:
原文:https://www.cnblogs.com/breakyizhan/p/13286386.html