请教一下JAVA里面的三目运算符?:的类型问题啊

zapdos 2008-06-16 10:53:21
为什么

char x = ‘X’;
int i = 10;
System.out.println(false ? i : x);
System.out.println(false ? 10 : x);

输出

88
X

我记得低于int型的数据进行运算就都会被提升为int型的啊,难道我搞错了?晕...
...全文
1452 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
老白干 2008-12-09
  • 打赏
  • 举报
回复
4楼有道理!!支持……
yxb0317 2008-06-17
  • 打赏
  • 举报
回复
看变量
szadek 2008-06-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 interpb 的回复:]
10是字面常量 而且在char范围内

所以被当成char

对chat 等的提升只有在进行算术运算时才会发生

这里没有进行算术元算吧

还有你可以把 10 写成 (int)10 10l 或者10d 就会看到不同的结果
[/Quote]
wensheng_zh2007 2008-06-17
  • 打赏
  • 举报
回复
uuuuup
zy2419 2008-06-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 interpb 的回复:]
刚刚去看了 java 规范 找到 了关于 这个运算符的规定


If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the condition…
[/Quote]

很好很强大
zapdos 2008-06-17
  • 打赏
  • 举报
回复
谢了,散分
interpb 2008-06-16
  • 打赏
  • 举报
回复
刚刚去看了 java 规范 找到 了关于 这个运算符的规定


If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.

If one of the operands is of type Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.

If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.

If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.

以上的就是规范原文

大意是 当后两个表达式有一个是 常量表达式(constant expression )时
另外一个是类型是T 时,而 常量表达式 可以被T表示时(representable in type T) 输出结果是T类型


所以 因为 10 是常量 可以被 char 表示 输出结果是char型的


另外还有一些规范详情 可以自己参考java规范

bootupnow 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zapdos 的回复:]
哎,8F有理,(int)10好像也没变化,为什么?...
[/Quote]
还是估计和编译器有关。
zapdos 2008-06-16
  • 打赏
  • 举报
回复
哎,8F有理,(int)10好像也没变化,为什么?...
zapdos 2008-06-16
  • 打赏
  • 举报
回复
噢,是这样啊
?:不算作算术运算,谢了
bootupnow 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 interpb 的回复:]
10是字面常量 而且在char范围内

所以被当成char

对chat 等的提升只有在进行算术运算时才会发生

这里没有进行算术元算吧

还有你可以把 10 写成 (int)10 10l 或者10d 就会看到不同的结果
[/Quote]
写出(int)10 ,貌似没有啥变化,有点奇怪,在JLS找到了答案的同学吼一下啊!!
interpb 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jdlsfl 的回复:]
按第一个出现的变量的类型作为最终的类型
[/Quote]


double x = 122.2d;
int i = 10;
System.out.println(false ? i : x);
System.out.println(false ? 10 : x);

那这样呢
interpb 2008-06-16
  • 打赏
  • 举报
回复
10是字面常量 而且在char范围内

所以被当成char

对chat 等的提升只有在进行算术运算时才会发生

这里没有进行算术元算吧

还有你可以把 10 写成 (int)10 10l 或者10d 就会看到不同的结果
qusic 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jdlsfl 的回复:]
按第一个出现的变量的类型作为最终的类型
[/Quote]
估计是正解~
bootupnow 2008-06-16
  • 打赏
  • 举报
回复
System.out.println(false ? i : x); // i是int(明确),x就转换成int的类型,输出88
System.out.println(false ? 10 : x); // 10其实是一个char的范围之内,所以x按照char输出'X'
System.out.println(false ? 0 : x); // x按照char输出'X' char的有效范围0 - 65535
System.out.println(false ? 65535 : x); // x按照char输出'X' char的有效范围0 - 65535
System.out.println(false ? -1: x); // -1,不在char有效范围只能,x按照转换成int输出

不知道java编程规范里有没有,有空去找找。

bianmazi 2008-06-16
  • 打赏
  • 举报
回复
帮忙顶一下.学习下,偶也没遇到过这种问题.
貌似低于int型的数据运算时会降低为当前操作类型吧.关注中
jdlsfl 2008-06-16
  • 打赏
  • 举报
回复
按第一个出现的变量的类型作为最终的类型
youzy 2008-06-16
  • 打赏
  • 举报
回复
我的理解是:
第一个输出x被提升为int型了,因为i是int类型
而第二输出x是和10比较,10是什么类型是不清楚的,所以就把它转换为与x同类型的了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧