SlideShare a Scribd company logo
1 of 30
April 6, 1998 CS102-02 Lecture 2-1
Java Operators
CS 102-02
Lecture 2-1
Being a Smooth Operator
April 6, 1998 CS102-02 Lecture 2-1
Operating with Java
• Most programming languages have
operators
– Operators are short-hand symbols for actions
= Assign right to left
+ Add two numbers (or concatenate two strings)
– Operators in Java have fixed meaning
• No operator overloading
• Can’t say:
List = List + Item; // Add item to list
April 6, 1998 CS102-02 Lecture 2-1
Kinds of Operators
Category What it does… Examples
Arithmetic Addition, subtraction +, -, /
Assignment Set a value to an expression =, +=, &=
Conditional Choose one of two values ? :
Logical Logical comparisons &&, ||
Relational Compare values ==, >=
Bitwise Move bits within a number <<, >>
April 6, 1998 CS102-02 Lecture 2-1
Operator Precedence
• Usually things go left-to-right, but there are
precedence rules
• Nutshell reading lists operators by
precedence
• Override precedence with ()’s
April 6, 1998 CS102-02 Lecture 2-1
Arithmetic Operators
• The usual suspects: plus, minus, blah, blah,
blah
• Modulo/remainder operator
April 6, 1998 CS102-02 Lecture 2-1
Modulo Operator
• Modulo (or remainder) operator: what’s left
over after division
7%3 = 1
198%3 = ??
6.0%4.0 = 2
• Is it odd or even?
• Looping with clock arithmetic
– Appointment at 5pm everyday
– Baking 217 cakes: step 3 of 7 same as 24 of 28
April 6, 1998 CS102-02 Lecture 2-1
Short-Hand Operators
• Increment and decrement: ++ and --
– Often need to add or subtract 1
– Pre: Add (subtract) first
– Post: Add (subtract) afterwards
• Compiler can sometimes optimize
April 6, 1998 CS102-02 Lecture 2-1
Testing Out Short-Hand
After X Y Whole thing
X++ * Y
++X + Y
++X / Y++
Suppose we start with:
X = 7;
Y = 9;
What’s the difference between:
X++;
++X;
April 6, 1998 CS102-02 Lecture 2-1
Are You My Type?
• What’s the type of a result?
Expression Result type
int * int int
float * float ??
int * float ??
int / int ??
• Conversion & promotion
April 6, 1998 CS102-02 Lecture 2-1
Assignment Operators
• Change the value on the left to the value of
the expression on the right
If you want to: Try:
Assign 8 to Y Y = 8;
Add 1 to Y Y++;
Assign Y+10 to Y X += 10;
April 6, 1998 CS102-02 Lecture 2-1
Works for Strings Too
• Strings are “added” (concatenated) with +
What is Name after the third line?
Name = “Simpson”;
First = “Lisa”;
Name += First;
What’s the result here?
Age = 11;
Message = “He’s “ + Age + “ years old.”;
April 6, 1998 CS102-02 Lecture 2-1
Conditional Operator
• Instead of If..Then..Else, use ?:
• Takes three arguments in the form:
Boolean condition ? If-true : If-false
If (Simpson == “Lisa”) {
Message = “She’s our favorite!”;
} else {
Message= “Doh!”;
}
System.out.println(Message);
is the same as…
April 6, 1998 CS102-02 Lecture 2-1
Using the Conditional Operator
System.out.println(Simpson==“Lisa”
? ”She’s our favorite” :“Doh!”);
(The above should be on one line in a real program)
April 6, 1998 CS102-02 Lecture 2-1
And, But and Or will get you
pretty far..
• Logical operators combine simple
expressions to form complex ones
• Boolean logic
Expression One Expression Two One AND Two One OR Two One XOR Two
False False False False False
False True False True True
True False False True True
True True True True False
April 6, 1998 CS102-02 Lecture 2-1
Boolean Types
• True or false are real values in Java
• Some languages just use 0 and not 0
if (y = 7) then …
• In Java result of a comparison is Boolean
8 != 9 ??
8 != 8 ??
April 6, 1998 CS102-02 Lecture 2-1
Logical Operators in Java
• Translating logic into Java
AND &&
OR ||
XOR ^
NOT !
April 6, 1998 CS102-02 Lecture 2-1
Boolean Expressions
• De Morgan’s Laws with Expressions One & Two
One OR Two == One AND Two
One AND Two == One OR Two
• Some handy relations
One XOR One == False
One OR One == True
April 6, 1998 CS102-02 Lecture 2-1
Short-Circuit
• Remember:
False AND Anything == False
True OR Anything == True
• Sometimes compiler can short-circuit and
skip evaluation of second expression
• What if there are side effects?
April 6, 1998 CS102-02 Lecture 2-1
Sideline on Side Effects
• Side effects are results of expression
evaluation other than the expression’s value
• Examples
X++;
– Output:
System.out.println(“Howdy!”);
April 6, 1998 CS102-02 Lecture 2-1
Short-Circuiting Side Effects
• Short-circuiting could prevent a side effect
• How do you force the compiler to evaluate
a second expression?
April 6, 1998 CS102-02 Lecture 2-1
No Short-Circuit Here
• Guarantee that the second expression is
evaluated
AND &
OR |
XOR ^
(Why is ^ listed here?)
April 6, 1998 CS102-02 Lecture 2-1
Relational Operators
• Determine the relationship between values
• Equality & inequality
• Less than, greater than
April 6, 1998 CS102-02 Lecture 2-1
(In)Equality
• Equality is different from assignment
== != =
• Most keyboards just have =
– Use == for equality
– And != for inequality
April 6, 1998 CS102-02 Lecture 2-1
Bitwise Operators
• Computers are binary creatures:
everything’s on or off
• For example, computers can’t store decimal
numbers so
April 6, 1998 CS102-02 Lecture 2-1
Binary Arithmetic
• Everything’s in powers of two
• Turn 78 into:
128 64 32 16 8 4 2 1
0 1 0 0 1 1 1 0
64 8 4 2
April 6, 1998 CS102-02 Lecture 2-1
Accentuate the positive
• Computers don’t know about negative
numbers
• Use the first (leftmost) bit as a sign bit:
1 if negative: -5 is 11111101
0 if positive: +5 is 00000011
April 6, 1998 CS102-02 Lecture 2-1
Bitwise is Binary
• Work with the bits inside the values
• Only good for integral values (integer
numbers, bytes and characters)
Operator Name Description
& AND AND the corresponding bits in the two
operands
| OR OR the corresponding bits in the two
operands
^ XOR XOR the corresponding bits in the two
operands
<< Left shift Shift the bits from right to left
>> Right shift with sign
extension
Shift the bits right and preserve the sign
>>> Right shift with zero
extension
Shift the bits right and always fill in 0’s
~ Complement Switch 0’s and 1’s
April 6, 1998 CS102-02 Lecture 2-1
And Shift Your Bits ‘Round and
‘Round
• Bitwise AND of 78 and 34
128 64 32 16 8 4 2 1
78 0 1 0 0 1 1 1 0
34 0 0 1 0 0 0 1 0
2 0 0 0 0 0 0 1 0
April 6, 1998 CS102-02 Lecture 2-1
Why Bother with Bitwise?
• Use numbers not for themselves but for
their internal representations
• Example: A tic-tac-toe grid might have 0’s
for O’s and 1’s for X’s
• Just need 9 bits to do the whole table and
only 27 bits for 3-D tic-tac-toe
April 6, 1998 CS102-02 Lecture 2-1
That’s It for Operators
• Operators are key to building large
expressions in Java
• Know operator precedence (or at least
where to look it up)
• Next time: Use operators to build
expressions for control structures

More Related Content

Similar to Java-operators.ppt

Chaptfffffuuer05.PPT
Chaptfffffuuer05.PPTChaptfffffuuer05.PPT
Chaptfffffuuer05.PPTsdvdsvsdvsvds
 
9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & SearchingMandeep Singh
 
Module I - Digital Systems & Logic Gates.ppt
Module I - Digital Systems & Logic Gates.pptModule I - Digital Systems & Logic Gates.ppt
Module I - Digital Systems & Logic Gates.pptAbhiRamPB2
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysisjayavignesh86
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
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 5FabMinds
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniquesPrabhjit Singh
 
8. operators
8. operators8. operators
8. operatorsWay2itech
 
Complex variable transformation presentation.pptx
Complex variable transformation presentation.pptxComplex variable transformation presentation.pptx
Complex variable transformation presentation.pptxHuzaifaAhmad51
 
Python lab basics
Python lab basicsPython lab basics
Python lab basicsAbi_Kasi
 
Mov is turing-complete
Mov is turing-completeMov is turing-complete
Mov is turing-completeyeokm1
 
Python programming
Python programmingPython programming
Python programmingsaroja20
 
Floating point arithmetic operations (1)
Floating point arithmetic operations (1)Floating point arithmetic operations (1)
Floating point arithmetic operations (1)cs19club
 

Similar to Java-operators.ppt (20)

Chaptfffffuuer05.PPT
Chaptfffffuuer05.PPTChaptfffffuuer05.PPT
Chaptfffffuuer05.PPT
 
3.Loops_conditionals.pdf
3.Loops_conditionals.pdf3.Loops_conditionals.pdf
3.Loops_conditionals.pdf
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
 
Module I - Digital Systems & Logic Gates.ppt
Module I - Digital Systems & Logic Gates.pptModule I - Digital Systems & Logic Gates.ppt
Module I - Digital Systems & Logic Gates.ppt
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
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
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniques
 
8. operators
8. operators8. operators
8. operators
 
Programing techniques
Programing techniquesPrograming techniques
Programing techniques
 
Complex variable transformation presentation.pptx
Complex variable transformation presentation.pptxComplex variable transformation presentation.pptx
Complex variable transformation presentation.pptx
 
Instruction types
Instruction typesInstruction types
Instruction types
 
Python lab basics
Python lab basicsPython lab basics
Python lab basics
 
Session 4.pptx
Session 4.pptxSession 4.pptx
Session 4.pptx
 
Mov is turing-complete
Mov is turing-completeMov is turing-complete
Mov is turing-complete
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
 
Python programming
Python programmingPython programming
Python programming
 
Floating point arithmetic operations (1)
Floating point arithmetic operations (1)Floating point arithmetic operations (1)
Floating point arithmetic operations (1)
 

More from kavitamittal18

Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingLec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingkavitamittal18
 
JDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptJDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptkavitamittal18
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteskavitamittal18
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects conceptkavitamittal18
 
480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentationkavitamittal18
 
gsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptgsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptkavitamittal18
 
ELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxkavitamittal18
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptkavitamittal18
 

More from kavitamittal18 (17)

Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingLec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreading
 
JDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptJDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java ppt
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture notes
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept
 
480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation
 
gsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptgsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing ppt
 
AdHocTutorial.ppt
AdHocTutorial.pptAdHocTutorial.ppt
AdHocTutorial.ppt
 
ELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptx
 
java_lect_03-2.ppt
java_lect_03-2.pptjava_lect_03-2.ppt
java_lect_03-2.ppt
 
Input and Output.pptx
Input and Output.pptxInput and Output.pptx
Input and Output.pptx
 
ch11.ppt
ch11.pptch11.ppt
ch11.ppt
 
11.ppt
11.ppt11.ppt
11.ppt
 
Ch06Part1.ppt
Ch06Part1.pptCh06Part1.ppt
Ch06Part1.ppt
 
IntroToOOP.ppt
IntroToOOP.pptIntroToOOP.ppt
IntroToOOP.ppt
 
09slide.ppt
09slide.ppt09slide.ppt
09slide.ppt
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 

Recently uploaded

KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 

Recently uploaded (20)

(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 

Java-operators.ppt

  • 1. April 6, 1998 CS102-02 Lecture 2-1 Java Operators CS 102-02 Lecture 2-1 Being a Smooth Operator
  • 2. April 6, 1998 CS102-02 Lecture 2-1 Operating with Java • Most programming languages have operators – Operators are short-hand symbols for actions = Assign right to left + Add two numbers (or concatenate two strings) – Operators in Java have fixed meaning • No operator overloading • Can’t say: List = List + Item; // Add item to list
  • 3. April 6, 1998 CS102-02 Lecture 2-1 Kinds of Operators Category What it does… Examples Arithmetic Addition, subtraction +, -, / Assignment Set a value to an expression =, +=, &= Conditional Choose one of two values ? : Logical Logical comparisons &&, || Relational Compare values ==, >= Bitwise Move bits within a number <<, >>
  • 4. April 6, 1998 CS102-02 Lecture 2-1 Operator Precedence • Usually things go left-to-right, but there are precedence rules • Nutshell reading lists operators by precedence • Override precedence with ()’s
  • 5. April 6, 1998 CS102-02 Lecture 2-1 Arithmetic Operators • The usual suspects: plus, minus, blah, blah, blah • Modulo/remainder operator
  • 6. April 6, 1998 CS102-02 Lecture 2-1 Modulo Operator • Modulo (or remainder) operator: what’s left over after division 7%3 = 1 198%3 = ?? 6.0%4.0 = 2 • Is it odd or even? • Looping with clock arithmetic – Appointment at 5pm everyday – Baking 217 cakes: step 3 of 7 same as 24 of 28
  • 7. April 6, 1998 CS102-02 Lecture 2-1 Short-Hand Operators • Increment and decrement: ++ and -- – Often need to add or subtract 1 – Pre: Add (subtract) first – Post: Add (subtract) afterwards • Compiler can sometimes optimize
  • 8. April 6, 1998 CS102-02 Lecture 2-1 Testing Out Short-Hand After X Y Whole thing X++ * Y ++X + Y ++X / Y++ Suppose we start with: X = 7; Y = 9; What’s the difference between: X++; ++X;
  • 9. April 6, 1998 CS102-02 Lecture 2-1 Are You My Type? • What’s the type of a result? Expression Result type int * int int float * float ?? int * float ?? int / int ?? • Conversion & promotion
  • 10. April 6, 1998 CS102-02 Lecture 2-1 Assignment Operators • Change the value on the left to the value of the expression on the right If you want to: Try: Assign 8 to Y Y = 8; Add 1 to Y Y++; Assign Y+10 to Y X += 10;
  • 11. April 6, 1998 CS102-02 Lecture 2-1 Works for Strings Too • Strings are “added” (concatenated) with + What is Name after the third line? Name = “Simpson”; First = “Lisa”; Name += First; What’s the result here? Age = 11; Message = “He’s “ + Age + “ years old.”;
  • 12. April 6, 1998 CS102-02 Lecture 2-1 Conditional Operator • Instead of If..Then..Else, use ?: • Takes three arguments in the form: Boolean condition ? If-true : If-false If (Simpson == “Lisa”) { Message = “She’s our favorite!”; } else { Message= “Doh!”; } System.out.println(Message); is the same as…
  • 13. April 6, 1998 CS102-02 Lecture 2-1 Using the Conditional Operator System.out.println(Simpson==“Lisa” ? ”She’s our favorite” :“Doh!”); (The above should be on one line in a real program)
  • 14. April 6, 1998 CS102-02 Lecture 2-1 And, But and Or will get you pretty far.. • Logical operators combine simple expressions to form complex ones • Boolean logic Expression One Expression Two One AND Two One OR Two One XOR Two False False False False False False True False True True True False False True True True True True True False
  • 15. April 6, 1998 CS102-02 Lecture 2-1 Boolean Types • True or false are real values in Java • Some languages just use 0 and not 0 if (y = 7) then … • In Java result of a comparison is Boolean 8 != 9 ?? 8 != 8 ??
  • 16. April 6, 1998 CS102-02 Lecture 2-1 Logical Operators in Java • Translating logic into Java AND && OR || XOR ^ NOT !
  • 17. April 6, 1998 CS102-02 Lecture 2-1 Boolean Expressions • De Morgan’s Laws with Expressions One & Two One OR Two == One AND Two One AND Two == One OR Two • Some handy relations One XOR One == False One OR One == True
  • 18. April 6, 1998 CS102-02 Lecture 2-1 Short-Circuit • Remember: False AND Anything == False True OR Anything == True • Sometimes compiler can short-circuit and skip evaluation of second expression • What if there are side effects?
  • 19. April 6, 1998 CS102-02 Lecture 2-1 Sideline on Side Effects • Side effects are results of expression evaluation other than the expression’s value • Examples X++; – Output: System.out.println(“Howdy!”);
  • 20. April 6, 1998 CS102-02 Lecture 2-1 Short-Circuiting Side Effects • Short-circuiting could prevent a side effect • How do you force the compiler to evaluate a second expression?
  • 21. April 6, 1998 CS102-02 Lecture 2-1 No Short-Circuit Here • Guarantee that the second expression is evaluated AND & OR | XOR ^ (Why is ^ listed here?)
  • 22. April 6, 1998 CS102-02 Lecture 2-1 Relational Operators • Determine the relationship between values • Equality & inequality • Less than, greater than
  • 23. April 6, 1998 CS102-02 Lecture 2-1 (In)Equality • Equality is different from assignment == != = • Most keyboards just have = – Use == for equality – And != for inequality
  • 24. April 6, 1998 CS102-02 Lecture 2-1 Bitwise Operators • Computers are binary creatures: everything’s on or off • For example, computers can’t store decimal numbers so
  • 25. April 6, 1998 CS102-02 Lecture 2-1 Binary Arithmetic • Everything’s in powers of two • Turn 78 into: 128 64 32 16 8 4 2 1 0 1 0 0 1 1 1 0 64 8 4 2
  • 26. April 6, 1998 CS102-02 Lecture 2-1 Accentuate the positive • Computers don’t know about negative numbers • Use the first (leftmost) bit as a sign bit: 1 if negative: -5 is 11111101 0 if positive: +5 is 00000011
  • 27. April 6, 1998 CS102-02 Lecture 2-1 Bitwise is Binary • Work with the bits inside the values • Only good for integral values (integer numbers, bytes and characters) Operator Name Description & AND AND the corresponding bits in the two operands | OR OR the corresponding bits in the two operands ^ XOR XOR the corresponding bits in the two operands << Left shift Shift the bits from right to left >> Right shift with sign extension Shift the bits right and preserve the sign >>> Right shift with zero extension Shift the bits right and always fill in 0’s ~ Complement Switch 0’s and 1’s
  • 28. April 6, 1998 CS102-02 Lecture 2-1 And Shift Your Bits ‘Round and ‘Round • Bitwise AND of 78 and 34 128 64 32 16 8 4 2 1 78 0 1 0 0 1 1 1 0 34 0 0 1 0 0 0 1 0 2 0 0 0 0 0 0 1 0
  • 29. April 6, 1998 CS102-02 Lecture 2-1 Why Bother with Bitwise? • Use numbers not for themselves but for their internal representations • Example: A tic-tac-toe grid might have 0’s for O’s and 1’s for X’s • Just need 9 bits to do the whole table and only 27 bits for 3-D tic-tac-toe
  • 30. April 6, 1998 CS102-02 Lecture 2-1 That’s It for Operators • Operators are key to building large expressions in Java • Know operator precedence (or at least where to look it up) • Next time: Use operators to build expressions for control structures

Editor's Notes

  1. 1
  2. 2