SlideShare a Scribd company logo
Basic Java Programming
Sasidhara Marrapu
Java Operators
 Operators  Lab Problem
 Home Work
Operators:
Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Bitwise Operators
Misc Operators
Mathematical/Arithmetic Operators
Name Meaning Example Result
+ Addition 34 + 1 35
- Subtraction 34.0 – 0.1 33.9
* Multiplication 300 * 30 9000
/ Division 1.0 / 2.0 0.5
% Remainder 20 % 3 2
Mathematical/Arithmetic Operators
public class Example {
public static void main(String[] args) {
int j, k, p, q, r, s, t;
j = 5;
k = 2;
p = j + k;
q = j - k;
r = j * k;
s = j / k;
t = j % k;
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("r = " + r);
System.out.println("s = " + s);
System.out.println("t = " + t);
}
} > java Example
p = 7
q = 3
r = 10
s = 2
t = 1
>
Relational Operators
Primitives
• Greater Than >
• Less Than <
• Greater Than or Equal >=
• Less Than or Equal <=
Primitives or Object References
• Equal (Equivalent) ==
• Not Equal !=
The Result is Always true or false
Relational Operator Examples
public class Example {
public static void main(String[] args) {
int p =2; int q = 2; int r = 3;
Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println("p < r " + (p < r));
System.out.println("p > r " + (p > r));
System.out.println("p == q " + (p == q));
System.out.println("p != q " + (p != q));
System.out.println("i == j " + (i == j));
System.out.println("i != j " + (i != j));
}
} > java Example
p < r true
p > r false
p == q true
p != q false
i == j false
i != j true
>
Assignment Operator (=)
lvalue = rvalue;
• Take the value of the rvalue and
store it in the lvalue.
• The rvalue is any constant,
variable or expression.
• The lvalue is named variable.
w = 10;
x = w;
z = (x - 2)/(2 + 2);
Assignment Operator (=) and
Classes
Date x = new Date();
Date y = new Date();
x = y;
Assignment Operator (=) and
Classes
Date x = new Date();
Date y = new Date();
x = y;
Shorthand Operators
Operator Common Shorthand
+= a = a + b; a += b;
-= a = a - b; a -= b;
*= a = a * b; a *= b;
/= a = a / b; a /= b;
%= a = a % b; a %= b;
Shorthand Operators
public class Example {
public static void main(String[] args) {
int j, p, q, r, s, t;
j = 5;
p = 1; q = 2; r = 3; s = 4; t = 5;
p += j;
q -= j;
r *= j;
s /= j;
t %= j;
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("r = " + r);
System.out.println("s = " + s);
System.out.println("t = " + t);
}
}
> java Example
p = 6
q = -3
r = 15
s = 0
t = 0
>
Shorthand Increment and
Decrement ++ and --
Common Shorthand
a = a + 1; a++; or ++a;
a = a - 1; a--; or --a;
Increment and Decrement
> java example
p = 6
q = 6
j = 7
r = 6
s = 6
>
public class Example {
public static void main(String[] args) {
int j, p, q, r, s;
j = 5;
p = ++j; // j = j + 1; p = j;
System.out.println("p = " + p);
q = j++; // q = j; j = j + 1;
System.out.println("q = " + q);
System.out.println("j = " + j);
r = --j; // j = j -1; r = j;
System.out.println("r = " + r);
s = j--; // s = j; j = j - 1;
System.out.println("s = " + s);
}
}
Logical Operators (boolean)
 Logical AND &&
 Logical OR ||
 Logical NOT !
Logical (&&) Operator Examples
public class Example {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
System.out.println("f && f " + (f && f));
System.out.println("f && t " + (f && t));
System.out.println("t && f " + (t && f));
System.out.println("t && t " + (t && t));
}
} > java Example
f && f false
f && t false
t && f false
t && t true
>
Logical (||) Operator Examples
public class Example {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
System.out.println("f || f " + (f || f));
System.out.println("f || t " + (f || t));
System.out.println("t || f " + (t || f));
System.out.println("t || t " + (t || t));
}
}
> java Example
f || f false
f || t true
t || f true
t || t true
>
Logical (!) Operator Examples
public class Example {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
System.out.println("!f " + !f);
System.out.println("!t " + !t);
}
} > java Example
!f true
!t false
>
Logical Operator Examples
Short Circuiting with &&
public class Example {
public static void main(String[] args) {
boolean b;
int j, k;
j = 0; k = 0;
b = ( j++ == k ) && ( j == ++k );
System.out.println("b, j, k " + b + ", " + j + ", " + k);
j = 0; k = 0;
b = ( j++ != k ) && ( j == ++k );
System.out.println("b, j, k " + b + ", " + j + ", " + k);
}
}
> java Example
b, j, k true 1, 1
> java Example
b, j, k true 1, 1
b, j, k false 1, 0
>
Logical Operator Examples
Short Circuiting with ||
public class Example {
public static void main(String[] args) {
boolean b;
int j, k;
j = 0; k = 0;
b = ( j++ == k ) || ( j == ++k );
System.out.println("b, j, k " + b + ", " + j + ", " + k);
j = 0; k = 0;
b = ( j++ != k ) || ( j == ++k );
System.out.println("b, j, k " + b + ", " + j + ", " + k);
}
}
> java Example
b, j, k true 1, 0
> java Example
b, j, k true 1, 0
b, j, k true 1, 1
>
Bitwise Operators
• AND &
• OR |
• XOR ^
• NOT ~
Bitwise Operator Examples
class BitwiseOR {
public static void main(String[] args) {
int number1 = 12, number2 = 25, result;
result = number1 | number2;
System.out.println(result);
}
}
29
Bitwise Operator Examples
29
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)
Bitwise Operator Examples
public class Example {
public static void main(String[] args) {
int a = 10; // 00001010 = 10
int b = 12; // 00001100 = 12
int and, or, xor, na;
and = a & b; // 00001000 = 8
or = a | b; // 00001110 = 14
xor = a ^ b; // 00000110 = 6
na = ~a; // 11110101 = -11
System.out.println("and " + and);
System.out.println("or " + or);
System.out.println("xor " + xor);
System.out.println("na " + na);
}
} > java Example
and 8
or 14
xor 6
na -11
>
Ternary Operator
? :
If true this expression is
evaluated and becomes the
value entire expression.
Any expression that evaluates
to a boolean value.
If false this expression is
evaluated and becomes the
value entire expression.
boolean_expression ? expression_1 : expression_2
Ternary ( ? : ) Operator Examples
public class Example {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
System.out.println("t?true:false "+(t ? true : false ));
System.out.println("t?1:2 "+(t ? 1 : 2 ));
System.out.println("f?true:false "+(f ? true : false ));
System.out.println("f?1:2 "+(f ? 1 : 2 ));
}
}
> java Example
t?true:false true
t?1:2 1
f?true:false false
f?1:2 2
>
String (+) Operator
String Concatenation
"Now is " + "the time."
"Now is the time."
String (+) Operator
Automatic Conversion to a String
If either expression_1
If either expression_1 or expression_2 evaluates
to a string the other will be converted to a string
if needed. The result will be their concatenation.
expression_1 + expression_2
String (+) Operator
Automatic Conversion with Primitives
"The number is " + 4
"The number is " + "4"
"The number is 4"
String (+) Operator
Automatic Conversion with Objects
"Today is " + new Date()
"Today is " + "Wed 27 22:12;26 CST 2000"
"Today is Wed 27 22:12;26 CST 2000"
"Today is " + new Date().toString()
Instance of Operator
• This operator is used only for object reference
variables.
• The operator checks whether the object is of a
particular type (class type or interface type).
instanceof operator is written as
( Object reference variable ) instanceof (class/interface type)
InstanceOf Examples
public class Example {
public static void main(String args[]) {
String name = "James";
// following will return true since name is type of String
boolean result = name instanceof String;
System.out.println( result );
}
}
> true
Operator Precedence
+ - ++ -- ! ~ ()
* / %
+ -
<< >> >>>
> < >= <= instanceof
== !=
& | ^
&& ||
?:
= (and += etc.)
Unary
Arithmetic
Shift
Comparison
Logical Bit
Boolean
Ternary
Assignment
Home Work

More Related Content

What's hot

Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
Vadim Dubs
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Ganesh Samarthyam
 
Chapter 2 Method in Java OOP
Chapter 2   Method in Java OOPChapter 2   Method in Java OOP
Chapter 2 Method in Java OOP
Khirulnizam Abd Rahman
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
Mario Fusco
 
Chapter 4 - Classes in Java
Chapter 4 - Classes in JavaChapter 4 - Classes in Java
Chapter 4 - Classes in Java
Khirulnizam Abd Rahman
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
Intro C# Book
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
Khirulnizam Abd Rahman
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario Fusco
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMMario Fusco
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
Upender Upr
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
Intro C# Book
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
Intro C# Book
 
Class method
Class methodClass method
Class method
kamal kotecha
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
Intro C# Book
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
Functional programming
Functional programmingFunctional programming
Functional programming
Prashant Kalkar
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
Prashant Kalkar
 

What's hot (20)

Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
 
Chapter 2 Method in Java OOP
Chapter 2   Method in Java OOPChapter 2   Method in Java OOP
Chapter 2 Method in Java OOP
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Chapter 4 - Classes in Java
Chapter 4 - Classes in JavaChapter 4 - Classes in Java
Chapter 4 - Classes in Java
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
Class method
Class methodClass method
Class method
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 

Similar to Pj01 4-operators and control flow

object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
MSohaib24
 
Operators
OperatorsOperators
Operators
Daman Toor
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
henokmetaferia1
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
mcollison
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
Logan Chien
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
franciscoortin
 
Effective Java - Still Effective After All These Years
Effective Java - Still Effective After All These YearsEffective Java - Still Effective After All These Years
Effective Java - Still Effective After All These Years
Marakana Inc.
 
operators.ppt
operators.pptoperators.ppt
operators.ppt
SharukSharuk3
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
rinkugupta37
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 
Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시
Junha Jang
 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query Expressions
Chris Eargle
 

Similar to Pj01 4-operators and control flow (20)

object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Operators
OperatorsOperators
Operators
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
Effective Java - Still Effective After All These Years
Effective Java - Still Effective After All These YearsEffective Java - Still Effective After All These Years
Effective Java - Still Effective After All These Years
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
operators.ppt
operators.pptoperators.ppt
operators.ppt
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
 
05 operators
05   operators05   operators
05 operators
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시
 
C#, What Is Next?
C#, What Is Next?C#, What Is Next?
C#, What Is Next?
 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query Expressions
 

Recently uploaded

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 

Pj01 4-operators and control flow

  • 2. Java Operators  Operators  Lab Problem  Home Work
  • 3. Operators: Arithmetic Operators Relational Operators Assignment Operators Logical Operators Bitwise Operators Misc Operators
  • 4. Mathematical/Arithmetic Operators Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2
  • 5. Mathematical/Arithmetic Operators public class Example { public static void main(String[] args) { int j, k, p, q, r, s, t; j = 5; k = 2; p = j + k; q = j - k; r = j * k; s = j / k; t = j % k; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("t = " + t); } } > java Example p = 7 q = 3 r = 10 s = 2 t = 1 >
  • 6. Relational Operators Primitives • Greater Than > • Less Than < • Greater Than or Equal >= • Less Than or Equal <= Primitives or Object References • Equal (Equivalent) == • Not Equal != The Result is Always true or false
  • 7. Relational Operator Examples public class Example { public static void main(String[] args) { int p =2; int q = 2; int r = 3; Integer i = new Integer(10); Integer j = new Integer(10); System.out.println("p < r " + (p < r)); System.out.println("p > r " + (p > r)); System.out.println("p == q " + (p == q)); System.out.println("p != q " + (p != q)); System.out.println("i == j " + (i == j)); System.out.println("i != j " + (i != j)); } } > java Example p < r true p > r false p == q true p != q false i == j false i != j true >
  • 8. Assignment Operator (=) lvalue = rvalue; • Take the value of the rvalue and store it in the lvalue. • The rvalue is any constant, variable or expression. • The lvalue is named variable. w = 10; x = w; z = (x - 2)/(2 + 2);
  • 9. Assignment Operator (=) and Classes Date x = new Date(); Date y = new Date(); x = y;
  • 10. Assignment Operator (=) and Classes Date x = new Date(); Date y = new Date(); x = y;
  • 11. Shorthand Operators Operator Common Shorthand += a = a + b; a += b; -= a = a - b; a -= b; *= a = a * b; a *= b; /= a = a / b; a /= b; %= a = a % b; a %= b;
  • 12. Shorthand Operators public class Example { public static void main(String[] args) { int j, p, q, r, s, t; j = 5; p = 1; q = 2; r = 3; s = 4; t = 5; p += j; q -= j; r *= j; s /= j; t %= j; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("t = " + t); } } > java Example p = 6 q = -3 r = 15 s = 0 t = 0 >
  • 13. Shorthand Increment and Decrement ++ and -- Common Shorthand a = a + 1; a++; or ++a; a = a - 1; a--; or --a;
  • 14. Increment and Decrement > java example p = 6 q = 6 j = 7 r = 6 s = 6 > public class Example { public static void main(String[] args) { int j, p, q, r, s; j = 5; p = ++j; // j = j + 1; p = j; System.out.println("p = " + p); q = j++; // q = j; j = j + 1; System.out.println("q = " + q); System.out.println("j = " + j); r = --j; // j = j -1; r = j; System.out.println("r = " + r); s = j--; // s = j; j = j - 1; System.out.println("s = " + s); } }
  • 15. Logical Operators (boolean)  Logical AND &&  Logical OR ||  Logical NOT !
  • 16. Logical (&&) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("f && f " + (f && f)); System.out.println("f && t " + (f && t)); System.out.println("t && f " + (t && f)); System.out.println("t && t " + (t && t)); } } > java Example f && f false f && t false t && f false t && t true >
  • 17. Logical (||) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("f || f " + (f || f)); System.out.println("f || t " + (f || t)); System.out.println("t || f " + (t || f)); System.out.println("t || t " + (t || t)); } } > java Example f || f false f || t true t || f true t || t true >
  • 18. Logical (!) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("!f " + !f); System.out.println("!t " + !t); } } > java Example !f true !t false >
  • 19. Logical Operator Examples Short Circuiting with && public class Example { public static void main(String[] args) { boolean b; int j, k; j = 0; k = 0; b = ( j++ == k ) && ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); j = 0; k = 0; b = ( j++ != k ) && ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); } } > java Example b, j, k true 1, 1 > java Example b, j, k true 1, 1 b, j, k false 1, 0 >
  • 20. Logical Operator Examples Short Circuiting with || public class Example { public static void main(String[] args) { boolean b; int j, k; j = 0; k = 0; b = ( j++ == k ) || ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); j = 0; k = 0; b = ( j++ != k ) || ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); } } > java Example b, j, k true 1, 0 > java Example b, j, k true 1, 0 b, j, k true 1, 1 >
  • 21. Bitwise Operators • AND & • OR | • XOR ^ • NOT ~
  • 22. Bitwise Operator Examples class BitwiseOR { public static void main(String[] args) { int number1 = 12, number2 = 25, result; result = number1 | number2; System.out.println(result); } } 29
  • 23. Bitwise Operator Examples 29 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ________ 00011101 = 29 (In decimal)
  • 24. Bitwise Operator Examples public class Example { public static void main(String[] args) { int a = 10; // 00001010 = 10 int b = 12; // 00001100 = 12 int and, or, xor, na; and = a & b; // 00001000 = 8 or = a | b; // 00001110 = 14 xor = a ^ b; // 00000110 = 6 na = ~a; // 11110101 = -11 System.out.println("and " + and); System.out.println("or " + or); System.out.println("xor " + xor); System.out.println("na " + na); } } > java Example and 8 or 14 xor 6 na -11 >
  • 25. Ternary Operator ? : If true this expression is evaluated and becomes the value entire expression. Any expression that evaluates to a boolean value. If false this expression is evaluated and becomes the value entire expression. boolean_expression ? expression_1 : expression_2
  • 26. Ternary ( ? : ) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("t?true:false "+(t ? true : false )); System.out.println("t?1:2 "+(t ? 1 : 2 )); System.out.println("f?true:false "+(f ? true : false )); System.out.println("f?1:2 "+(f ? 1 : 2 )); } } > java Example t?true:false true t?1:2 1 f?true:false false f?1:2 2 >
  • 27. String (+) Operator String Concatenation "Now is " + "the time." "Now is the time."
  • 28. String (+) Operator Automatic Conversion to a String If either expression_1 If either expression_1 or expression_2 evaluates to a string the other will be converted to a string if needed. The result will be their concatenation. expression_1 + expression_2
  • 29. String (+) Operator Automatic Conversion with Primitives "The number is " + 4 "The number is " + "4" "The number is 4"
  • 30. String (+) Operator Automatic Conversion with Objects "Today is " + new Date() "Today is " + "Wed 27 22:12;26 CST 2000" "Today is Wed 27 22:12;26 CST 2000" "Today is " + new Date().toString()
  • 31. Instance of Operator • This operator is used only for object reference variables. • The operator checks whether the object is of a particular type (class type or interface type). instanceof operator is written as ( Object reference variable ) instanceof (class/interface type)
  • 32. InstanceOf Examples public class Example { public static void main(String args[]) { String name = "James"; // following will return true since name is type of String boolean result = name instanceof String; System.out.println( result ); } } > true
  • 33. Operator Precedence + - ++ -- ! ~ () * / % + - << >> >>> > < >= <= instanceof == != & | ^ && || ?: = (and += etc.) Unary Arithmetic Shift Comparison Logical Bit Boolean Ternary Assignment