KEMBAR78
OCA JAVA - 3 Programming with Java Operators | PPTX
3. Programming with
Java Operators
• Understanding Fundamentals Operators
• Understanding Operator Precedence
By Fernando Gil
Date: Jan 2015
Version: 1.0
Based in the book OCA Java SE 7 Programmer I Study Guide (Examn 1Z0-803)
1. Understanding Fundamental
Operators
• Java operators are used to return a result from an
expression using one, two, or three operands
• Operands are the values placed to the right or left side of
the operators
• Prefix/postfix – increment/decrement operators use one
operand
• The conditional ternary operator (?:) uses three operands
• All other operators use two operands
Assignment Operators
• Assignment operators are used to assign values
to variables
• The assignment operator by itself is the equal
sign
• At it simplest, the assignment operator moves
valid literals into variables
• Assignment operators cause compiler errors
when the literals are not valid for the variable to
which they are assigned
Compound Assignment
Operators
• Compound assignment operators provide a shorter
syntax for assigning the result of an arithmetic or bitwise
operator
• They perform the operation on the two operands before
assigning the result to the first operand
• There are 11 compound assignment operators
• While the use of compound assignment operators cuts
down on keystrokes, it is generally good practice to use
the longhand approach since the code is clearly more
readable
Operator Function
+= Assigns the result of the addition
- = Assigns the result of the subtraction
*= Assigns the result of the multiplication
/= Assigns the result of the division
%= Assigns the remainder of the division
&= Assigns the result of the logical AND
|= Assigns the result of the logical OR
^= Assigns the result of the logical XOR
<<= Assigns the result of the signed left bit shift
>>= Assigns the result of the signed right bit shift
>>>= Assigns the result of the unsigned right bit shift
Basic Arithmetic Operators
Operator Function
+ Addition (sum) operator
- Subtraction (difference) operator
* Multiplication (product) operator
/ Division (quotient) operator
% Modulus (remainder) operator
int totalCannonBalls = greyCannonBalls + blackCannonBalls;
Prefix and Postfix Operators
Operator Function
++ X Prefix increment operator
-- X Prefix decrement operator
X++ Postfix increment operator
X -- Postfix decrement operator
• The execution of prefix operators occurs on the operand
prior to the evaluation of the whole expression
• The execution of postfix operators occurs after the
expression has been evaluated
Basic Relational Operators
Operator Function
< Less than operator
<= Less than or equal to operator
> Greater than operator
>= Greater than or equal to operator
• Those operators are used to compare integers, floating
points, and characters
• When the expression used with the relational operators is
true, the Boolean value of true is returned; otherwise, false
is returned
Equality Operators
Operator Function
== Equal to operator
!= Not equal to operator
• Relational operators that directly compare the equality of
primitives and object reference variables are considered
equality operators
Logical Operators
Operator Function
&& Logical AND to operator
|| Logical OR operator
• Logical (conditional) operators evaluate a pair of Boolean
operands. If both values of the operands have a value of
true, then a value of true is returned
• To return true with the AND operator both operands would
need to be true.
• To return true with the OR operator only one operand
needs to be true
Logical Negation Operator
Operator Function
! Logical negation operator
• It is also known as the inversion operator or Boolean invert
operator, and it returns the opposite of a Boolean value
• Expect to see the logical negation operator used in
conjunction with any method or expression that return a
Boolean value
• This operator cannot be used on a non-Boolean value
2. Understanding Operator
Precedence
• Operator precedence is the order in which
operators will be evaluated when several operators
are included in an expression
• Operators with a higher precedence are evaluated
before operators with a lower precedence
• Operator precedence can be overridden using
parentheses
• When multiple sets of parentheses are present, the
innermost set is evaluated first
Precedence Order
• When two operators share an operand, the operator with
the higher precedence goes first. For example:
1 + 2 * 3 is treated as 1 + (2 * 3)
whereas
1 * 2 + 3 is treated as (1 * 2) + 3
since multiplication has a higher precedence than addition
Associativity
• When an expression has two operators with the same
precedence, the expression is evaluated according to its
associativity. For example:
x = y = z = 17 is treated as x = (y = (z = 17))
leaving all three variables with the value 17, since the =
operator has right-to-left associativity. On the other hand,
72 / 2 / 3 is treated as (72 / 2) / 3
since the / operator has left-to-right associativity.
Precedence and Associativity
of Java Operators
Operator Description Level Associativity
[] Access array element 1 Left to right
. Access object member
() Invoke a method
++ Post-increment
-- Post-decrement
++ Pre-increment 2 Right to left
-- Pre-decrement
+ Unary plus
- Unary minus
! Logical NOT
~ Bitwise NOT
Operator Description Level Associativity
() Cast 3 Right to left
new Object creation
* Multiplicative 4 Left to right
/
%
+ - Additive 5 Left to right
+ String concatenation
<< >> Shift 6 Left to right
>>>
< <= Relational type comparison 7 Left to right
> >=
== Equality 8 Left to right
!=
& Bitwise AND 9 Left to right
^ Bitwise XOR 10 Left to right
Operator Description Level Associativity
| Bitwise OR 11 Left to right
&& Conditional AND 12 Left to right
|| Conditional OR 13 Left to right
?: Conditional 14 Right to left
= += -= Assignment 15 Right to left
*= /= %=
&= ^= |=
<<= >>= >>>=
4. Programming with
Java Strings
Next Slide:

OCA JAVA - 3 Programming with Java Operators

  • 1.
    3. Programming with JavaOperators • Understanding Fundamentals Operators • Understanding Operator Precedence By Fernando Gil Date: Jan 2015 Version: 1.0 Based in the book OCA Java SE 7 Programmer I Study Guide (Examn 1Z0-803)
  • 2.
    1. Understanding Fundamental Operators •Java operators are used to return a result from an expression using one, two, or three operands • Operands are the values placed to the right or left side of the operators • Prefix/postfix – increment/decrement operators use one operand • The conditional ternary operator (?:) uses three operands • All other operators use two operands
  • 4.
    Assignment Operators • Assignmentoperators are used to assign values to variables • The assignment operator by itself is the equal sign • At it simplest, the assignment operator moves valid literals into variables • Assignment operators cause compiler errors when the literals are not valid for the variable to which they are assigned
  • 5.
    Compound Assignment Operators • Compoundassignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator • They perform the operation on the two operands before assigning the result to the first operand • There are 11 compound assignment operators • While the use of compound assignment operators cuts down on keystrokes, it is generally good practice to use the longhand approach since the code is clearly more readable
  • 6.
    Operator Function += Assignsthe result of the addition - = Assigns the result of the subtraction *= Assigns the result of the multiplication /= Assigns the result of the division %= Assigns the remainder of the division &= Assigns the result of the logical AND |= Assigns the result of the logical OR ^= Assigns the result of the logical XOR <<= Assigns the result of the signed left bit shift >>= Assigns the result of the signed right bit shift >>>= Assigns the result of the unsigned right bit shift
  • 7.
    Basic Arithmetic Operators OperatorFunction + Addition (sum) operator - Subtraction (difference) operator * Multiplication (product) operator / Division (quotient) operator % Modulus (remainder) operator int totalCannonBalls = greyCannonBalls + blackCannonBalls;
  • 8.
    Prefix and PostfixOperators Operator Function ++ X Prefix increment operator -- X Prefix decrement operator X++ Postfix increment operator X -- Postfix decrement operator • The execution of prefix operators occurs on the operand prior to the evaluation of the whole expression • The execution of postfix operators occurs after the expression has been evaluated
  • 9.
    Basic Relational Operators OperatorFunction < Less than operator <= Less than or equal to operator > Greater than operator >= Greater than or equal to operator • Those operators are used to compare integers, floating points, and characters • When the expression used with the relational operators is true, the Boolean value of true is returned; otherwise, false is returned
  • 10.
    Equality Operators Operator Function ==Equal to operator != Not equal to operator • Relational operators that directly compare the equality of primitives and object reference variables are considered equality operators
  • 11.
    Logical Operators Operator Function &&Logical AND to operator || Logical OR operator • Logical (conditional) operators evaluate a pair of Boolean operands. If both values of the operands have a value of true, then a value of true is returned • To return true with the AND operator both operands would need to be true. • To return true with the OR operator only one operand needs to be true
  • 12.
    Logical Negation Operator OperatorFunction ! Logical negation operator • It is also known as the inversion operator or Boolean invert operator, and it returns the opposite of a Boolean value • Expect to see the logical negation operator used in conjunction with any method or expression that return a Boolean value • This operator cannot be used on a non-Boolean value
  • 13.
    2. Understanding Operator Precedence •Operator precedence is the order in which operators will be evaluated when several operators are included in an expression • Operators with a higher precedence are evaluated before operators with a lower precedence • Operator precedence can be overridden using parentheses • When multiple sets of parentheses are present, the innermost set is evaluated first
  • 14.
    Precedence Order • Whentwo operators share an operand, the operator with the higher precedence goes first. For example: 1 + 2 * 3 is treated as 1 + (2 * 3) whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition
  • 15.
    Associativity • When anexpression has two operators with the same precedence, the expression is evaluated according to its associativity. For example: x = y = z = 17 is treated as x = (y = (z = 17)) leaving all three variables with the value 17, since the = operator has right-to-left associativity. On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity.
  • 16.
    Precedence and Associativity ofJava Operators Operator Description Level Associativity [] Access array element 1 Left to right . Access object member () Invoke a method ++ Post-increment -- Post-decrement ++ Pre-increment 2 Right to left -- Pre-decrement + Unary plus - Unary minus ! Logical NOT ~ Bitwise NOT
  • 17.
    Operator Description LevelAssociativity () Cast 3 Right to left new Object creation * Multiplicative 4 Left to right / % + - Additive 5 Left to right + String concatenation << >> Shift 6 Left to right >>> < <= Relational type comparison 7 Left to right > >= == Equality 8 Left to right != & Bitwise AND 9 Left to right ^ Bitwise XOR 10 Left to right
  • 18.
    Operator Description LevelAssociativity | Bitwise OR 11 Left to right && Conditional AND 12 Left to right || Conditional OR 13 Left to right ?: Conditional 14 Right to left = += -= Assignment 15 Right to left *= /= %= &= ^= |= <<= >>= >>>=
  • 19.
    4. Programming with JavaStrings Next Slide: