| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1490 人关注过本帖
标题:关于三目运算符的一个小问题
只看楼主 加入收藏
ET_bug
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:黑侠
帖 子:89
专家分:602
注 册:2009-7-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
关于三目运算符的一个小问题
int x=4;
System.out.println("value is "+ ((x>4) ? 99.9 :9));
的输出为什么是value is 9.0而不是9
而把99.9改成99的话,输出就变成value is 9了
三目运算符会自动转型吗
搜索更多相关主题的帖子: 运算符 
2009-07-23 00:24
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:20 
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.

以上的就是规范原文

   (1)假如表达式1和表达式2具有相同的类型,那么整个条件运算符结果的类型就是这个类型。
   (2)假如一个表达式的类型是T,T是byte或short或char,另一个表达式的类型是int类型的常量表达式,而且这个常量表达式的值是可以用类型T表示的(也就是说,常量表达式的值是在类型T的取值范围之内),那么整个条件运算符结果的类型就是T。
    (3)除以上情况外,假如表达式1和表达式2的类型不同,那么将进行类型提升,整个条件运算符结果的类型就是提升后的类型。

[[it] 本帖最后由 lampeter123 于 2009-7-23 08:22 编辑 [/it]]

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2009-07-23 08:10
xiao_ou0725
Rank: 2
来 自:江苏苏州
等 级:论坛游民
帖 子:59
专家分:20
注 册:2008-10-24
收藏
得分:0 
这样啊 还没有发现原来这个操作符还有这个用处啊
2009-07-23 09:35
ET_bug
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:黑侠
帖 子:89
专家分:602
注 册:2009-7-21
收藏
得分:0 
感谢lampeter兄的指点,学到新东西了...

编程之路无止境!
可是小子才入门!
2009-07-23 09:52
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
回帖是一种美德!传说每次回
帖即可获得 10 分可用积分!

学习下三目运算。呵呵

C#超级群 74862681,欢迎大家的到来!
2009-07-23 17:55
hwpayg
Rank: 2
等 级:论坛游民
帖 子:108
专家分:25
注 册:2008-4-3
收藏
得分:0 
很多偏门的东西可以去看下Java解惑
2009-07-25 07:41
michael_cc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:105
注 册:2009-8-24
收藏
得分:0 
又学到东西了,感谢感谢lampeter兄的指点。
2009-08-25 10:43
快速回复:关于三目运算符的一个小问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015490 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved