|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
最初被命名为Oak,目标设定在家用电器等小型系统的编程语言,来解决诸如电视机、电话、闹钟、烤面包机等家用电器的控制和通讯问题。5)OperatorsandAssignments
Objective1)
Determinetheresultofapplyinganyoperatorincludingassignmentoperatorsandinstanceoftooperandsofanytypeclassscopeoraccessibilityoranycombinationofthese.
1.Unaryoperators.
1.1IncrementandDecrementoperators++--
Wehavepostfixandprefixnotation.Inpost-fixnotationvalueofthevariable/expressionismodifiedafterthevalueistakenfortheexecutionofstatement.Inprefixnotation,valueofthevariable/expressionismodifiedbeforethevalueistakenfortheexecutionofstatement.
x=5;y=0;y=x++;Resultwillbex=6,y=5
x=5;y=0;y=++x;Resultwillbex=6,y=6
Implicitnarrowingconversionisdone,whenappliedtobyte,shortorchar.
1.2Unaryminusandunaryplus+-
+hasnoeffectthantostresspositivity.
-negatesanexpression’svalue.(2’scomplementforintegralexpressions)
1.3Negation!
Invertsthevalueofabooleanexpression.
1.4Complement~
Invertsthebitpatternofanintegralexpression.(1’scomplementC0sto1sand1sto0s)
(~i=-i-1)
Cannotbeappliedtonon-integraltypes.
1.5Cast()
Persuadescompilertoallowcertainassignments.Extensivecheckingisdoneatcompileandruntimetoensuretype-safety.
2.Arithmeticoperators-*,/,%,+,-
・Canbeappliedtoallnumerictypes.
・Canbeappliedtoonlythenumerictypes,except‘+’CitcanbeappliedtoStringsaswell.
・Allarithmeticoperationsaredoneatleastwith‘int’.(Iftypesaresmaller,promotionhappens.Resultwillbeofatypeatleastaswideasthewidetypeofoperands)
・Accuracyislostsilentlywhenarithmeticoverflow/erroroccurs.Resultisanonsensevalue.
・Integerdivisionor%byzerothrowsArithmeticException.
・Floatingpointarithmeticalwayslosesprecision.Thefollowingcodefragmentreturnsfalse.
floatf=1.0F/3.0F;
if(A*3.0F==1.0F)returntrue;
elsereturnfalse;
・%-signoftheresultentirelydeterminedbysignofLHS
・FloatingpointcalculationscanproduceNaN(squarerootofanegativeno.or%byzero)orInfinity/-Infinity(divisionbyzero).FloatandDoublewrapperclasseshavenamedconstantsforNaNandinfinities.
・NaN’sarenon-ordinalforcomparisons.X==Float.NaNwon’twork.UseFloat.IsNaN(x)andDouble.IsNaN(x).Butequalsmethodonwrapperobjects(DoubleorFloat)withNaNvaluescomparesNan’scorrectly.
・Infinitiesareordinal.X==Double.POSITIVE_INFINITYwillgiveexpectedresult.
・+alsoperformsStringconcatenation(whenanyoperandinanexpressionisaString).Thelanguageitselfoverloadsthisoperator.toString()methodofnon-Stringobjectoperandsarecalledtoperformconcatenation.Incaseofprimitives,awrapperobjectiscreatedwiththeprimitivevalueandtoStringmethodofthatobjectiscalled.(“Vel”+3willwork.)
・Beawareofassociativitywhenmultipleoperandsareinvolved.
System.out.println(1+2+“3”);//Prints33
System.out.println(“1”+2+3);//Prints123
3.Shiftoperators-<<,>>,>>>
・<<performsasignedleftshift.0bitsarebroughtinfromtheright.Signbit(MSB)ispreserved.(?)Valuebecomesoldvalue*2^xwherexisnoofbitsshifted.
・>>performsasignedrightshift.Signbitisbroughtinfromtheleft.(0ifpositive,1ifnegative.Valuebecomesoldvalue/2^xwherexisnoofbitsshifted.Alsocalledarithmeticrightshift.
・>>>performsanunsignedlogicalrightshift.0bitsarebroughtinfromtheleft.ThisoperatorexistssinceJavadoesn’tprovideanunsigneddatatype(exceptchar).>>>changesthesignofanegativenumbertobepositive.Sodon’tuseitwithnegativenumbers,ifyouwanttopreservethesign.Alsodon’tuseitwithtypessmallerthanint.(Sincetypessmallerthanintarepromotedtoanintbeforeanyshiftoperationandtheresultiscastdownagain,sotheendresultisunpredictable.)
・Shiftoperatorscanbeappliedtoonlyintegraltypes.
・-1>>1isC1,not0.
・1<<31willbecometheminimumvaluethatanintcanrepresent.
・Negativenumbersarerepresentedintwo’scomplementnotation.(Takeone’scomplementandadd1togettwo’scomplement)
・Shiftoperatorsnevershiftmorethanthenumberofbitsthetypeofresultcanhave.(i.e.int32,long64)RHSoperandisreducedtoRHS%xwherexisnoofbitsintypeofresult.
intx;
x==x>>32//ture
x=x>>33;//Hereactuallywhathappensisx>>1
・ifrightsideisnegative,forint,uselow5bits;forlong,uselow6bits.
4.ComparisonoperatorsCallreturnbooleantype.
4.1Ordinalcomparisons-<,<=,>,>=
・Onlyoperateonnumerictypes.Testtherelativevalueofthenumericoperands.
・Arithmeticpromotionsapply.charcanbecomparedtofloat.
4.2ObjecttypecomparisonCinstanceof
・Teststheclassofanobjectatruntime.Checkingisdoneatcompileandruntimesameasthecastoperator.
・ReturnstrueiftheobjectdenotedbyLHSreferencecanbecasttoRHStype.
・LHSshouldbeanobjectreferenceexpression,variableoranarrayreference.
・RHSshouldbeaclass(abstractclassesarefine),aninterfaceoranarraytype,castabletoLHSobjectreference.CompilererrorifLHS&RHSareunrelated.
・Can’tusejava.lang.ClassoritsStringnameasRHS.(?)
・ReturnstrueifLHSisaclassorsubclassofRHSclass
・ReturnstrueifLHSimplementsRHSinterface.
・ReturnstrueifLHSisanarrayreferenceandoftypeRHS.
・xinstanceofComponent[]Clegal.
・xinstanceof[]Cillegal.Can’ttestfor‘anyarrayofanytype’
・ReturnsfalseifLHSisnull,noexceptionsarethrown.
・IfxinstanceofYisnotallowedbycompiler,thenYy=(Y)xisnotavalidcastexpression.IfxinstanceofYisallowedandreturnsfalse,theabovecastisvalidbutthrowsaClassCastExceptionatruntime.IfxinstanceofYreturnstrue,theabovecastisvalidandrunsfine.
4.3Equalitycomparisons-==,!=
・Forprimitivesit’sastraightforwardvaluecomparison.(promotionsapply)
・Forobjectreferences,thisdoesn’tmakemuchsense.Useequalsmethodformeaningfulcomparisons.(Makesurethattheclassimplementsequalsinameaningfulway,likeforX.equals(Y)tobetrue,YinstanceofXmustbetrueaswell)
・ForStringliterals,==willreturntrue,thisisbecauseofcompileroptimization.
5.Bit-wiseoperators-&,^,|
・Operateonnumericandbooleanoperands.
・&-ANDoperator,bothbitsmustbe1toproduce1.
・|-ORoperator,anyonebitcanbe1toproduce1.
・^-XORoperator,anyonebitcanbe1,butnotboth,toproduce1.
・Incaseofbooleanstrueis1,falseis0.
・Order:AND,XOR,OR.
・Can’tcastanyothertypetoboolean.
6.Short-circuitlogicaloperators-&&,||
・Operateonlyonbooleantypes.
・RHSmightnotbeevaluated(hencethenameshort-circuit),iftheresultcanbedeterminedonlybylookingatLHS.
・false&&Xisalwaysfalse.
・true||Xisalwaystrue.
・RHSisevaluatedonlyiftheresultisnotcertainfromtheLHS.
7.Ternaryoperator
・Formata=x?b:c;
・xshouldbeabooleanexpression.
・Basedonx,eitherborcisevaluated.Bothareneverevaluated.
・bwillbeassignedtoaifxistrue,elsecisassignedtoa.
・bandcshouldbeassignmentcompatibletoa.
・bandcaremadeidenticalduringtheoperationaccordingtopromotions.
8.Assignmentoperators.
・Simpleassignment=.
・op=calculateandassignoperators(extendedassignmentoperators)
・*=,/=,%=,+=,-=
・x+=ymeansx=x+y.Butxisevaluatedonlyonce.Beaware.
・Assignmentofreferencevariablescopiesthereferencevalue,nottheobjectbody.
・Assignmenthasvalue,valueofLHSafterassignment.Soa=b=c=0islegal.c=0isexecutedfirst,andthevalueoftheassignment(0)assignedtob,thenthevalueofthatassignment(again0)isassignedtoa.
・Extendedassignmentoperatorsdoanimplicitcast.(Usefulwhenappliedtobyte,shortorchar)
byteb=10;
b=b+10;//Won’tcompile,explicitcastreqdsincetheexpressionevaluatestoanint
b+=10;//OK,+=doesanimplicitcastfrominttobyte
9.General
・InJava,Nooverfloworunderflowofintegershappens.i.e.Thevalueswraparound.Adding1tothemaximumintvalueresultsintheminimumvalue.
・Alwayskeepinmindthatoperandsareevaluatedfromlefttoright,andtheoperationsareexecutedintheorderofprecedenceandassociativity.
・UnaryPostfixoperatorsandallbinaryoperators(exceptassignmentoperators)havelefttorightassoiciativity.
・Allunaryoperators(exceptpostfixoperators),assignmentoperators,ternaryoperator,objectcreationandcastoperatorshaverighttoleftassoiciativity.
・Inspectthefollowingcode.
publicclassPrecedence{
finalpublicstaticvoidmain(Stringargs[]){
inti=0;
i=i++;
i=i++;
i=i++;
System.out.println(i);//prints0,since=operatorhasthelowestprecedence.
intarray[]=newint[5];
intindex=0;
array[index]=index=3;//1stelementgetassignedto3,notthe4thelement
for(intc=0;c<array.length;c++)
System.out.println(array[c]);
System.out.println("indexis"+index);//prints3
}
}
TypeofOperatorsOperatorsAssociativity
Postfixoperators[].(parameters)++--LefttoRight
PrefixUnaryoperators++--+-~!RighttoLeft
Objectcreationandcastnew(type)RighttoLeft
Multiplication/Division/Modulus*/%LefttoRight
Addition/Subtraction+-LefttoRight
Shift>>>>><<LefttoRight
Relational<<=>>=instanceofLefttoRight
Equality==!=LefttoRight
Bit-wise/BooleanAND&LefttoRight
Bit-wise/BooleanXOR^LefttoRight
Bit-wise/BooleanOR|LefttoRight
LogicalAND(Short-circuitorConditional)&&LefttoRight
LogicalOR(Short-circuitorConditional)||LefttoRight
Ternary?:RighttoLeft
Assignment=+=-=*=/=%=<<=>>=>>>=&=^=|=RighttoLeft
到时我们不用学struts,不用学spring,不用学Hibernate,只要能把jsf学会了,完全可以替代所有的框架,包括AJAX,都知道AJAX并不是新技术,虽说我没深入学习jsf但我认为jsf应该已经能通过其它技术替代AJAX,实现无缝刷新。 |
|