Java Quiz(一)
我准备发一些Java的小Quiz,这些题目看似比较简单,但如果一不小心,就会得出错误的结论,大家小心咯。
如果你有什么意见或者建议,请留言或者加我的QQ:472429029,验证信息写Java,javaeye什么的都行。
一。请问下面的代码的运行结果是?
/**
* created on 2009-4-13
* Author softkid
*/
package cn.softkid;
public class MulOrAdd {
public static void main(String[] args) {
int x=5,y=5;
x *= 5+2;
y = y*5+2;
System.out.println("Value of X is : "+x);
System.out.println("Value of Y is : "+y);
}
}
怎么样?简单吧!请留言给出你的结果吧!然后编译运行试试,看运行生成的结果是否和你想的一样?
二。请问下面的代码的运行结果是?
/**
* created on 2009-4-13
* Author softkid
*/
package cn.softkid;
public class MyDouble {
public static void main(String[] args) {
for(double d = 0.0; d != 1.0; d = d + 0.1){
System.out.println("Tesging...");
}
}
}
两道题目下来,你是否觉得我的题目很幼稚呢?如果有兴趣,请继续吧!
三。下面一个是关于循环的,请问运行的结果是?
/**
* created on 2009-4-13
* Author softkid
*/
package cn.softkid;
public class MyLoop {
public static void main(String[] args) {
for(int i=0;i<10;i++){
for(i=0;i<5;i++){
System.out.println("Testing");
}
}
}
}
这一个应该没什么问题?但如果被前面的搞迷糊了,那就不一定了!
四。接下来扯到Integer和String了,请问下面代码运行的结果是?
/**
* created on 2009-4-13
* Author softkid
*/
package cn.softkid;
public class Equality {
public static void main(String args[]) {
Integer first = 100;
Integer second = 100;
String str1 = "abc";
String str2 = "abc";
String str3 = new String("abc");
String str4 = new String("abc");
if (first == second)
System.out.println("first == second");
else
System.out.println("first != second");
if (str1 == str2)
System.out.println("str1 == str2");
else
System.out.println("str1 != str2");
if (str1 .equals(str3))
System.out.println("str1 equals str3");
else
System.out.println("str1 not equals str3");
if (str3 == str4)
System.out.println("str3 == str4");
else
System.out.println("str3 != str4");
}
}
这一次就到这里吧,下次再继续!
1 楼
wthwth
2009-04-13
1
x *= (5+2);
y = (y*5+2);
2
浮点数不可以用==比较
3
两个i是同一个变量
4
first == second 值不同
str1 == str2 地址相同,初始化时自动优化的?
str1 equals str3 值相同
str3 != str4 地址不同
2 楼
javacto
2009-04-14
1.
x* = (5+2) 结果是:35
y = y*5+2 = 27
2.
double 0.0~1.0 无穷数啊
3.
for(int i=0;i<10;i++){
for(i=0;i<5;i++){}
} 两个i是同一个变量,死循环
4.
first == second 指向相同
str1 == str2 "abc"被共用
str1 equals str3 值相同
str3!=str4 地址不同
3 楼
softkid
2009-04-14
wthwth 写道
str1 == str2 地址相同,初始化时自动优化的?
java内存有个常量池,执行String str1="abc";时,jvm先检查常量池中是否有字符串常量"abc",如果没有
则在常量池中生成之。再执行String str2="abc";时,jvm检查到了常量池中的"abc"字符串常量,然后将str2指向
该字符串常量。
4 楼
shaobin0604
2009-05-24
Value of X is : 35
Value of Y is : 27
死循环
死循环
first == second
str1 == str2
str1 equals str3
str3 != str4
5 楼
softkid
2009-05-24
shaobin0604 写道
Value of X is : 35
Value of Y is : 27
死循环
死循环
first == second
str1 == str2
str1 equals str3
str3 != str4
正解