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)

CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
Pranav Ghildiyal
 
Operators
OperatorsOperators
Operators
jayesh30sikchi
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
Darshan Patel
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
vishaljot_kaur
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
vinay arora
 
Operators in java
Operators in javaOperators in java
Operators in java
Ravi_Kant_Sahu
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
Praveen M Jigajinni
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
savitamhaske
 
operators in c++
operators in c++operators in c++
operators in c++
Kartik Fulara
 
Operators in python
Operators in pythonOperators in python
Operators in python
eShikshak
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
zeeshan turi
 
Operators
OperatorsOperators
Operators
Devi Pradeep Podugu
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 
Operators in c++
Operators in c++Operators in c++
Operators in c++
ABHIJITPATRA23
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
PreSolutions Softwares
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
Abhilash Nair
 
Report on c
Report on cReport on c
Report on c
jasmeen kr
 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
Anusuya123
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
Pranav Ghildiyal
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
vishaljot_kaur
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
vinay arora
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
Praveen M Jigajinni
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
savitamhaske
 
Operators in python
Operators in pythonOperators in python
Operators in python
eShikshak
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
zeeshan turi
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
Abhilash Nair
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
Anusuya123
 

Viewers also liked (15)

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

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

Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
Emmanuel Alimpolos
 
C# operators
C# operatorsC# operators
C# operators
baabtra.com - No. 1 supplier of quality freshers
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
rinkugupta37
 
Java Operators with Simple introduction.pptx
Java Operators with Simple introduction.pptxJava Operators with Simple introduction.pptx
Java Operators with Simple introduction.pptx
kuntadinesh21
 
Lec13
Lec13Lec13
Lec13
Sri Harsha Pamu
 
Java Operators with Simple introduction.pptx
Java Operators with Simple introduction.pptxJava Operators with Simple introduction.pptx
Java Operators with Simple introduction.pptx
kuntadinesh21
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
Munsif Ullah
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
Mohammad Imam Hossain
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
11operator in c#
11operator in c#11operator in c#
11operator in c#
Sireesh K
 
L3 operators
L3 operatorsL3 operators
L3 operators
teach4uin
 
L3 operators
L3 operatorsL3 operators
L3 operators
teach4uin
 
L3 operators
L3 operatorsL3 operators
L3 operators
teach4uin
 
Logical Operators C/C++ language Programming
Logical Operators C/C++ language ProgrammingLogical Operators C/C++ language Programming
Logical Operators C/C++ language Programming
Nawab Developers
 
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdfintroduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
rajd20284
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
Tanmay Modi
 
Java script operators
Java script operatorsJava script operators
Java script operators
baabtra.com - No. 1 supplier of quality freshers
 
operator
operatoroperator
operator
aamirsahito
 
Ad

Recently uploaded (20)

Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration KeySmadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
joybepari360
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWSWomen in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Who will create the languages of the future?
Who will create the languages of the future?Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
Open Source Software Development Methods
Open Source Software Development MethodsOpen Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptxwAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
 
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.pptSAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptxMicrosoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
Transmission Media. (Computer Networks)
Transmission Media.  (Computer Networks)Transmission Media.  (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutionsZoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Migrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right WayMigrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right Way
Alexander (Alex) Komyagin
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration KeySmadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
joybepari360
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWSWomen in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Who will create the languages of the future?
Who will create the languages of the future?Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
Open Source Software Development Methods
Open Source Software Development MethodsOpen Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptxwAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
 
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.pptSAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptxMicrosoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
Transmission Media. (Computer Networks)
Transmission Media.  (Computer Networks)Transmission Media.  (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutionsZoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 

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: