Java Puzzlers笔记--puzzle 8: Dos Equis 选择操作符问题
2007-03-03 19:15
381 查看
public class DosEquis{
public static void main(String[] args){
char x = 'X';
int i = 0;
System.out.println(true ? x : 0);
System.out.println(false ? i : x);
}
}
Solution:
显示:X88
?: 选择操作符的特别的地方:当选择的数有int类型出现时,char类型要转化为int显示;
TID:
mixed-type computation can be confusing. Nowhere is this more apparent than in
conditional expressions.
use the same type for the second and third operands in conditional expressions.
The rules for determining the result type of a conditional expression are too long
and complex to reproduce in their entirety, but here are three key points:
1. If the second and third operands have the same type, that is the type of the
conditional expression. In other word, you can avoid the whole mess by steering clear of
mixed-type computation.
2. 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, the
type of the conditional expression is T.
3. Otherwise, binary numberic promotion is applied to the operand types, and the
type of the conditional expression is the promoted type of the second and third operands.
public static void main(String[] args){
char x = 'X';
int i = 0;
System.out.println(true ? x : 0);
System.out.println(false ? i : x);
}
}
Solution:
显示:X88
?: 选择操作符的特别的地方:当选择的数有int类型出现时,char类型要转化为int显示;
TID:
mixed-type computation can be confusing. Nowhere is this more apparent than in
conditional expressions.
use the same type for the second and third operands in conditional expressions.
The rules for determining the result type of a conditional expression are too long
and complex to reproduce in their entirety, but here are three key points:
1. If the second and third operands have the same type, that is the type of the
conditional expression. In other word, you can avoid the whole mess by steering clear of
mixed-type computation.
2. 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, the
type of the conditional expression is T.
3. Otherwise, binary numberic promotion is applied to the operand types, and the
type of the conditional expression is the promoted type of the second and third operands.
相关文章推荐
- Java Puzzlers笔记--puzzle 9: Tweedledum +=的问题
- Java Puzzlers笔记--puzzle 10: Tweedledee += 问题(2)
- Java Puzzlers笔记--puzzle 19: Classy Fire 注释问题
- Java Puzzlers笔记--puzzle 13: Animal Farm 优先级以及对象引用问题
- Java Puzzlers笔记--puzzle 17: Huh? 转义字符问题
- Java Puzzlers笔记--Puzzle 2: Time for a change 关于浮点型的问题
- Java Puzzlers笔记--puzzle 14: Escape Rout (")双引号的问题
- Java Puzzlers笔记--puzzle 5: The joy of Hex 十六进制计算问题
- Java Puzzlers笔记--puzzle 15: Hello Whirled -Unicode字符问题
- Java Puzzlers笔记--Puzzle 3: Long Division 溢出问题
- Java Puzzlers笔记--puzzle 7: Swap Meat ^符号问题
- Java Puzzlers笔记--puzzle 21: What's my Class, take 2 路径字符问题
- Java Puzzlers笔记--puzzle 12: ABC String与char的区别
- 读Java Puzzlers笔记: 表达式问题
- 算法笔记_031:计算中值和选择问题(Java)
- Java Puzzlers笔记--Puzzle 1: Oddity奇偶问题
- Java Puzzlers笔记--puzzle 20: What's my Class? String.replaceAll()的使用
- Java Puzzlers笔记--puzzle 6: Multicast 类型转换
- Java学习笔记之接口和抽象类的区别,及选择问题,看完之后就懂了。
- Java Puzzlers笔记--puzzle 22:Dupe of URL url的使用