Operators in Java – Basic Language Symbology for Beginners
Or… What does that
crazy ‘^’ symbol mean anyway?
Java has (as of
this writing in Java 1.7) 37 tokens that are designated as operators. (See the
Java Language Specification http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.12”>here.
Non-assignment tokens are always
evaluated left to right, with a set of precedence rules applied. Assignment
tokens evaluate right to left. Order of precedence tells us that a complex
statement with lots of operators will be guaranteed to be evaluated in a
reliable order. I won’t cover the specifics of order in this entry… they’re
very nicely charted in the Oracle Java Tutorial link at the end of this post.
An oft-asked question I receive from “future Java gurus”
(beginners J)
is, “What is that char and what does it mean?” Or “what is
the difference between ‘&’ and ‘&&’?”
I know computer science training helps a lot, but if you’re
like me, you might have bootstrapped yourself into software engineering without
an official BS/CS degree completion.
I thought I would encapsulate a few of the most common
things that beginners tend to ask, and put them in human-readable form. This
isn’t intended to be a be-all and define-all, just a quick post to get your
head wrapped around a few less-commonly-used operators.
Remember: if your statement gets long and hairy, you can
always break it down into smaller parts, or you can use parentheses to “force”
precedence.
& The bitwise
AND operator (applies to integral/primitive types) also works with booleans
int resultingInt = 12 & 15;
//resultingInt is now assigned with
the integer 12
//it helps to understand the binary representations of the operands to see how this works
//homework: why 12?
| The bitwise inclusive OR operator (applies to
integral/primitive types) also works with booleans
int resultingInt = 12 | 15;
//resultingInt is now assigned with
the integer 15
//it helps to understand the binary representations of the operands to see how this works
//homework: why 15
^ the bitwise exclusive OR operator (applies to
integral/primitive types) also works with booleans
int resultingInt = 12 ^ 15;
//resultingInt is now assigned with
the integer 3
//it helps to understand the binary representations of the operands to see how this works
//homework: why 3?
&& the logical
AND operator
boolean isTrue = boolean1 &&
boolean2;
//in English: “if boolean1 is true AND
boolean2 is true, assign true to isTrue,
//else, assign false to isTrue”
//This operator is”fail fast.” If
the first operand is false, the second isn’t evaluated.
|| the logical OR operator
boolean isTrue = boolean1 || boolean2;
//in English: “if boolean1 is true
OR boolean2 is true, assign true to isTrue,
//else, assign false to isTrue”
variable =
? : ; The ternary
operator
int firstYear = 0;
int lastYear = 12;
boolean isLastYearLater = lastYear <
firstYear ? false : true;
//in English: “if last year is less
than firstYear,
//assign false to isLastYearLater,
else assign true to isLastYearLater”
Want to know more? Ask the source! The Java Tutorials have a
nice concise up-to-date page here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html”>
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Want to see a glimpse of how the bitwise operators work? http://www.leepoint.net/notes-java/data/expressions/bitops.html">This is a good starting point.
No comments:
Post a Comment
Thank you for commenting. I'll review your comments as time permits and will publish them when I can.