第一道:
class Check4
{
public static void main(String args[])
{
int x=0;
x=x++;
System.out.println(x);
int y=x;
System.out.println(y);
}
}
运行结果是0,0。为什么呢?请问x++ 什么时候才执行啊?难道一直不执行吗?
第二道:public class Check2
{
public static void main(String args[])
{
int x=4;
System.out.println("value is " +((x>4)?99.9:9));
}
}
运行结果是9.0而不是9,是不是因为99.9是浮点数的原因呢?
class Check4
{
public static void main(String args[])
{
int x=0;
x=x++;
System.out.println(x);
int y=x;
System.out.println(y);
}
}
运行结果是0,0。为什么呢?请问x++ 什么时候才执行啊?难道一直不执行吗?
第二道:public class Check2
{
public static void main(String args[])
{
int x=4;
System.out.println("value is " +((x>4)?99.9:9));
}
}
运行结果是9.0而不是9,是不是因为99.9是浮点数的原因呢?
java.util.Date date = (x > 4) ? new java.util.Date() : new java.sql.Date();看到报错信息,你就知道了
int x = 4;
java.util.Date date = (x > 4) ? new A() : new B();
}
}class A extends java.util.Date {}
class B extends java.util.Date {}jdk1.4编译错误
Test.java:5: incompatible types for ?: neither is a subtype of the other
second operand: A
third operand : B
java.util.Date date = (x > 4) ? new A() : new B();
^
1 error
2)在一个运算表达式中,不过相互运算的数据类型不一样,父类型会向子类型转换后再参加运算,如果转换不成功就会出现错误。所以,我们用不同的数据类型进行运算时都人为的进行强制转换(当然要求时可行的),如int x;float f;double d=double(x)+double(f);
x=x++;
把x传给x(此时x传的值是0,然后x加1,这时候原来的x已经没有引用了,所以白加
第二题:
(x>4)?99.9:9
boolean-exp?value0:value1
这个一个三元操作符,它结果的类型是value中最大的类型
比如 inta,double b,a+b的类型自动是double的,如果想要int型的就要强制转型
int c = (int)(a+b)
此题在scjp的练习中经常出现=。=
x=x++相当于
x=x+1;
x=0
我试验了一下可以运行;
DiskClusters d1 =new DiskClusters();
DiskClusters d2 = null;
DiskClusters d=(r>0)?d1:d2;
第二题,在JAVA中选择运算符,运行的结果是级别最高的数据类型。我想应该是这样的吗?
int x = 4;
java.util.Date date = (x > 4) ? new A() : new B();
}
}class A extends java.util.Date {}
class B extends java.util.Date {}jdk1.4编译错误
Test.java:5: incompatible types for ?: neither is a subtype of the other
second operand: A
third operand : B
java.util.Date date = (x > 4) ? new A() : new B();
^
1 error
==============================
这个是什么意思啊?哪个高手解释下!
不要想当然
结果是0,0 我刚才验证过了。
x++是马上执行了。我们可以把x++当成如下的函数
INTPP(int x){
int result;
result=x;
x=x+1;
return result;
}x=INTPP(x);
也就是
int result;
result=x;
x=x+1; x=result;
第一题:
x=x++;
把x传给x(此时x传的值是0,然后x加1,这时候原来的x已经没有引用了,所以白加请问这里的引用,指的是什么???