0% found this document useful (0 votes)
1 views

OOP- Lec-04

The document is a lecture on operators in Java, covering various types including assignment, arithmetic, unary, relational, and logical operators. It provides examples and explanations for each operator type, demonstrating their usage in Java programming. Additionally, it introduces the ternary operator as a shorthand for if-then-else statements.

Uploaded by

ilyasabbasi254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

OOP- Lec-04

The document is a lecture on operators in Java, covering various types including assignment, arithmetic, unary, relational, and logical operators. It provides examples and explanations for each operator type, demonstrating their usage in Java programming. Additionally, it introduces the ternary operator as a shorthand for if-then-else statements.

Uploaded by

ilyasabbasi254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

National University of Technology, Islamabad

Operators in Java
Lecture 04

Faizan Abbas
FA/Lecture 04 Introduction to Computer Programming with Java

LEARNING OBJECTIVES:

 Previous lecture (recap)


 Operators
 Assignment Operators
 Arithmetic Operators
 Unary Operators
 Relational Operators
 Logical Operators

2
FA/Lecture 04 Introduction to Computer Programming with Java

OPERATORS
Java provides a rich set of operators to manipulate variables. We can divide all the Java
operators into the following groups.

 Assignment Operators
 Arithmetic Operators
 Relational Operators
 Logical Operators

ASSIGNMENT OPERATOR
Assignment operators are used in Java to assign values to variables. For example,

int age;
age = 5;

The assignment operator assigns the value on its right to the variable on its left. Here, 5 is
assigned to the variable age using = operator.

There are other assignment operators too. However, to keep things simple, we will learn
other assignment operators later in this article.

Example 1: Assignment Operator

1. class AssignmentOperator {
2. public static void main(String[] args) {
3.
4. int number1, number2;
5.
6. // Assigning 5 to number1
7. number1 = 5;
8. System.out.println(number1);
9.

3
FA/Lecture 04 Introduction to Computer Programming with Java
10. // Assigning value of variable number2 to number1
11. number2 = number1;
12. System.out.println(number2);
13. }
14. }

When you run the program, the output will be:

5
5

More Assignment Operators

We have only discussed about one assignment operator = in the beginning of the article.
Except this operator, there are quite a few assignment operators that helps us to write
cleaner code.

Operator Example Equivalent to

+= x += 5 x=x+5

-= x -= 5 x=x-5

*= x *= 5 x=x*5

/= x /= 5 x=x/5

%= x %= 5 x=x/5

<<= x <<= 5 x = x << 5

>>= x >>= 5 x = x >> 5

&= x &= 5 x=x&5

^= x ^= 5 x=x^5

|= x |= 5 x=x|5

4
FA/Lecture 04 Introduction to Computer Programming with Java

Operator Example Equivalent to

Java Assignment Operators

ARITHMETIC OPERATORS
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication etc.

Operator Meaning

+ Addition (also used for string concatenation)

- Subtraction Operator

* Multiplication Operator

/ Division Operator

% Remainder Operator

Java Arithmetic Operators

Example 2: Arithmetic Operator


1. class ArithmeticOperator {
2. public static void main(String[] args) {
3.
4. double number1 = 12.5, number2 = 3.5, result;
5.
6. // Using addition operator
7. result = number1 + number2;
8. System.out.println("number1 + number2 = " + result);
9.
10. // Using subtraction operator
11. result = number1 - number2;

5
FA/Lecture 04 Introduction to Computer Programming with Java
12. System.out.println("number1 - number2 = " + result);
13.
14. // Using multiplication operator
15. result = number1 * number2;
16. System.out.println("number1 * number2 = " + result);
17.
18. // Using division operator
19. result = number1 / number2;
20. System.out.println("number1 / number2 = " + result);
21.
22. // Using remainder operator
23. result = number1 % number2;
24. System.out.println("number1 % number2 = " + result);
25. }
26. }

When you run the program, the output will be:

number1 + number2 = 16.0


number1 - number2 = 9.0
number1 * number2 = 43.75
number1 / number2 = 3.5714285714285716
number1 % number2 = 2.0

In above example, all operands used are variables. However, it's not necessary at all.
Operands used in arithmetic operators can be literals as well. For example,

result = number1 + 5.2;


result = 2.3 + 4.5;
number2 = number1 -2.9;

The + operator can also be used to concatenate two or more strings.

Example 3: Arithmetic Operator for Strings


1. class ArithmeticOperator {
2. public static void main(String[] args) {
3.
4. String start, middle, end, result;
5.
6. start = "Talk is cheap. ";
7. middle = "Show me the code. ";
8. end = "- Linus Torvalds";

6
FA/Lecture 04 Introduction to Computer Programming with Java
9.
10. result = start + middle + end;
11. System.out.println(result);
12. }
13. }

When you run the program, the output will be:

Talk is cheap. Show me the code. - Linus Torvalds

UNARY OPERATORS
Unary operator performs operation on only one operand.

Operator Meaning

+ Unary plus (not necessary to use since numbers are positive without using it)

- Unary minus; inverts the sign of an expression

++ Increment operator; increments value by 1

-- decrement operator; decrements value by 1

! Logical complement operator; inverts the value of a boolean

Example 4: Unary Operator


1. class UnaryOperator {
2. public static void main(String[] args) {
3.
4. double number = 5.2, resultNumber;
5. boolean flag = false;
6.
7. System.out.println("+number = " + +number);
8. // number is equal to 5.2 here.
9.
10. System.out.println("-number = " + -number);
11. // number is equal to 5.2 here.
12.
13. // ++number is equivalent to number = number + 1
14. System.out.println("number = " + ++number);

7
FA/Lecture 04 Introduction to Computer Programming with Java
15. // number is equal to 6.2 here.
16.
17. // -- number is equivalent to number = number - 1
18. System.out.println("number = " + --number);
19. // number is equal to 5.2 here.
20.
21. System.out.println("!flag = " + !flag);
22. // flag is still false.
23. }
24. }

When you run the program, the output will be:

+number = 5.2
-number = -5.2
number = 6.2
number = 5.2
!flag = true

You can also use ++ and -- operator as both prefix and postfix in Java. The ++ operator
increases value by 1 and -- operator decreases value by 1.

int myInt = 5;
++myInt // myInt becomes 6
myInt++ // myInt becomes 7
--myInt // myInt becomes 6
myInt-- // myInt becomes 5

Simple enough till now. However, there is a crucial difference while using increment and
decrement operator as prefix and postfix. Consider this example,

Example 5: Unary Operator


1. class UnaryOperator {
2. public static void main(String[] args) {
3.
4. double number = 5.2;
5.
6. System.out.println(number++);
7. System.out.println(number);
8.
9. System.out.println(++number);

8
FA/Lecture 04 Introduction to Computer Programming with Java
10. System.out.println(number);
11. }
12. }

When you run the program, the output will be:

5.2
6.2
7.2
7.2

When System.out.println(number++); statement is executed, the original value is evaluated


first. The number is increased only after that. That's why you are getting 5.2 as an output.
Then, when System.out.println(number); is executed, the increased value 6.2 is displayed.

However, when System.out.println(++number); is executed, number is increased by 1 first before


it's printed on the screen.

Similar is the case for decrement -- operator.

EQUALITY AND RELATIONAL OPERATORS


The equality and relational operators determines the relationship between two operands. It
checks if an operand is greater than, less than, equal to, not equal to and so on. Depending
on the relationship, it results to either true or false.

Operator Description Example

== equal to 5 == 3 is evaluated to false

!= not equal to 5 != 3 is evaluated to true

> greater than 5 > 3 is evaluated to true

9
FA/Lecture 04 Introduction to Computer Programming with Java

Operator Description Example

< less than 5 < 3 is evaluated to false

>= greater than or equal to 5 >= 5 is evaluated to true

<= less then or equal to 5 <= 5 is evaluated to true

Java Equality and Relational Operators

Equality and relational operators are used in decision making and loops (which will be
discussed later). For now, check this simple example

Example 6: Equality and Relational Operators


1. class RelationalOperator {
2. public static void main(String[] args) {
3.
4. int number1 = 5, number2 = 6;
5.
6. if (number1 > number2)
7. {
8. System.out.println("number1 is greater than number2.");
9. }
10. else
11. {
12. System.out.println("number2 is greater than number1.");
13. }
14. }
15. }
When you run the program, the output will be:
number2 is greater than number1.

Here, we have used > operator to check if number1 is greater than number2 or not.

Since, number2 is greater than number1, the expression number1 > number2 is evaluated to false.

Hence, the block of code inside else is executed and the block of code inside if is skipped.

10
FA/Lecture 04 Introduction to Computer Programming with Java
If you didn't understand the above code, don't worry. You will learn it in detail in Java
if...else article.

For now, just remember that the equality and relational operators compares two operands
and is evaluated to either true or false.

In addition to relational operators, there is also a type comparison operator instanceof which
compares an object to a specified type. For example,

INSTANCE OF OPERATOR
Here's an example of instanceof operator.

1. class instanceofOperator {
2. public static void main(String[] args) {
3.
4. String test = "asdf";
5. boolean result;
6.
7. result = test instanceof String;
8. System.out.println(result);
9. }
10. }
When you run the program, the output will be true. It's because test is the instance
of String class.
You will learn more about instanceof operator works once you understand Java Classes and
Objects.

LOGICAL OPERATORS
The logical operators || (conditional-OR) and && (conditional-AND) operates on boolean
expressions. Here's how they work.

11
FA/Lecture 04 Introduction to Computer Programming with Java

Operator Description Example

conditional-OR; true if either of the boolean false || true is evaluated

|| expression is true to true

conditional-AND; true if all boolean expressions false && true is evaluated to

&& are true false

Java Logical Operators

Example 8: Logical Operators


1. class LogicalOperator {
2. public static void main(String[] args) {
3.
4. int number1 = 1, number2 = 2, number3 = 9;
5. boolean result;
6.
7. // At least one expression needs to be true for result to be true
8. result = (number1 > number2) || (number3 > number1);
9. // result will be true because (number1 > number2) is true
10. System.out.println(result);
11.
12. // All expression must be true from result to be true
13. result = (number1 > number2) && (number3 > number1);
14. // result will be false because (number3 > number1) is false
15. System.out.println(result);
16. }
17. }

When you run the program, the output will be:

true
false

Logical operators are used in decision making and looping.

TERNARY OPERATOR
The conditional operator or ternary operator ?: is shorthand for if-then-else statement. The
syntax of conditional operator is:

12
FA/Lecture 04 Introduction to Computer Programming with Java

variable = Expression ? expression1 : expression2

Here's how it works.

 If the Expression is true, expression1 is assigned to variable.


 If the Expression is false, expression2 is assigned to variable.

Example 9: Ternary Operator


1. class ConditionalOperator {
2. public static void main(String[] args) {
3.
4. int februaryDays = 29;
5. String result;
6.
7. result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
8. System.out.println(result);
9. }
10. }

When you run the program, the output will be:

Leap year

13

You might also like