SlideShare a Scribd company logo
Programming in Java
Lecture 3: Operators and Expressions
Contents
• Operators in Java
• Arithmetic Operators
• Bitwise Operators
• Relational Operators
• Logical Operators
Operators in Java
• Java’s operators can be grouped into
following four categories:
1. Arithmetic
2. Bitwise
3. Relational
4. Logical.
Arithmetic Operators
• used in mathematical expressions.
• operands of the arithmetic operators must be of a
numeric type.
• most operators in Java work just like they do in
C/C++.
• We can not use them on boolean types, but we can
use them on char types, since the char type in Java is,
essentially, a subset of int.
Arithmetic Operators
Operator Result
• + Addition
• - Subtraction (also unary minus)
• * Multiplication
• / Division
• % Modulus
• ++ Increment
• += Addition assignment
• -= Subtraction assignment
• *= Multiplication assignment
• /= Division assignment
• %= Modulus assignment
• - - Decrement
Modulus Operator (%)
• returns the remainder of a division operation.
• can be applied to floating-point types as well as integer types.
• This differs from C/C++, in which the % can only be applied to
integer types.
Example: Modulus.java
Arithmetic Assignment Operators
• used to combine an arithmetic operation with an assignment.
Thus, the statements of the form
var = var op expression;
can be rewritten as
var op= expression;
• In Java, statements like
a = a + 5;
can be written as
a += 5;
Similarly:
b = b % 3; can be written as b %= 3;
Increment and Decrement
• ++ and the - - are Java's increment and decrement operators.
• The increment operator increases its operand by one.
• x++; is equivalent to x = x + 1;
• The decrement operator decreases its operand by one.
• y--; is equivalent to y = y – 1;
• They can appear both in
• postfix form, where they follow the operand, and
• prefix form, where they precede the operand.
• In the prefix form, the operand is incremented or
decremented before the value is obtained for use in the
expression.
Example: x = 19; y = ++x;
Output: y = 20 and x = 20
• In postfix form, the previous value is obtained for use
in the expression, and then the operand is modified.
Example: x = 19; y = x++;
Output: y = 19 and x = 20
Bitwise Operators
• These operators act upon the individual bits of their
operands.
• can be applied to the integer types, long, int, short,
char, and byte.
Operator Result
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
Bitwise Logical Operators
• The bitwise logical operators are
• ~ (NOT)
• & (AND)
• | (OR)
• ^ (XOR)
• Example:
The Left Shift
• The left shift operator,<<, shifts all of the bits in
a value to the left a specified number of times.
value << num
• Example:
• 01000001 65
• << 2
• 00000100 4
The Right Shift
• The right shift operator, >>, shifts all of the bits
in a value to the right a specified number of times.
value >> num
• Example:
• 00100011 35
• >> 2
• 00001000 8
• When we are shifting right, the top (leftmost)
bits exposed by the right shift are filled in with
the previous contents of the top bit.
• This is called sign extension and serves to
preserve the sign of negative numbers when you
shift them right.
The Unsigned Right Shift
• In these cases, to shift a zero into the high-order bit no
matter what its initial value was. This is known as an
unsigned shift.
• To accomplish this, we will use Java’s unsigned, shift-right
operator, >>>, which always shifts zeros into the high-order
bit.
• Example:
• 11111111 11111111 11111111 11111111 –1 in
binary as an int
• >>>24
• 00000000 00000000 00000000 11111111 255 in
binary as an int
Bitwise Operator Compound Assignments
• combines the assignment with the bitwise operation.
• Similar to the algebraic operators
• For example, the following two statements, which shift the
value in a right by four bits, are equivalent:
a = a >> 4;
a >>= 4;
• Likewise,
a = a | b;
a |= b;
Relational Operators
• The relational operators determine the relationship that one
operand has to the other.
• Relational operators determine equality and ordering.
• The outcome of relational operations is a boolean value.
Operator Function
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
• The result produced by a relational operator is a boolean
value.
• For example, the following code fragment is perfectly
valid:
• int a = 4;
• int b = 1;
• boolean c = a < b;
•In this case, the result of a<b (which is false) is stored in c.
Boolean Logical Operators
• The Boolean logical operators shown here operate
only on boolean operands.
Operator Result
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else
•All of the binary logical operators combine two
boolean values to form a resultant boolean value.
•The logical Boolean operators,&, |, and ^, operate
on boolean values in the same way that they operate
on the bits of an integer.
•The following table shows the effect of each
logical operation:
A B A | B A & B A ^ B ~ A
False False False False False True
True False True False True False
False True True False True True
True True True True False False
Short-Circuit Logical Operators
• These are secondary versions of the Boolean AND and OR operators,
and are known as short-circuit logical operators.
• OR operator results in true when A is true , no matter what B is.
• Similarly, AND operator results in false when A is false, no matter what
B is.
• If we use the || and && forms, rather than the | and & forms of these
operators, Java will not bother to evaluate the right-hand operand when the
outcome of the expression can be determined by the left operand alone.
•This is very useful when the right-hand operand depends on the value of
the left one in order to function properly.
• For example
if (denom != 0 && num / denom > 10)
• the following code fragment shows how you can take
advantage of short-circuit logical evaluation to be sure that a
division operation will be valid before evaluating it.
• circuit form of AND (&&) is used, there is no risk of
causing a run-time exception when denom is zero.
• If this line of code were written using the single & version
of AND, both sides would be evaluated, causing a run-time
exception when denom is zero.
The Assignment Operator
• The assignment operator is the single equal sign, =.
• The assignment operator works in Java much as it does in
any other computer language.
var = expression ;
• Here, the type of var must be compatible with the type of
expression.
• The assignment operator allows to create a chain of
assignments.
• For example:
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
• This fragment sets the variables x , y, and z to 100 using a
single statement.
The ? Operator
• Java includes a special ternary (three-way)operator, ?, that
can replace certain types of if-then-else statements.
•The ? has this general form:
expression1 ? expression2 : expression3
• Here,expression1 can be any expression that evaluates to a
boolean value.
• If expression1 is true , then expression2 is evaluated;
otherwise, expression3 is evaluated.
• The result of the? operation is that of the expression
evaluated.
• Both expression2 and expression3 are required to return the
same type, which can’t be void.
• Example: TestTernary.java
ratio = denom == 0 ? 0 : num / denom ;
•When Java evaluates this assignment expression, it first looks
at the expression to the left of the question mark.
• If denom equals zero, then the expression between the
question mark and the colon is evaluated and used as the
value of the entire ? expression.
•If denom does not equal zero, then the expression after the
colon is evaluated and used for the value of the entire ?
expression.
•The result produced by the? operator is then assigned to ratio.
Operator Precedence
Highest
( ) [] .
++ -- %
+ -
>> >>> <<
> >= < <=
== !=
&
^
|
&&
||
?:
= Op=
Lowest
Questions

More Related Content

What's hot

OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++
ANANT VYAS
 
9 cm604.10
9 cm604.109 cm604.10
9 cm604.10
myrajendra
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
Mohammad Imam Hossain
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and Associativity
Nicole Ynne Estabillo
 
Shubhrat operator &amp; expression
Shubhrat operator &amp; expressionShubhrat operator &amp; expression
Shubhrat operator &amp; expression
Shubhrat Mishra
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
Aakash Singh
 
Operators in java
Operators in javaOperators in java
Operators in java
AbhishekMondal42
 
Nullable type in C#
Nullable type in C#Nullable type in C#
Nullable type in C#
ZohaibAnwerChaudhry
 
Java 8 Functional Programming - I
Java 8 Functional Programming - IJava 8 Functional Programming - I
Java 8 Functional Programming - I
Ugur Yeter
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: Functions
Vivek Bhargav
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
Darshan Patel
 
Oop using JAVA
Oop using JAVAOop using JAVA
Oop using JAVA
umardanjumamaiwada
 
Type conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programmingType conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programming
Dhrumil Panchal
 
C++
C++ C++
Regular expression automata
Regular expression automataRegular expression automata
Regular expression automata
성욱 유
 
Operators
OperatorsOperators

What's hot (16)

OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++
 
9 cm604.10
9 cm604.109 cm604.10
9 cm604.10
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and Associativity
 
Shubhrat operator &amp; expression
Shubhrat operator &amp; expressionShubhrat operator &amp; expression
Shubhrat operator &amp; expression
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Nullable type in C#
Nullable type in C#Nullable type in C#
Nullable type in C#
 
Java 8 Functional Programming - I
Java 8 Functional Programming - IJava 8 Functional Programming - I
Java 8 Functional Programming - I
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: Functions
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 
Oop using JAVA
Oop using JAVAOop using JAVA
Oop using JAVA
 
Type conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programmingType conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programming
 
C++
C++ C++
C++
 
Regular expression automata
Regular expression automataRegular expression automata
Regular expression automata
 
Operators
OperatorsOperators
Operators
 

Viewers also liked

Riesgos psicosociales junto con la higiene industrial
Riesgos psicosociales junto con la higiene industrialRiesgos psicosociales junto con la higiene industrial
Riesgos psicosociales junto con la higiene industrial
marina18081986
 
consideración para el cálculo de las asignaciones en una nomina
consideración para el cálculo de las asignaciones en una nominaconsideración para el cálculo de las asignaciones en una nomina
consideración para el cálculo de las asignaciones en una nomina
Frank Ibarra
 
Linkedln_gabysoto
Linkedln_gabysotoLinkedln_gabysoto
Linkedln_gabysoto
gasomi
 
2015年4月TDOH幹部訓
2015年4月TDOH幹部訓2015年4月TDOH幹部訓
2015年4月TDOH幹部訓
Vincent Chi
 
La eutanasia
La eutanasiaLa eutanasia
La eutanasia
SandyGarrido
 
Componentes del proyecto
Componentes del proyectoComponentes del proyecto
Componentes del proyecto
Yurani Arévalo Bernal
 
estadistica
estadisticaestadistica
estadistica
Nina Sayay
 
Thread presentation
Thread presentationThread presentation
Thread presentation
AAshish Ojha
 
Taraz state pedagogical institute report 3 dec 2014
Taraz state pedagogical institute report 3 dec 2014Taraz state pedagogical institute report 3 dec 2014
Taraz state pedagogical institute report 3 dec 2014
Breach_P
 
Razas bovinas
Razas bovinasRazas bovinas
Razas bovinas
mvz20
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
Nishant Mevawala
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
Mohammed625
 
multi threading
multi threadingmulti threading
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
teach4uin
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Self incompatibility ppt
Self incompatibility pptSelf incompatibility ppt
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
Robert MacLean
 

Viewers also liked (20)

Riesgos psicosociales junto con la higiene industrial
Riesgos psicosociales junto con la higiene industrialRiesgos psicosociales junto con la higiene industrial
Riesgos psicosociales junto con la higiene industrial
 
consideración para el cálculo de las asignaciones en una nomina
consideración para el cálculo de las asignaciones en una nominaconsideración para el cálculo de las asignaciones en una nomina
consideración para el cálculo de las asignaciones en una nomina
 
Linkedln_gabysoto
Linkedln_gabysotoLinkedln_gabysoto
Linkedln_gabysoto
 
2015年4月TDOH幹部訓
2015年4月TDOH幹部訓2015年4月TDOH幹部訓
2015年4月TDOH幹部訓
 
La eutanasia
La eutanasiaLa eutanasia
La eutanasia
 
Componentes del proyecto
Componentes del proyectoComponentes del proyecto
Componentes del proyecto
 
estadistica
estadisticaestadistica
estadistica
 
Thread presentation
Thread presentationThread presentation
Thread presentation
 
Taraz state pedagogical institute report 3 dec 2014
Taraz state pedagogical institute report 3 dec 2014Taraz state pedagogical institute report 3 dec 2014
Taraz state pedagogical institute report 3 dec 2014
 
Razas bovinas
Razas bovinasRazas bovinas
Razas bovinas
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
multi threading
multi threadingmulti threading
multi threading
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Self incompatibility ppt
Self incompatibility pptSelf incompatibility ppt
Self incompatibility ppt
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 

Similar to L3 operators

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
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
SKUP1
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
LECO9
 
Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
Munazza-Mah-Jabeen
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
OCA JAVA - 3 Programming with Java Operators
 OCA JAVA - 3 Programming with Java Operators OCA JAVA - 3 Programming with Java Operators
OCA JAVA - 3 Programming with Java Operators
Fernando Gil
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
Anusuya123
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
C Session 2.pptx for engninering students
C Session 2.pptx for engninering studentsC Session 2.pptx for engninering students
C Session 2.pptx for engninering students
nraj02252
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
TKSanthoshRao
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
İbrahim Kürce
 
IOS Swift Language 3rd tutorial
IOS Swift Language 3rd tutorialIOS Swift Language 3rd tutorial
IOS Swift Language 3rd tutorial
Hassan A-j
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
Thesis Scientist Private Limited
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
michaelaaron25322
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
sanya6900
 
Operators in Python Arithmetic Operators
Operators in Python Arithmetic OperatorsOperators in Python Arithmetic Operators
Operators in Python Arithmetic Operators
ramireddyobulakondar
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
AllanGuevarra1
 
Py-Slides-2 (1).ppt
Py-Slides-2 (1).pptPy-Slides-2 (1).ppt
Py-Slides-2 (1).ppt
KalaiVani395886
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
TejaValmiki
 

Similar to L3 operators (20)

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
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
OCA JAVA - 3 Programming with Java Operators
 OCA JAVA - 3 Programming with Java Operators OCA JAVA - 3 Programming with Java Operators
OCA JAVA - 3 Programming with Java Operators
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
Chap 3(operator expression)
 
C Session 2.pptx for engninering students
C Session 2.pptx for engninering studentsC Session 2.pptx for engninering students
C Session 2.pptx for engninering students
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
IOS Swift Language 3rd tutorial
IOS Swift Language 3rd tutorialIOS Swift Language 3rd tutorial
IOS Swift Language 3rd tutorial
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
Operators in Python Arithmetic Operators
Operators in Python Arithmetic OperatorsOperators in Python Arithmetic Operators
Operators in Python Arithmetic Operators
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
 
Py-Slides-2 (1).ppt
Py-Slides-2 (1).pptPy-Slides-2 (1).ppt
Py-Slides-2 (1).ppt
 
Py-Slides-2.ppt
Py-Slides-2.pptPy-Slides-2.ppt
Py-Slides-2.ppt
 

More from teach4uin

Controls
ControlsControls
Controls
teach4uin
 
validation
validationvalidation
validation
teach4uin
 
validation
validationvalidation
validation
teach4uin
 
Master pages
Master pagesMaster pages
Master pages
teach4uin
 
.Net framework
.Net framework.Net framework
.Net framework
teach4uin
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
teach4uin
 
Css1
Css1Css1
Css1
teach4uin
 
Code model
Code modelCode model
Code model
teach4uin
 
Asp db
Asp dbAsp db
Asp db
teach4uin
 
State management
State managementState management
State management
teach4uin
 
security configuration
security configurationsecurity configuration
security configuration
teach4uin
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
teach4uin
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
teach4uin
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
teach4uin
 
.Net overview
.Net overview.Net overview
.Net overview
teach4uin
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
teach4uin
 
enums
enumsenums
enums
teach4uin
 
memory
memorymemory
memory
teach4uin
 
array
arrayarray
array
teach4uin
 
storage clas
storage classtorage clas
storage clas
teach4uin
 

More from teach4uin (20)

Controls
ControlsControls
Controls
 
validation
validationvalidation
validation
 
validation
validationvalidation
validation
 
Master pages
Master pagesMaster pages
Master pages
 
.Net framework
.Net framework.Net framework
.Net framework
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Css1
Css1Css1
Css1
 
Code model
Code modelCode model
Code model
 
Asp db
Asp dbAsp db
Asp db
 
State management
State managementState management
State management
 
security configuration
security configurationsecurity configuration
security configuration
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
 
.Net overview
.Net overview.Net overview
.Net overview
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
enums
enumsenums
enums
 
memory
memorymemory
memory
 
array
arrayarray
array
 
storage clas
storage classtorage clas
storage clas
 

Recently uploaded

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 

Recently uploaded (20)

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 

L3 operators

  • 1. Programming in Java Lecture 3: Operators and Expressions
  • 2. Contents • Operators in Java • Arithmetic Operators • Bitwise Operators • Relational Operators • Logical Operators
  • 3. Operators in Java • Java’s operators can be grouped into following four categories: 1. Arithmetic 2. Bitwise 3. Relational 4. Logical.
  • 4. Arithmetic Operators • used in mathematical expressions. • operands of the arithmetic operators must be of a numeric type. • most operators in Java work just like they do in C/C++. • We can not use them on boolean types, but we can use them on char types, since the char type in Java is, essentially, a subset of int.
  • 5. Arithmetic Operators Operator Result • + Addition • - Subtraction (also unary minus) • * Multiplication • / Division • % Modulus • ++ Increment • += Addition assignment • -= Subtraction assignment • *= Multiplication assignment • /= Division assignment • %= Modulus assignment • - - Decrement
  • 6. Modulus Operator (%) • returns the remainder of a division operation. • can be applied to floating-point types as well as integer types. • This differs from C/C++, in which the % can only be applied to integer types. Example: Modulus.java
  • 7. Arithmetic Assignment Operators • used to combine an arithmetic operation with an assignment. Thus, the statements of the form var = var op expression; can be rewritten as var op= expression; • In Java, statements like a = a + 5; can be written as a += 5; Similarly: b = b % 3; can be written as b %= 3;
  • 8. Increment and Decrement • ++ and the - - are Java's increment and decrement operators. • The increment operator increases its operand by one. • x++; is equivalent to x = x + 1; • The decrement operator decreases its operand by one. • y--; is equivalent to y = y – 1; • They can appear both in • postfix form, where they follow the operand, and • prefix form, where they precede the operand.
  • 9. • In the prefix form, the operand is incremented or decremented before the value is obtained for use in the expression. Example: x = 19; y = ++x; Output: y = 20 and x = 20 • In postfix form, the previous value is obtained for use in the expression, and then the operand is modified. Example: x = 19; y = x++; Output: y = 19 and x = 20
  • 10. Bitwise Operators • These operators act upon the individual bits of their operands. • can be applied to the integer types, long, int, short, char, and byte.
  • 11. Operator Result ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR >> Shift right >>> Shift right zero fill << Shift left &= Bitwise AND assignment |= Bitwise OR assignment ^= Bitwise exclusive OR assignment >>= Shift right assignment >>>= Shift right zero fill assignment <<= Shift left assignment
  • 12. Bitwise Logical Operators • The bitwise logical operators are • ~ (NOT) • & (AND) • | (OR) • ^ (XOR) • Example:
  • 13. The Left Shift • The left shift operator,<<, shifts all of the bits in a value to the left a specified number of times. value << num • Example: • 01000001 65 • << 2 • 00000100 4
  • 14. The Right Shift • The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. value >> num • Example: • 00100011 35 • >> 2 • 00001000 8
  • 15. • When we are shifting right, the top (leftmost) bits exposed by the right shift are filled in with the previous contents of the top bit. • This is called sign extension and serves to preserve the sign of negative numbers when you shift them right.
  • 16. The Unsigned Right Shift • In these cases, to shift a zero into the high-order bit no matter what its initial value was. This is known as an unsigned shift. • To accomplish this, we will use Java’s unsigned, shift-right operator, >>>, which always shifts zeros into the high-order bit. • Example: • 11111111 11111111 11111111 11111111 –1 in binary as an int • >>>24 • 00000000 00000000 00000000 11111111 255 in binary as an int
  • 17. Bitwise Operator Compound Assignments • combines the assignment with the bitwise operation. • Similar to the algebraic operators • For example, the following two statements, which shift the value in a right by four bits, are equivalent: a = a >> 4; a >>= 4; • Likewise, a = a | b; a |= b;
  • 18. Relational Operators • The relational operators determine the relationship that one operand has to the other. • Relational operators determine equality and ordering. • The outcome of relational operations is a boolean value. Operator Function == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to
  • 19. • The result produced by a relational operator is a boolean value. • For example, the following code fragment is perfectly valid: • int a = 4; • int b = 1; • boolean c = a < b; •In this case, the result of a<b (which is false) is stored in c.
  • 20. Boolean Logical Operators • The Boolean logical operators shown here operate only on boolean operands. Operator Result & Logical AND | Logical OR ^ Logical XOR (exclusive OR) || Short-circuit OR && Short-circuit AND ! Logical unary NOT &= AND assignment |= OR assignment ^= XOR assignment == Equal to != Not equal to ?: Ternary if-then-else
  • 21. •All of the binary logical operators combine two boolean values to form a resultant boolean value. •The logical Boolean operators,&, |, and ^, operate on boolean values in the same way that they operate on the bits of an integer. •The following table shows the effect of each logical operation: A B A | B A & B A ^ B ~ A False False False False False True True False True False True False False True True False True True True True True True False False
  • 22. Short-Circuit Logical Operators • These are secondary versions of the Boolean AND and OR operators, and are known as short-circuit logical operators. • OR operator results in true when A is true , no matter what B is. • Similarly, AND operator results in false when A is false, no matter what B is. • If we use the || and && forms, rather than the | and & forms of these operators, Java will not bother to evaluate the right-hand operand when the outcome of the expression can be determined by the left operand alone. •This is very useful when the right-hand operand depends on the value of the left one in order to function properly.
  • 23. • For example if (denom != 0 && num / denom > 10) • the following code fragment shows how you can take advantage of short-circuit logical evaluation to be sure that a division operation will be valid before evaluating it. • circuit form of AND (&&) is used, there is no risk of causing a run-time exception when denom is zero. • If this line of code were written using the single & version of AND, both sides would be evaluated, causing a run-time exception when denom is zero.
  • 24. The Assignment Operator • The assignment operator is the single equal sign, =. • The assignment operator works in Java much as it does in any other computer language. var = expression ; • Here, the type of var must be compatible with the type of expression. • The assignment operator allows to create a chain of assignments. • For example: int x, y, z; x = y = z = 100; // set x, y, and z to 100 • This fragment sets the variables x , y, and z to 100 using a single statement.
  • 25. The ? Operator • Java includes a special ternary (three-way)operator, ?, that can replace certain types of if-then-else statements. •The ? has this general form: expression1 ? expression2 : expression3 • Here,expression1 can be any expression that evaluates to a boolean value. • If expression1 is true , then expression2 is evaluated; otherwise, expression3 is evaluated. • The result of the? operation is that of the expression evaluated. • Both expression2 and expression3 are required to return the same type, which can’t be void.
  • 26. • Example: TestTernary.java ratio = denom == 0 ? 0 : num / denom ; •When Java evaluates this assignment expression, it first looks at the expression to the left of the question mark. • If denom equals zero, then the expression between the question mark and the colon is evaluated and used as the value of the entire ? expression. •If denom does not equal zero, then the expression after the colon is evaluated and used for the value of the entire ? expression. •The result produced by the? operator is then assigned to ratio.
  • 27. Operator Precedence Highest ( ) [] . ++ -- % + - >> >>> << > >= < <= == != & ^ | && || ?: = Op= Lowest

Editor's Notes

  1. 이 TP에서는 자바의 생성 배경과 그 동안 자바가 어떻게 발전해 왔는지에 대해 설명한다.