Java Reference
In-Depth Information
cedencelevelis10andmultiplication'sprecedencelevelis11,whichmeansthatmul-
tiplication is performed before addition.
Precedencecanbecircumventedbyintroducingopenandcloseparentheses, ( and ) ,
intotheexpression,wheretheinnermostpairofnestedparenthesesisevaluatedfirst.For
example,2* ((60+3)*6) resultsin (60+3) beingevaluatedfirst, (60+3)*6 being
evaluatednext,andtheoverallexpressionbeingevaluatedlast.Similarly,intheexpres-
sion 60/(3-6) , subtraction is performed before division.
Duringevaluation,operatorswiththesameprecedencelevel(e.g.,additionandsub-
traction,whichbothhavelevel10)areprocessedaccordingtotheir associativity (aprop-
ertythatdetermineshowoperatorshavingthesameprecedencearegroupedwhenpar-
entheses are missing).
Forexample,expression 9*4/3 isevaluatedasifitwas (9*4)/3 because * and /
areleft-to-rightassociativeoperators.Incontrast,expression x=y=z=100 isevaluated
asifitwas x=(y=(z=100)) 100 isassignedto z , z 'snewvalue(100)isassigned
to y ,and y 'snewvalue(100)isassignedto x -because = isaright-to-leftassociative
operator.
MostofJava'soperatorsareleft-to-rightassociative.Right-to-leftassociativeoperat-
ors include assignment, bitwise complement, cast, compound assignment, conditional,
logicalcomplement,objectcreation,predecrement,preincrement,unaryminus,andun-
ary plus.
Note Unlike languages such as C++, Java doesn't let you overload operators.
However, Java overloads the + , ++ , and -- operator symbols.
Statements
Statementsaretheworkhorsesofaprogram.Theyassignvaluestovariables,controla
program'sflowbymakingdecisionsand/orrepeatedlyexecutingotherstatements,and
perform other tasks. A statement can be expressed as a simple statement or as a com-
pound statement:
• A simple statement isasinglestandalonesourcecodeinstructionforperforming
some task; it's terminated with a semicolon.
• A compound statement isa(possiblyempty)sequenceofsimpleandothercom-
poundstatementssandwichedbetweenopenandclosebracedelimiters—a de-
limiter isacharacterthatmarksthebeginningorendofsomesection.Amethod
body(e.g.,the main() method'sbody)isanexample.Compoundstatements
Search WWH ::




Custom Search