几个陷阱题Word下载.docx
《几个陷阱题Word下载.docx》由会员分享,可在线阅读,更多相关《几个陷阱题Word下载.docx(28页珍藏版)》请在冰点文库上搜索。
★★★★★
NO.3长整除
publicclassLongDivision{
finallongMICROS_PER_DAY=24l*60*60*1000*1000;
//微秒
finallongMILLIS_PER_DAY=24*60*60*1000;
//毫秒
System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);
NO.4互换内容
publicclassSwap{
intx=1984;
inty=2001;
x^=y^=x^=y;
System.out.println("
x="
+x+"
;
y="
+y);
陷阱等级:
★★☆☆☆
NO.5字符串和字符
publicclassCharAndString{
H"
+"
a"
);
System.out.println('
H'
+'
a'
publicclassCharAndString{
publicstaticvoidmain(String[]args){
System.out.println("
System.out.println('
}
}
NO.6字符数组
publicclassCharArray{
Stringletters="
ABC"
char[]numbers={'
1'
'
2'
3'
};
System.out.print(numbers);
System.out.print(letters+"
easyas"
+numbers);
publicclassCharArray{
Stringletters="
char[]numbers={'
System.out.print(letters+"
★★★★☆
NO.7转义字符
publicclassUnicodeTest{
a\u0022.length()+\u0022b"
.length());
publicclassUnicodeTest{
}
}陷阱等级:
NO.8打印输出类名
publicclassMyClass{
System.out.println(MyClass.class.getName().replaceAll("
."
"
/"
)+"
.class"
publicclassMyClass{
NO.9随机数的问题
importjava.util.Random;
publicclassRandomTest{
privatestaticRandomrnd=newRandom();
StringBufferword=null;
switch(rnd.nextInt
(2)){
case1:
word=newStringBuffer("
P"
case2:
G"
default:
M"
word.append('
i'
n'
System.out.println(word);
publicclassRandomTest{
NO.10无情的增量操作
publicclassForTest{
intj=0;
for(inti=0;
i<
100;
i++){
j=j++;
System.out.println(j);
publicclassForTest{
NO.11整数边界的问题
publicclassWhileTest{
publicstaticfinalintEND=Integer.MAX_VALUE;
publicstaticfinalintSTART=END-100;
intcount=0;
for(inti=START;
=END;
i++)
count++;
System.out.println(count);
publicclassWhileTest{
intcount=0;
for(inti=START;
count++;
System.out.println(count);
NO.12计数器的问题
publicclassClock{
intminutes=0;
for(intms=0;
ms<
60*60*1000;
ms++)
if(ms%60*1000==0)
minutes++;
System.out.println(minutes);
publicclassClock{
NO.13优柔寡断的返回值
publicclassReturnValue{
System.out.println(decision());
publicstaticbooleandecision(){
try{
returntrue;
}finally{
returnfalse;
publicclassReturnValue{
System.out.println(decision());
NO.14你好,再见
publicclassGoodBye{
Helloworld"
System.exit(0);
Goodbyeworld"
publicclassGoodBye{
NO.15到底关闭了吗
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
publicclassIOTest{
publicstaticvoidcopy(Stringsrc,Stringdest)throwsIOException{
InputStreamin=null;
OutputStreamout=null;
in=newFileInputStream(src);
out=newFileOutputStream(dest);
byte[]buf=newbyte[1024];
intn;
while((n=in.read(buf))>
0)
out.write(buf,0,n);
if(in!
=null)in.close();
if(out!
=null)out.close();
publicclassIOTest{
5、无限循环
viewplaincopytoclipboardprint?
publicstaticfinalintEND=Integer.MAX_VALUE;
publicstaticfinalintSTART=END-100;
publicstaticvoidmain(String[]args){
intcount=0;
for(inti=START;
count++;
System.out.println(count);
}
上面程序运行的结果是什么?
A:
无限循环。
将i<
=END改成i<
END?
为什么呢?
你知道的,呵呵!
6、计数器问题
intminutes=0;
for(intms=0;
if(ms%60*1000==0)
minutes++;
System.out.println(minutes);
结果跟你想的一样么?
A:
呵呵,括号问题,不多说!
7、到底返回什么?
publicstaticbooleandecision(){
try{
returntrue;
}finally{
returnfalse;
true?
false?
一般情况下,不管怎么说try/catch代码块中,finally总是最后被执行的。
8、错误里聚集遍历
Vectorv=newVector();
v.add("
one"
two"
three"
four"
Enumerationenume=v.elements();
while(enume.hasMoreElements()){
Strings=(String)enume.nextElement();
if(s.equals("
))
v.remove("
else{
System.out.println(s);
System.out.println("
What'
sreallythere..."
enume=v.elements();
运行代码看看结果跟你想的一样么?
一般不建议在遍历聚集的时候对聚集进行操作。
为什么结果是这样呢?
看JDK源码能得到答案。
Enumeration没有实现FailFast操作,如果换成ArrayList,上面的代码可能会出错。
《java与模式》迭代子(iterator)介绍了。
publicStringsubstring(intbeginIndex,intendIndex){
if(beginIndex<
0){
thrownewStringIndexOutOfBoundsException(beginIndex);
if(endIndex>
count){
thrownewStringIndexOutOfBoundsException(endIndex);
if(beginIndex>
endIndex){
thrownewStringIndexOutOfBoundsException(endI