0% found this document useful (0 votes)
21 views15 pages

Lec 3 Operators

The document provides an overview of various operators in Java, including arithmetic, unary, assignment, relational, logical, and conditional operators, along with their usage and examples. It also discusses operator precedence, implicit type conversion, and the cast operator. Additionally, it introduces the ternary operator and its application in evaluating Boolean expressions.

Uploaded by

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

Lec 3 Operators

The document provides an overview of various operators in Java, including arithmetic, unary, assignment, relational, logical, and conditional operators, along with their usage and examples. It also discusses operator precedence, implicit type conversion, and the cast operator. Additionally, it introduces the ternary operator and its application in evaluating Boolean expressions.

Uploaded by

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

Java Programming

By:
Asad Khan
Lecturer in CS GPGC Lakki Marwat

07/30/2025
Arithmetic Operators
• They are used to perform simple arithmetic operations on
primitive data types.

• * : Multiplication
• / : Division
• % : Modulus
• + : Addition
• – : Subtraction

07/30/2025
Arithmetic Operators
import java.io.*;

// Drive Class
class GFG {
// Main Function
public static void main (String[] args) {

// Arithmetic operators
int a = 10;
int b = 3;

System.out.println("a + b = " + (a + b));


System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));

}
07/30/2025
}
Unary operators
• Unary operators need only one operand.
• They are used to increment, decrement, or negate a value.

• – : Unary minus, used for negating the values.


• + : Unary plus indicates the positive value (numbers are positive without
this, however). It performs an automatic conversion to int when the type of
its operand is the byte, char, or short. This is called unary numeric
promotion.
• ++ : Increment operator, used for incrementing the value by 1. There are
two varieties of increment operators.
• Post-Increment: Value is first used for computing the result and then
incremented.
• Pre-Increment: Value is incremented first, and then the result is computed.
• – – : Decrement operator, used for decrementing the value by 1. There are
two varieties of decrement operators.
• Post-decrement: Value is first used for computing the result and then
decremented.
• Pre-Decrement: The value is decremented first, and then the result is
07/30/2025 computed.
Java Assignment Operators

• Assignment operators are used to assign values to variables.

• In the example below, we use the assignment operator (=) to assign the value 10 to a
variable called x:
• In many cases, the assignment operator can be combined with other operators to build a
shorter version of the statement called a Compound Statement. For example, instead of
a = a+5, we can write a += 5.

import java.io.*;
class GFG {
// Main Function
public static void main(String[] args)
{

// Assignment operators
int f = 7;
System.out.println("f += 3: " + (f += 3));
System.out.println("f -= 2: " + (f -= 2));
System.out.println(“f *= 4: " + (f *= 4));}
07/30/2025
}
Relational Operators

• These operators are used to check for relations like equality,


greater than, and less than.
• They return boolean results after the comparison and are
extensively used in looping statements as well as conditional if-
else statements. The general format is,
• variable relation_operator value
• Some of the relational operators are-

• ==, Equal to returns true if the left-hand side is equal to the right-
hand side.
• !=, Not Equal to returns true if the left-hand side is not equal to the
right-hand side.

07/30/2025
Cont…
• <, less than: returns true if the left-hand side is less than the right-hand side.
• <=, less than or equal to returns true if the left-hand side is less than or equal to
the right-hand side.
• >, Greater than: returns true if the left-hand side is greater than the right-hand
side.
• >=, Greater than or equal to returns true if the left-hand side is greater than or
equal to the right-hand side.

class GFG {
// main function
public static void main(String[] args)
{
// Comparison operators
int a = 10; int b = 3; int c = 5;
System.out.println("a > b: " + (a > b));
System.out.println("a <= b: " + (a <= b));
System.out.println("a == c: " + (a == c))
}}
07/30/2025
Logical Operators
• These operators are used to perform “logical AND” and “logical OR”
operations.
• One thing to keep in mind is the second condition is not evaluated if the first
one is false, i.e., it has a short-circuiting effect.
• Used extensively to test for several conditions for making a decision.
• Java also has “Logical NOT”, which returns true when the condition is false
and vice-versa
• Conditional operators are:
• &&, Logical AND: returns true when both conditions are true.
• ||, Logical OR: returns true if at least one condition is true.
• !, Logical NOT: returns true when a condition is false and vice-versa.
import java.io.*;
class logical {
public static void main (String[] args) {
boolean x = true;
boolean y = false;
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
07/30/2025 System.out.println("!x: " + (!x));}}
Operator precedence
• The operator precedence represents how two expressions are bind
together.
• In an expression, it determines the grouping of operators with operands
and decides how an expression will evaluate.
• While solving an expression two things must be kept in mind the first
is a precedence and the second is associativity.

07/30/2025
Implicit Type Conversion
• Java converts shorter data types to larger data types when they are assigned to the larger
variable.
• For example, if you assign a short value to an int variable then Java does the work for
you and converts the short value to an int and stores it in the int variable.
• This is similar to the fact that you can put a raisin in a XXL polythene bag.

public class ImplicitTypecastingExample {


public static void main(String args[]) {
byte p = 12;
System.out.println("byte value : "+p);
// Implicit Typecasting
short q = p;
System.out.println("short value : "+q);
int r = q;
System.out.println("int value : "+r);
long s = r;
System.out.println("long value : "+s);
07/30/2025 float t = s;
Cast Operator
• The cast operator in Java is employed to convert between various data
types.

• Example
• CastOperatorExpl.java

• public class CastOperatorExpl {


• public static void main(String args[]) {
• double d = 21.9;
• int i = (int)d;
• System.out.println(i);
• }
• }
07/30/2025
Conditional Operator in Java

• In Java, conditional operators check the condition and decides the


desired result on the basis of both conditions.
• In this section, we will discuss the conditional operator in Java.

• Types of Conditional Operator


• Conditional AND
• Conditional OR
• Ternary Operator

07/30/2025
Cont…
• Conditional AND:
• The operator is applied between two Boolean expressions.
• It is denoted by the two AND operators (&&).
• It returns true if and only if both expressions are true, else returns false.
• Conditional OR
• The operator is applied between two Boolean expressions.
• It is denoted by the two OR operator (||).
• It returns true if any of the expression is true, else returns false.
• Ternary Operator
• The meaning of ternary is composed of three parts.
• The ternary operator (? :) consists of three operands.
• It is used to evaluate Boolean expressions.
• The operator decides which value will be assigned to the variable.

07/30/2025
Cont…
public class TernaryOperatorExample
{
public static void main(String args[])
{
int x, y;
x = 20;
y = (x == 1) ? 61: 90;
System.out.println("Value of y is: " + y);
y = (x == 20) ? 61: 90;
System.out.println("Value of y is: " + y);
}
}

07/30/2025
Thank you

07/30/2025

You might also like