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

ITE 1122 - Fundamental Structures of Programming - 2

The document discusses different types of operators in Java including arithmetic, relational, bitwise, and logical operators. It provides examples of each operator type using integer variables, demonstrating how each operator works and the result it produces. The key operator types covered are arithmetic (addition, subtraction, etc.), relational (equal, not equal, greater than, etc.), bitwise (AND, OR, XOR, shift), and logical (AND, OR, NOT) operators.

Uploaded by

warunapriyanath3
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 views

ITE 1122 - Fundamental Structures of Programming - 2

The document discusses different types of operators in Java including arithmetic, relational, bitwise, and logical operators. It provides examples of each operator type using integer variables, demonstrating how each operator works and the result it produces. The key operator types covered are arithmetic (addition, subtraction, etc.), relational (equal, not equal, greater than, etc.), bitwise (AND, OR, XOR, shift), and logical (AND, OR, NOT) operators.

Uploaded by

warunapriyanath3
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

Fundamental structures of

programming_2
Agenda
Basic Operators
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Conditional Operator
Basic Operators
Java provides a rich set of operators to manipulate variables. We can
divide all the Java operators into the following groups −
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
Arithmetic Operators
Arithmetic Operator Description Example
Adds values on either side
operators are used + (Addition) of the operator. A + B will give 30

in mathematical Subtracts right-hand


expressions in the - (Subtraction) operand from left-hand
operand.
A - B will give -10

same way that they Multiplies values on either


* (Multiplication) side of the operator. A * B will give 200
are used in algebra.
The following table / (Division)
Divides left-hand operand
B / A will give 2
by right-hand operand.
lists the arithmetic
Divides left-hand operand
operators % (Modulus) by right-hand operand and B % A will give 0
returns remainder.

Increases the value of


++ (Increment) B++ gives 21
operand by 1.
Decreases the value of
-- (Decrement) B-- gives 19
operand by 1.
Arithmetic Operators - example
public class Test {

public static void main(String args[]) {


int a = 10;
int b = 20;
int c = 25; This will produce the following result
int d = 25; a + b = 30
a - b = -10
System.out.println("a + b = " + (a + b) ); a * b = 200
System.out.println("a - b = " + (a - b) ); b/a=2
System.out.println("a * b = " + (a * b) ); b%a=0
System.out.println("b / a = " + (b / a) );
c%a=5
System.out.println("b % a = " + (b % a) );
System.out.println("c % a = " + (c % a) );
a++ = 10
System.out.println("a++ = " + (a++) ); b-- = 11
System.out.println("b-- = " + (a--) ); d++ = 25
++d = 27
// Check the difference in d++ and ++d
System.out.println("d++ = " + (d++) );
System.out.println("++d = " + (++d) );
}
}
Relational Operators
There are Operator Description Example
Checks if the values of two operands are
following == (equal to) equal or not, if yes then condition becomes
true.
(A == B) is not true.

relational Checks if the values of two operands are


equal or not, if values are not equal then
operators != (not equal to) condition becomes true. (A != B) is true.

supported by Java Checks if the value of left operand is greater


than the value of right operand, if yes then
language > (greater than) condition becomes true. (A > B) is not true.

Checks if the value of left operand is less than


the value of right operand, if yes then
< (less than) condition becomes true. (A < B) is true.

Checks if the value of left operand is greater


>= (greater than or than or equal to the value of right operand, if
yes then condition becomes true. (A >= B) is not true.
equal to)

Checks if the value of left operand is less than


<= (less than or equal or equal to the value of right operand, if yes
then condition becomes true. (A <= B) is true.
to)
Relational Operators- example
public class Test {

public static void main(String args[]) {


int a = 10;
int b = 20;
This will produce the following result
System.out.println("a == b = " + (a == b) ); a == b = false
System.out.println("a != b = " + (a != b) ); a != b = true
System.out.println("a > b = " + (a > b) ); a > b = false
System.out.println("a < b = " + (a < b) ); a < b = true
System.out.println("b >= a = " + (b >= a) ); b >= a = true
System.out.println("b <= a = " + (b <= a) );
b <= a = false
}
}
Bitwise Operators
Operator Description Example
Java defines several
Binary AND Operator copies a bit to the result if (A & B) will give 12 which is
& (bitwise and)
bitwise operators, which it exists in both operands. 0000 1100

can be applied to the | (bitwise or)


Binary OR Operator copies a bit if it exists in
either operand.
(A | B) will give 61 which is
0011 1101
integer types, long, int, ^ (bitwise XOR)
Binary XOR Operator copies the bit if it is set in (A ^ B) will give 49 which is
short, char, and byte. one operand but not both. 0011 0001

(~A ) will give -61 which is 1100


Binary Ones Complement Operator is unary
~ (bitwise compliment) 0011 in 2's complement form
and has the effect of 'flipping' bits.
Bitwise operator works due to a signed binary number.

on bits and performs bit- Binary Left Shift Operator. The left operands
A << 2 will give 240 which is
by-bit operation. << (left shift) value is moved left by the number of bits
specified by the right operand.
1111 0000
Assume if a = 60 and b =
Binary Right Shift Operator. The left operands
13; now in binary format >> (right shift)
value is moved right by the number of bits A >> 2 will give 15 which is
specified by the right operand.
they will be as follows − 1111

 a = 0011 1100 Shift right zero fill operator. The left operands
value is moved right by the number of bits
 b = 0000 1101 specified by the right operand and shifted A >>>2 will give 15 which is
>>> (zero fill right shift) values are filled up with zeros. 0000 1111
Bitwise Operators - example
public class Test {

public static void main(String args[]) {


int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
int c = 0;

c = a & b; /* 12 = 0000 1100 */


This will produce the following result
System.out.println("a & b = " + c ); a & b = 12
c = a | b; /* 61 = 0011 1101 */
a | b = 61
System.out.println("a | b = " + c ); a ^ b = 49
c = a ^ b; /* 49 = 0011 0001 */
~a = -61
System.out.println("a ^ b = " + c ); a << 2 = 240
c = ~a; /*-61 = 1100 0011 */
a >> 2 = 15
System.out.println("~a = " + c ); a >>> 2 = 15
c = a << 2; /* 240 = 1111 0000 */
System.out.println("a << 2 = " + c );

c = a >> 2; /* 15 = 1111 */
System.out.println("a >> 2 = " + c );

c = a >>> 2; /* 15 = 0000 1111 */


System.out.println("a >>> 2 = " + c );
}
}
Logical Operators
Operator Description Example
The following
Called Logical AND operator. If both the
table lists the operands are non-zero, then the condition
&& (logical and) becomes true. (A && B) is false
logical operators

Called Logical OR Operator. If any of the


Assume Boolean two operands are non-zero, then the
condition becomes true.
variables A holds || (logical or) (A || B) is true

true and variable B


holds false, then Called Logical NOT Operator. Use to
reverses the logical state of its operand. If
a condition is true then Logical NOT
operator will make false.
! (logical not) !(A && B) is true
Logical Operators- example

public class Test {

public static void main(String args[]) {


boolean a = true; This will produce the following result
boolean b = false; a && b = false
a || b = true
System.out.println("a && b = " + (a&&b)); !(a && b) = true
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
}
}
Assignment Operators
Operator Description Example
Following are the =
Simple assignment operator. Assigns values from right side C = A + B will assign value
operands to left side operand. of A + B into C
assignment +=
Add AND assignment operator. It adds right operand to the left
operand and assign the result to left operand. C += A is equivalent to C =

operators Subtract AND assignment operator. It subtracts right operand


C+A

supported by Java -= from the left operand and assign the result to left operand. C -= A is equivalent to C =
C–A

language Multiply AND assignment operator. It multiplies right operand


with the left operand and assign the result to left operand. C *= A is equivalent to C =
*=
C*A

/= Divide AND assignment operator. It divides left operand with the


right operand and assign the result to left operand. C /= A is equivalent to C =
C/A
Modulus AND assignment operator. It takes modulus using two
operands and assign the result to left operand. C %= A is equivalent to C =
%=
C%A

C <<= 2 is same as C = C <<


<<= Left shift AND assignment operator.
2
C >>= 2 is same as C = C >>
>>= Right shift AND assignment operator.
2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

^= bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2

|= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C |


Assignment Operators - example
public class Test {
a = 10; This will produce the following result
public static void main(String args[]) {
c = 15;
c %= a ;
c = a + b = 30
int a = 10;
int b = 20;
System.out.println("c %= a = " + c ); c += a = 40
int c = 0;
c <<= 2 ;
c -= a = 30
c = a + b;
System.out.println("c <<= 2 = " + c ); c *= a = 300
System.out.println("c = a + b = " + c );
c >>= 2 ;
c /= a = 1
c += a ;
System.out.println("c >>= 2 = " + c ); c %= a = 5
System.out.println("c += a = " + c );
c >>= 2 ;
c <<= 2 = 20
c -= a ;
System.out.println("c >>= 2 = " + c ); c >>= 2 = 5
System.out.println("c -= a = " + c );
c &= a ;
c >>= 2 = 1
c *= a ;
System.out.println("c &= a = " + c ); c &= a = 0
System.out.println("c *= a = " + c );
c ^= a ;
c ^= a = 10
a = 10;
System.out.println("c ^= a = " + c ); c |= a = 10
c = 15;
c |= a ;
c /= a ;
System.out.println("c |= a = " + c );
System.out.println("c /= a = " + c );
}
}
Conditional Operator
 Conditional operator is also known as the ternary operator. This operator consists of three
operands and is used to evaluate Boolean expressions. The goal of the operator is to decide,
which value should be assigned to the variable. The operator is written as

 variable x = (expression) ? value if true : value if false

public class Test { This will produce the following result

public static void main(String args[]) { Value of b is : 30


int a, b; Value of b is : 20
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );

b = (a == 10) ? 20: 30;


System.out.println( "Value of b is : " + b );
}
}
Precedence of Java Operators
Category Operator Associativity
 Operator precedence determines the grouping
of terms in an expression. This affects how an Postfix >() [] . (dot operator) Left toright

expression is evaluated. Certain operators have Unary >++ - - ! ~ Right to left

higher precedence than others; for example, Multiplicative >* / Left to right
the multiplication operator has higher Additive >+ - Left to right
precedence than the addition operator
Shift >>> >>> << Left to right

Relational >> >= < <= Left to right


 For example, x = 7 + 3 * 2; here x is assigned 13,
Equality >== != Left to right
not 20 because operator * has higher
precedence than +, so it first gets multiplied Bitwise AND >& Left to right

with 3 * 2 and then adds into 7. Bitwise XOR >^ Left to right

Bitwise OR >| Left to right

 operators with the highest precedence appear Logical AND >&& Left to right
at the top of the table, those with the lowest Logical OR >|| Left to right
appear at the bottom. Within an expression, Conditional ?: Right to left
higher precedence operators will be evaluated
Assignment >= += -= *= /= %= >>= <<= &= ^= |= Right to left
first.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy