class Test { 
public static void main(String[] args) throws IOException{ 
int x=4;
System.out.println("Vlaue is "+((x>4)?99.9:9));
}

为什么输出的是9.0而不是9呢?

解决方案 »

  1.   

    因为Java系统默认的值的类型是double型的
      

  2.   

    系统默认的值的类型好像是int吧???
      

  3.   

    (byte,short,char,int,long,float,double)op double-->double
    当存在double型的数据参与运算时,其他所有的数据都会自动转换为double型的数据类型,并把运算结果转换为double型的。
      

  4.   

    支持.......
    不过byte 不太清楚.
      

  5.   

    实际是表达式的类型。在一个表达式中如果有多个数据类型,自动转换为高的数据类型。表达式(x>4)?99.9:9中有整型,也有实型,则表达式类型为实型。如果写(x>4)?99:9,则表达式类型为整型。
      

  6.   

    3楼正解,有double型数据参与运算时,int就会自动上转为double类型的值
      

  7.   

    看看
    http://topic.csdn.net/u/20080407/09/dabcc399-4460-47ef-966f-26bcb800bd39.html
    109 楼的回复。另外参考:
    http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25
    http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.6.2
      

  8.   

    因为(x>4)?99.9:9中间有99.9,java计算表达式的时候会向上转型
      

  9.   

    当存在double型的数据参与运算时,其他所有的数据都会自动转换为double型的数据类型,并把运算结果转换为double型的。