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

PPT
Java operators
PDF
Laziness, trampolines, monoids and other functional amenities: this is not yo...
PDF
Lazy java
PPT
Chapter 2 Java Methods
PDF
FP in Java - Project Lambda and beyond
PPTX
Java 7, 8 & 9 - Moving the language forward
PPTX
Is java8a truefunctionallanguage
PPTX
Is java8 a true functional programming language
Java operators
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Lazy java
Chapter 2 Java Methods
FP in Java - Project Lambda and beyond
Java 7, 8 & 9 - Moving the language forward
Is java8a truefunctionallanguage
Is java8 a true functional programming language

What's hot (20)

PDF
Javaz. Functional design in Java 8.
PDF
From object oriented to functional domain modeling
PDF
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
PPT
Chapter 2 Method in Java OOP
PDF
Java 8 Workshop
PPT
Chapter 4 - Classes in Java
PPTX
16. Java stacks and queues
PPT
Chapter 3 Arrays in Java
PDF
OOP and FP - Become a Better Programmer
PDF
Thinking in Functions: Functional Programming in Python
PDF
Monadic Java
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
PDF
Java Simple Programs
PPTX
07. Arrays
PPTX
07. Java Array, Set and Maps
PPS
Class method
PPTX
13. Java text processing
PDF
The Ring programming language version 1.8 book - Part 37 of 202
PPTX
Functional programming
PDF
Functional programming ii
Javaz. Functional design in Java 8.
From object oriented to functional domain modeling
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Chapter 2 Method in Java OOP
Java 8 Workshop
Chapter 4 - Classes in Java
16. Java stacks and queues
Chapter 3 Arrays in Java
OOP and FP - Become a Better Programmer
Thinking in Functions: Functional Programming in Python
Monadic Java
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Java Simple Programs
07. Arrays
07. Java Array, Set and Maps
Class method
13. Java text processing
The Ring programming language version 1.8 book - Part 37 of 202
Functional programming
Functional programming ii
Ad

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

PPT
object oriented programming java lectures
PPT
Operators
PPTX
Operators in java
PPT
Chapter 2&3 (java fundamentals and Control Structures).ppt
PPTX
Arithmetic Operators ____ java.pptx
PPT
Functional Programming
PPTX
Pi j1.3 operators
PDF
Java 8 lambda expressions
PPTX
Java fundamentals
PDF
New Functional Features of Java 8
PPT
Effective Java - Still Effective After All These Years
PPTX
Java chapter 3
PPT
operators.ppt
PPTX
Class_IX_Operators.pptx
PPTX
05 operators
PPT
TechTalk - Dotnet
PDF
Astronomical data analysis by python.pdf
PPTX
CSE240 Pointers
PPTX
Working effectively with legacy code
PDF
Java 스터디 강의자료 - 1차시
object oriented programming java lectures
Operators
Operators in java
Chapter 2&3 (java fundamentals and Control Structures).ppt
Arithmetic Operators ____ java.pptx
Functional Programming
Pi j1.3 operators
Java 8 lambda expressions
Java fundamentals
New Functional Features of Java 8
Effective Java - Still Effective After All These Years
Java chapter 3
operators.ppt
Class_IX_Operators.pptx
05 operators
TechTalk - Dotnet
Astronomical data analysis by python.pdf
CSE240 Pointers
Working effectively with legacy code
Java 스터디 강의자료 - 1차시
Ad

Recently uploaded (20)

PDF
CEH Module 2 Footprinting CEH V13, concepts
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
PDF
SaaS reusability assessment using machine learning techniques
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PDF
Examining Bias in AI Generated News Content.pdf
PDF
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
substrate PowerPoint Presentation basic one
PDF
Human Computer Interaction Miterm Lesson
PDF
Altius execution marketplace concept.pdf
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
Electrocardiogram sequences data analytics and classification using unsupervi...
PPTX
SGT Report The Beast Plan and Cyberphysical Systems of Control
PDF
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
PDF
Build Real-Time ML Apps with Python, Feast & NoSQL
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
PDF
NewMind AI Weekly Chronicles – August ’25 Week IV
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
CEH Module 2 Footprinting CEH V13, concepts
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
SaaS reusability assessment using machine learning techniques
EIS-Webinar-Regulated-Industries-2025-08.pdf
4 layer Arch & Reference Arch of IoT.pdf
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
Examining Bias in AI Generated News Content.pdf
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
substrate PowerPoint Presentation basic one
Human Computer Interaction Miterm Lesson
Altius execution marketplace concept.pdf
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
Electrocardiogram sequences data analytics and classification using unsupervi...
SGT Report The Beast Plan and Cyberphysical Systems of Control
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
Build Real-Time ML Apps with Python, Feast & NoSQL
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
NewMind AI Weekly Chronicles – August ’25 Week IV
A symptom-driven medical diagnosis support model based on machine learning te...

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