0% found this document useful (0 votes)
5 views24 pages

2024 08 03 - Operators

The document provides an overview of various operators in Java, including arithmetic, increment/decrement, assignment, relational, logical, bitwise, and the ternary operator. It includes code examples demonstrating how each operator works and their outcomes. Additionally, it explains the use of the comma operator and the sizeof operator for determining data sizes.

Uploaded by

prajwal sri tej
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)
5 views24 pages

2024 08 03 - Operators

The document provides an overview of various operators in Java, including arithmetic, increment/decrement, assignment, relational, logical, bitwise, and the ternary operator. It includes code examples demonstrating how each operator works and their outcomes. Additionally, it explains the use of the comma operator and the sizeof operator for determining data sizes.

Uploaded by

prajwal sri tej
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/ 24

TEST TIME ON CLASS AND OBJECTS

URL- https://forms.gle/T4q8ZZs9ukLDVuLH7

QR code-
OPERATORS
Arithmetic

o These are the operators used to perform arithmetic/mathematical operations on


operands. Examples: (+, -, *, /, %,++,--)
class Main {
public static void main(String[] args) {
int a=9,b=4,c;
c=a+b;
System.out.println("a+b"+" "+"="+" "+c);
c=a-b;
System.out.println("a-b"+" "+"="+" "+c);
c=a*b;
System.out.println("a*b"+" "+"="+" "+c);
c=a/b;
System.out.println("a/b"+" "+"="+" "+c);
c=a%b;
System.out.println("a%b"+" "+"="+" "+c);
}
}
Arithmetic
o The operators +, - and * computes addition, subtraction and
multiplication respectively as you might have expected
o In normal calculation, 9/4 = 2.25
o However, the output is 2 in the program
o It is because both variables a and b are integers
o Hence, the output is also an integer
o The compiler neglects the term after decimal point and shows answer
2 instead of 2.25
o The modulo operator % computes the remainder. When a = 9 is
divided by b = 4, the remainder is 1
o The % operator can only be used with integers
Increment and Decrement
o Java programming has two operators increment ++ and decrement --
to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement --
decreases the value by 1. These two operators are unary operators,
meaning they only operate on a single operand

Example :
suppose, a = 5 then,
o ++a; //a becomes 6
o a++; //a becomes 7
o --a; //a becomes 6
o a--; //a becomes 5
Increment and Decrement
o 5 is displayed then, var is increased to 6
o Initially, var = 6. It is increased to 7 then, it is displayed

class Main {
public static void main(String[]
args) {
int a =5;
System.out.println(a++);
System.out.println(++a);
}
}
Assignment

o An assignment operator is used for assigning a value to a variable. The


most common assignment operator is =

Operator Example Same as


= a = b a = b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b
Assignment
class Main {
public static void main(String[] args) {
int a = 5, c;
c = a;
System.out.println("c"+" "+"="+" "+c);
c += a;
System.out.println("c"+" "+"="+" "+c);
c -= a;
System.out.println("c"+" "+"="+" "+c);
c *= a;
System.out.println("c"+" "+"="+" "+c);
c /= a;
System.out.println("c"+" "+"="+" "+c);
c %= a;
System.out.println("c"+" "+"="+" "+c);
}
}
Relational
o Relational operators are used for comparison of the values of two
operands. For example: checking if one operand is equal to the other
operand or not, an operand is greater than the other operand or not
etc. Some of the relational operators are (==, > , = , <= )
o A relational operator checks the relationship between two operands. If
the relation is true, it returns 1; if the relation is false, it returns value
0. Relational operators are used in decision making and loops
Relational

Operator Meaning of Example


Operator
== Equal to 5 == 3 returns 0

> Greater than 5 > 3 returns 1

< Less than 5 < 3 returns 0

!= Not equal to 5 != 3 returns 1

>= Greater than or equal 5 >= 3 returns 1


to
<= Less than or equal to 5 <= 3 return 0
Relational
class Main { else
public static void main(String[] System.out.println("a is less
args) { than");
int a=10, b=4; if (a < b)
if (a > b) System.out.println("a is less
System.out.println("a is greater than");
than"); else
else System.out.println("a is
System.out.println("a is less greater than or equal to");
than or equal to"); if (a <= b)
if (a >= b) System.out.println("a is
System.out.println("a is greater lesser than or equal to");
than or equal to"); else
System.out.println("a is
greater than");
Relational

if (a == b)
System.out.println ("a is equal to ");
else
System.out.println("a and b are not
equal");
if (a != b)
System.out.println("a is not equal to ");
else System.out.println("a is equal ");
}
}
Logical
o An expression containing logical operator returns either 0 or 1
depending upon whether expression results true or false. Logical
operators are commonly used in decision making in Java programming

Operator Meaning of Operator Example


&& Logical AND. True only if If c = 5 and d =
all operands are true 2 then,
expression ((c
== 5) && (d >
5)) equals to 0
|| Logical OR. True only if If c = 5 and d =
either one operand is 2 then,
true expression ((c
== 5) || (d > 5))
equals to 1
! Logical NOT. True only if If c = 5 then,
Logical
class Main { if((a == b) && (c < b))
public static void main(String[] {
args) { result=1;
int a = 5, b = 5, c = 10, result; System.out.println("(a == b) && (c <
if ((a == b) && (c > b)) b) equals to"+" "+ result);
{ }
result=1; else
System.out.println("(a == b) && (c > {
b) equals to "+result); result=0;
} System.out.println("(a == b) && (c >
else b) equals to"+" "+ result);
{ }
result=0; if ((a == b) || (c < b))
System.out.println("(a == b) && (c > {
b) equals to"+" "+ result); result=1;
} System.out.println("(a == b) || (c <
b) equals to "+" "+ result);
}
Logical
else else
{ {
result=0; result=0;
System.out.println("(a == b) || (c < System.out.println("(a != b) || (c <
b) equals to "+" "+ result); b) equals to "+" "+ result);
} }
if((a != b) || (c < b)) if (!(a != b))
{ {
result=1; result=1;
System.out.println("(a != b) || (c < System.out.println("!(a == b) equals
b) equals to"+" "+ result); to "+" "+ result);
} }
Logical
else
{
result=0;
System.out.println("!(a == b) equals to "+" "+ result);
}
if(!(a == b))
{
result=1;
System.out.println("!(a == b) equals to "+" "+ result);
}
else
{
result=0;
System.out.println("!(a == b) equals to "+" "+ result);
}
}
}
Bitwise
o The Bitwise operators is used to perform bit-level operations on the operands. The
operators are first converted to bit-level and then calculation is performed on the
operands. The mathematical operations such as addition , subtraction , multiplication
etc
Operator Meaning of Operator
& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right


Bitwise
class Main {
public static void main(String[] args) {
int a = 2, b = 4,c;
c=a & b;
System.out.println("Bitwise AND "+ c);
c= a | b;
System.out.println("Bitwise OR "+c);
c=a >> 1;
System.out.println("Right shift "+ c);
c=a << 1;
System.out.println("Left shift "+ c);
c=a ^ b;
System.out.println("Bitwise exclusive OR "+ c);
c=~a;
System.out.println("Bitwise complement "+ c);
}
}
Comma Operator & SIZE
o Comma operators are used to link related expressions together

int a, c = 5, d;

o The sizeof is an unary operator which returns the size of data (constant, variables, array,
structure etc)
Comma Operator & SIZE
class Main {
public static void main(String[] args) {
int []n= new int[10],a;
float b;
double c;
char d;
System.out.println("Size of int bytes "+(Float.SIZE/8));
System.out.println("Size of float bytes "+(Double.SIZE/8));
System.out.println("Size of char bytes "+(Character.SIZE/8));
System.out.println("Size of double bytes "+(Integer.SIZE/8));
}
}
C Ternary Operator (?:)
o A conditional operator is a ternary operator, that is, it works on 3
operands

The conditional operator works as follows:

o The first expression conditionalExpression is evaluated first. This


expression evaluates to 1 if it's true and evaluates to 0 if it's false
o If conditionalExpression is true, expression1 is evaluated
o If conditionalExpression is false, expression2 is evaluated

conditionalExpression ? expression1 : expression2


C Ternary Operator (?:)
import java.util.Scanner;
class Main {
public static void main(String[] args) {
char February;
int days;
System.out.println("Leap year? enter 1. If not enter any integer:
");
Scanner s=new Scanner(System.in);
February =s.next().charAt(0);
days = (February == '1') ? 29 : 28;
System.out.println("Number of days in February = "+days);
}
}

You might also like