SlideShare a Scribd company logo
1 of 19
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:

More Related Content

What's hot

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 expressionsvishaljot_kaur
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressionsvinay 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 programmingsavitamhaske
 
Operators in python
Operators in pythonOperators in python
Operators in pythoneShikshak
 
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 JavaAbhilash Nair
 
Operators in Python
Operators in PythonOperators in Python
Operators in PythonAnusuya123
 

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++
 
Operators
OperatorsOperators
Operators
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
operators in c++
operators in c++operators in c++
operators in c++
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
 
Operators
OperatorsOperators
Operators
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Operators in c++
Operators in c++Operators in c++
Operators in c++
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Report on c
Report on cReport on c
Report on c
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 

Viewers also liked

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 StatementsFernando 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 algortihmSajid 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 ExamsGanesh Samarthyam
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsArvind Krishnaa
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in JavaJin Castor
 
Type conversions
Type conversionsType conversions
Type conversionssanya6900
 
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 CarsLinkedIn
 
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 WorkVolker Hirsch
 

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
 
Java - Operators
Java - OperatorsJava - Operators
Java - Operators
 
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
 
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
 
Java Datatypes
Java DatatypesJava Datatypes
Java Datatypes
 
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
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Type conversions
Type conversionsType conversions
Type conversions
 
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
 
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
 

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

Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
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
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
Java script operators
Java script operatorsJava script operators
Java script operators
 
Oop using JAVA
Oop using JAVAOop using JAVA
Oop using JAVA
 
Operators in java By cheena
Operators in java By cheenaOperators in java By cheena
Operators in java By cheena
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
Chap 3(operator expression)
 
C# operators
C# operatorsC# operators
C# operators
 
operat in vb .pptx
operat in vb .pptxoperat in vb .pptx
operat in vb .pptx
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
 
02-01-0-Operators and Operands.pdf
02-01-0-Operators and Operands.pdf02-01-0-Operators and Operands.pdf
02-01-0-Operators and Operands.pdf
 
Python Programming | JNTUK | UNIT 1 | Lecture 5
Python Programming | JNTUK | UNIT 1 | Lecture 5Python Programming | JNTUK | UNIT 1 | Lecture 5
Python Programming | JNTUK | UNIT 1 | Lecture 5
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
C++
C++ C++
C++
 
Operators in java script
Operators   in  java scriptOperators   in  java script
Operators in java script
 

Recently uploaded

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 

Recently uploaded (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

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
  • 3.
  • 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: