0% found this document useful (0 votes)
20 views41 pages

3 Basic I_O, Operators 16-07-2024

Uploaded by

abhissunita763
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)
20 views41 pages

3 Basic I_O, Operators 16-07-2024

Uploaded by

abhissunita763
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/ 41

Operators

Topic/Course
Sub-Topic (Example: name of college)
Operators are used to perform operations on variables and
values.
Topic/Course operators
Sub-Topic (Example: name of college)

• Unary operators
X=Y–1
• Binary operators

• Ternary operators
operands
Operators in Java
• Unary operators • Logical operators
• Arithmetic operators • Ternary operators
• Relational operators • Assignment
• Bitwise operators operators
• instanceof operator
Unary operators
• Unary operator performs operation on only one operand.
• They are used to increment, decrement or negate a
value.
Unary operators
Operator Description Example
++x(prefix)
Increases the value of a variable
++ by 1
x++(postfix)
--x(prefix)
Decreases the value of a variable
-- by 1
x--(postfix)
+ Used for giving positive values +x

- Used for negating the values -x

~ Negating an expression ~x

! Inverting the value of a boolean !x


What is the difference between
++x and x++ ?
1 class OperatorExample{ output
2 public static void main(String args[])
3 {
4 int x=10; 10
5 System.out.println(x++); 12
6 System.out.println(++x); 12
7 System.out.println(x--); 10
10 System.out.println(--x);
11 }
12 }
1 class OperatorExample{ output
2 public static void main(String args[]){
3 int a=10;
4 int b=-10; -11
5 boolean c=true; 9
6 boolean d=false; False
7 System.out.println(~a); true
10 System.out.println(~b);
11 System.out.println(!c);
12 System.out.println(!d);
13 }
14 }
Arithmetic operators
• Arithmetic operators are used to perform common mathematical
operations like addition, subtraction etc..
Arithmetic operators
Operator Description Example
+ Adds two values x+y

- Subtracts one value from another x-y

* Multiplies two values x*y

/ Returns the division quotient x/y

% Returns the division remainder x%y


What is the difference between
/ and % ?
1 class OperatorExample{
2 public static void main(String args[]){
3 int a=10;
4 int b=5;
5 System.out.println(a+b);
6 System.out.println(a-b);
7 System.out.println(a*b);
10 System.out.println(a/b);
11 System.out.println(a%b);
12 }
13 }

output

15
5
50
2
0
Relational operators
• Relational/Comparison operators are used to compare two
values.

• They return boolean result after the comparison.


Relational operators
Operat Exampl
Description
or e
Returns true if left hand side is equal to right
== x == y
hand side
Returns true if left hand side is not equal to right
!= x != y
hand side
Returns true if left hand side is less than right
< x<y
hand side
Returns true if left hand side is less than or equal
<= x < =y
to right hand side
Returns true if left hand side is greater than right
> x >=y
hand side
1 public class Test { output
2 public static void main(String args[]){
3 int a = 10;
4 int b = 20; false
5 System.out.println(a>b); true
6 System.out.println(a<b); false
7 System.out.println(a>=b); true
10 System.out.println(a<=b); false
11 System.out.println(a==b); true
12 System.out.println(a!=b);
13 }
14 }
Bitwise operators
• Bitwise operator works on bits and performs bit-by-bit operation.

• Can be applied to the integer types, long, int, short, char, and
byte.
Bitwise operators
Operator Description Example
& Returns bit by bit AND of input values x&y

| Returns bit by bit OR of input values x|y

^ Returns bit by bit XOR of input values x^y

~ Returns the one’s compliment representation of the input value ~x

<< shifts the bits of the number to the left and fills 0 on voids left as a result x << 2

>> shifts the bits of the number to the right and fills 0 on voids left as a result x >> 2

>>> shifts the bits of the number to the right and fills 0 on voids left as a result x >>>2
1 public class Main { output
2 public static void main(String args[]){
3 int a = 10;
4 int b = 20; 0
5 System.out.println(a&b); 30
6 System.out.println(a|b); -11
7 System.out.println(~a); 40
10 System.out.println(a<<2); 2
11 System.out.println(a>>2); 2
12 System.out.println(a>>>2);
13 }
14 }
Logical operators
• The logical operators || (conditional-OR) and && (conditional-
AND) operates on boolean expressions.

• The second condition is not evaluated if the first one is false, i.e.
it has a short-circuiting effect.
Logical operators
Operator Description Example
&& Returns true if both statements are true x < 5 && x < 10

|| Returns true if one of the statements is true x < 5 || x < 4

! Reverse the result, returns false if the result is true !(x < 5 && x < 10)
1 public class Test { output
2 public static void main(String args[]){
3 boolean a = true;
4 boolean b = false; false
5 System.out.println(a&&b); true
6 System.out.println(a||b); true
7 System.out.println(!(a && b));
10 }
11 }
Ternary operator
• Ternary/Conditional operator consists of three operands and is
used to evaluate Boolean expressions.
• Ternary operator is a shorthand version of if-else
statement.
• It has three operands and hence the name ternary.
1 class OperatorExample{ output
2 public static void main(String args[]){
3 int a=2;
4 int b=5; 2
5 int min=(a<b)?a:b;
6 System.out.println(min);
7 }
10 }
Assignment operators
• Assignment operator is used to assign a value to any variable.

• In many cases assignment operator can be combined with other


operators to build a shorter version of statement
called Compound Statement.
Assignment operators
Operator Description Example
= Assigns values from right side operands to left side operand. C=A+B

+= Adds right operand to the left operand and assign the result to left operand. C += A

Subtracts right operand from the left operand and assign the result to left
-= C -= A
operand.
Multiplies right operand with the left operand and assign the result to left C *= A
*=
operand.
Divides left operand with the right operand and assign the result to left C /= A
/=
operand.
%= Takes modulus using two operands and assign the result to left operand. C %= A
Assignment operators
Operator Description Example
<<= Left shift AND assignment operator C <<= 2

>>= Right shift AND assignment operator C >>= 2

&= Bitwise AND assignment operator C &= 2

^= Bitwise exclusive OR and assignment operator C ^= 2

|= Bitwise inclusive OR and assignment operator C |= 2


What is the difference between
= and == ?
1 class OperatorExample{ output
2 public static void main(String[] args){
3 int a=10;
4 a+=3; 13
5 System.out.println(a); 9
6 a-=4; 18
7 System.out.println(a); 9
10 a*=2;
11 System.out.println(a);
12 a/=2;
13 System.out.println(a);
14 }
15 }
instanceof operators
• instanceof operator is used only for object reference variables.

• The operator checks whether the object is of a particular type


(class type or interface type).
1 public class Test { output
2 public static void main(String args[]) {
3 String name = "James";
4 boolean result = name instanceof String;
5 System.out.println( result ); true
6 }
7 }
10
11
Precedence and associativity
• Operator precedence determines which operator is evaluated
first when an expression has more than one operators.

• Associativity is used when there are two or more operators of


same precedence is present in an expression.
1 // Predict the output
2 public class A {
3 public static void main(String[] args)
4 {
5 int $_ = 5;
6 }
7 }
OUTPUT

1. Nothing
2. Error
1 // Predict the output
2 class Test {
3 public static void main(String args[]) {
4 System.out.println(10 + 20 + “Face");
5 System.out.println(“Face" + 10 + 20);
6 }
7 }
OUTPUT

1. 30Face Face30
2. 1020Face Face1020
3. 30Face Face1020
4. 1020Face Face30
1 // Predict the output
2 class Test
3 {
4 public static void main(String args[])
5 {
6 String s1 = “FACE";
7 String s2 = “FACE";
8 System.out.println("s1 == s2 is:" + s1 == s2);
9 }
10 }
OUTPUT

1. true
2. false
3. compiler error
4. throws an exception
THANK YOU

You might also like