SlideShare a Scribd company logo
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
 OCA JAVA - 3 Programming with Java Operators
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:

More Related Content

What's hot (19)

PPT
CBSE Class XI :- Operators in C++
Pranav Ghildiyal
 
ODP
Operators
jayesh30sikchi
 
PPTX
Operator.ppt
Darshan Patel
 
PPTX
Operators and expressions
vishaljot_kaur
 
PPT
C Prog. - Operators and Expressions
vinay arora
 
PDF
Operators in java
Ravi_Kant_Sahu
 
PPT
Operators and Expressions in C++
Praveen M Jigajinni
 
PDF
Operators in c programming
savitamhaske
 
ODP
operators in c++
Kartik Fulara
 
PDF
Operators in python
eShikshak
 
PPT
Expressions in c++
zeeshan turi
 
PPT
Operators
Devi Pradeep Podugu
 
PPT
Operator & Expression in c++
bajiajugal
 
PPTX
Operators in c++
ABHIJITPATRA23
 
PPTX
Operators in C & C++ Language
PreSolutions Softwares
 
PPTX
Operators and Expressions in Java
Abhilash Nair
 
DOC
Report on c
jasmeen kr
 
PDF
Operators in python
Prabhakaran V M
 
PPTX
Operators in Python
Anusuya123
 
CBSE Class XI :- Operators in C++
Pranav Ghildiyal
 
Operators
jayesh30sikchi
 
Operator.ppt
Darshan Patel
 
Operators and expressions
vishaljot_kaur
 
C Prog. - Operators and Expressions
vinay arora
 
Operators in java
Ravi_Kant_Sahu
 
Operators and Expressions in C++
Praveen M Jigajinni
 
Operators in c programming
savitamhaske
 
operators in c++
Kartik Fulara
 
Operators in python
eShikshak
 
Expressions in c++
zeeshan turi
 
Operator & Expression in c++
bajiajugal
 
Operators in c++
ABHIJITPATRA23
 
Operators in C & C++ Language
PreSolutions Softwares
 
Operators and Expressions in Java
Abhilash Nair
 
Report on c
jasmeen kr
 
Operators in python
Prabhakaran V M
 
Operators in Python
Anusuya123
 

Viewers also liked (15)

PPTX
OCA JAVA - 2 Programming with Java Statements
Fernando Gil
 
PPT
Java - Operators
Preethi Nambiar
 
PPTX
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce
 
PPT
how to calclute time complexity of algortihm
Sajid Marwat
 
PPTX
Java Datatypes
Mayank Aggarwal
 
PDF
Cracking OCA and OCP Java 8 Exams
Ganesh Samarthyam
 
PPT
Java Basics
Dhanunjai Bandlamudi
 
PPT
Unit 2 Java
arnold 7490
 
PDF
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
PPTX
Design and Analysis of Algorithms
Arvind Krishnaa
 
PPTX
Control statements in Java
Jin Castor
 
PPT
Type conversions
sanya6900
 
PDF
Study: The Future of VR, AR and Self-Driving Cars
LinkedIn
 
PDF
TEDx Manchester: AI & The Future of Work
Volker Hirsch
 
OCA JAVA - 2 Programming with Java Statements
Fernando Gil
 
Java - Operators
Preethi Nambiar
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
İbrahim Kürce
 
how to calclute time complexity of algortihm
Sajid Marwat
 
Java Datatypes
Mayank Aggarwal
 
Cracking OCA and OCP Java 8 Exams
Ganesh Samarthyam
 
Unit 2 Java
arnold 7490
 
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Design and Analysis of Algorithms
Arvind Krishnaa
 
Control statements in Java
Jin Castor
 
Type conversions
sanya6900
 
Study: The Future of VR, AR and Self-Driving Cars
LinkedIn
 
TEDx Manchester: AI & The Future of Work
Volker Hirsch
 
Ad

Similar to OCA JAVA - 3 Programming with Java Operators (20)

PPTX
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
PDF
Java basic operators
Emmanuel Alimpolos
 
PPTX
Class_IX_Operators.pptx
rinkugupta37
 
PPTX
Java Operators with Simple introduction.pptx
kuntadinesh21
 
PDF
Lec13
Sri Harsha Pamu
 
PPTX
Java Operators with Simple introduction.pptx
kuntadinesh21
 
PPTX
Java chapter 3
Munsif Ullah
 
PDF
SPL 6 | Operators in C
Mohammad Imam Hossain
 
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
PPTX
11operator in c#
Sireesh K
 
PPTX
L3 operators
teach4uin
 
PPTX
L3 operators
teach4uin
 
PPTX
L3 operators
teach4uin
 
PPTX
Logical Operators C/C++ language Programming
Nawab Developers
 
PDF
introduction to c programming - Topic 3.pdf
rajd20284
 
PPTX
Operators and expressions in c language
tanmaymodi4
 
PPTX
Operators inc c language
Tanmay Modi
 
PPT
operator
aamirsahito
 
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Java basic operators
Emmanuel Alimpolos
 
Class_IX_Operators.pptx
rinkugupta37
 
Java Operators with Simple introduction.pptx
kuntadinesh21
 
Java Operators with Simple introduction.pptx
kuntadinesh21
 
Java chapter 3
Munsif Ullah
 
SPL 6 | Operators in C
Mohammad Imam Hossain
 
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
11operator in c#
Sireesh K
 
L3 operators
teach4uin
 
L3 operators
teach4uin
 
L3 operators
teach4uin
 
Logical Operators C/C++ language Programming
Nawab Developers
 
introduction to c programming - Topic 3.pdf
rajd20284
 
Operators and expressions in c language
tanmaymodi4
 
Operators inc c language
Tanmay Modi
 
operator
aamirsahito
 
Ad

Recently uploaded (20)

PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 

OCA JAVA - 3 Programming with Java Operators

  • 1. 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)
  • 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 • 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
  • 5. 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
  • 6. 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
  • 7. Basic Arithmetic Operators Operator Function + Addition (sum) operator - Subtraction (difference) operator * Multiplication (product) operator / Division (quotient) operator % Modulus (remainder) operator int totalCannonBalls = greyCannonBalls + blackCannonBalls;
  • 8. 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
  • 9. 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
  • 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 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
  • 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 • 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
  • 15. 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.
  • 16. 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
  • 17. 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
  • 18. 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 *= /= %= &= ^= |= <<= >>= >>>=
  • 19. 4. Programming with Java Strings Next Slide: