Java(易犯错误)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

u1————C.A.R.Hoare,TheEmperor’sOldClothesJavaC++ObjectiveCEiffelSmalltalkMesaLispJavaJavaJavaC++Java10Item1Item2String.equals()==”12tItem3JavaC++JavaItem4JavaItem5JavaItem6JavathisItem7Item8JavaItem9C++JavaJavaItem10Javashort-circuitJava33JavaItem5Item1Item5Item1:u3Java8.4.8.501:classSuper02:{03:staticStringgreeting()04:{05:returnGoodnight;06:}07:08:Stringname()09:{10:returnRichard;11:}12:}01:classSubextendsSuper02:{03:staticStringgreeting()04:{05:returnHello;06:}07:08:Stringname()09:{10:returnDick;11:}12:}01:classTest02:{03:publicstaticvoidmain(String[]args)04:{05:Supers=newSub();06:System.out.println(s.greeting()+,+s.name());07:}08:}TestGoodnight,DickSupergreetingnameSubSupergreetingnameTestmain14tTest5SubsSuperSubsSuperSub(6)s.greeting()s.name()SuperSubname()name()SubSupername()Subname()Supername()sSubs.name()Dickgreeting()SuperSubgreeting()Subgreeting()Supergreeting()sSuperSubgreeting()Supergreeting()s.greeting()Goodnight“”“”Super/SubsSubSubgreeting()SupersSupergreeting()sSubSupername()JavaSub/Super“”“”Item2:String.equals()==u5lllfinalll1C++==stringString.equals(...)==01:publicclassStringExample02:{03:publicstaticvoidmain(Stringargs[])04:{05:Strings0=Programming;06:Strings1=newString(Programming);07:Strings2=Program+ming;08:09:System.out.println(s0.equals(s1):+(s0.equals(s1)));10:System.out.println(s0.equals(s2):+(s0.equals(s2)));11:System.out.println(s0==s1:+(s0==s1));12:System.out.println(s0==s2:+(s0==s2));13:}14:}3String“Programming”“Programming”Stringequals(...)“==”s0.equals(s1):trues0.equals(s2):trues0==s1:falses0==s2:true116tString.equals()equals(...)trues0s1s2true“==”Strings0s1Strings0s2s0s2JavaStringLiterals“Programming”“Program”“ming”1s2Java“Programming”“Program”+“ming”JavaconstantpoolJava.classJVM.classs0s2JVMconstantpoolresolution3JVM5.4nconstantpoolentryCONSTANT_String2UnicodeStringnintern()UnicodeStringStringnStringCONSTANT_StringUnicodeStringJavaStringStringJVM6ProgrammingStrings0s1falses0==s1s0.equals(s1)s0==s1s0.equals(s1)1Java0escapesequence2.classItem2:String.equals()==u7.classJVMintern()StringStringintern()intern()301:importjava.io.*;02:03:publicclassStringExample204:{05:publicstaticvoidmain(Stringargs[])06:{07:StringsFileName=test.txt;08:Strings0=readStringFromFile(sFileName);09:Strings1=readStringFromFile(sFileName);10:11:System.out.println(s0==s1:+(s0==s1));12:System.out.println(s0.equals(s1):+(s0.equals(s1)));13:14:s0.intern();15:s1.intern();16:17:System.out.println(s0==s1:+(s0==s1));18:System.out.println(s0==s1.intern():+19:(s0==s1.intern()));20:}21:22:privatestaticStringreadStringFromFile(StringsFileName)23:{24://…readstringfromfile…25:}26:}s0s1readStringFromFile(...)String9String111218ts0==s1:falses0.equals(s1):trues0==s1:falses0==s1.intern():true14Strings015s1.intern()s01718s0s1Strings0==s1falses1.intern()s0s0==s1.intern()trues1s0nullgarbagecollectors0Strings0s1.intern()s1equalitycomparisonString.equals(...)====intern()nmStringn.equals(m)n.intern()==m.intern()String.intern()JavaJavaprimitivetypeJavastronglytype8buildingblocksJavaJavaJavaJVMbooleanCC++Javabooleanboolean0false0true“”Item3:Javau9Cvalue=get_value();if(value)do_something;Javabooleanvalue=getValue();if(value!=null)doSomething;JavabooleanlossofprecisionJavaarithmeticoperationaccidentaltruncationTruncation10“2.0”1112“2.4”01:publicclassTruncation02:{03:staticvoidprintFloat(floatf)04:{05:System.out.println(f=+f);06:}07:08:publicstaticvoidmain(String[]args)09:{10:printFloat(12/5);//datalost!11:printFloat((float)12/5);12:printFloat(12/5.0f);13:}14:}125integer10integerprintFloatfloatfloatpromotefloat1112110tintbyte1.11.1intlongfloatlongdouble-46publicclassLostPrecision{publicstaticvoidmain(String[]args){intorig=1234567890;floatapprox=orig;introunded=(int)approx;//lostprecisionSystem.out.println(orig-rounded);}}Java5.1.2“”narrowing1.1floatdoubleshortintlongoverflowlongshortcast“”Item3:Javau11byteshortcharintTypeConversion7127byte-128127812801:publicclassTypeConversion02:{03:staticvoidconvertArg(byteb){}04:05:publicstaticvoidmain(String[]args)06:{07:byteb1=127;08://byteb2=128;//won'tcompile09:10://convertArg(127);//won'tcompile11:convertArg((byte)127);12:13:bytec1=b1;14://bytec2=-b1;//won'tcompile15:inti=++b1;//overflow16:System.out.println(i=+i);17:}18:}3Math.pow()doubleint10113intfloatshortdouble112t14byteshortcharint14TypeConversion.java:14:possiblelossofprecisionfound:intrequired:byteb1intintbyte15byteint16“i=128”“i=-128”BugBugBug01:publicclassIntAdder02:{03:privateintx;04:privateinty;05:privateintz;06:07:publicvoidIntAdder()08:{09:x=39;10:y=54;11:z=x+y;12:}13:14:publicvoidprintResults()15:{16:System.out.println(Thevalueof'z'is'+z+');17:}18:19:publicstaticvoidmain(String[]args)20:{21:IntAdderia=newIntAdder();22:ia.printResults();23:}24:}Item4:u13IntAdder3privatexyzprintResultsmain21iaIntAdder7xyz22printResultszIntAdderThevalueof'z'is'93'BugThevalueof'zÉis'0'21IntAdderia7IntAdderxyz7“void”IntAdderIntAdder“void”7IntAdderiaJavaIntAdderpublicIntAdder(){}21iaxyz022printResultsThevalueof'z'is'0'.“”BugIntAdderJava6.8114tJavaJava

1 / 37
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功