0% found this document useful (0 votes)
3 views13 pages

Detailed Notes On Java Operators

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)
3 views13 pages

Detailed Notes On Java Operators

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

Detailed Notes on Java Operators

ICSE Class 9 & 10 Computer Applications

1. Introduction to Operators
Operators are special symbols used to perform operations on variables and values in Java.

Example:

int a = 10;
int b = 5;
int c = a + b;

Here:

• + is an operator
• a and b are operands

2. Types of Operators in Java


Java operators are mainly divided into:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional (Ternary) Operator
7. Bitwise Operators (basic introduction for ICSE)

3. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Operator Meaning Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus (remainder) a%b

Example Program
class Arithmetic
{
public static void main()
{
int a = 20;
int b = 6;

[Link]("Addition = " + (a+b));


[Link]("Subtraction = " + (a-b));
[Link]("Multiplication = " + (a*b));
[Link]("Division = " + (a/b));
[Link]("Remainder = " + (a%b));
}
}

Important Concepts
Integer Division
int a = 10;
int b = 3;

[Link](a/b);

Output:

Reason: Both are integers, so decimal part is discarded.

Modulus Operator %

Used to find remainder.


10 % 3 = 1
15 % 2 = 1
20 % 5 = 0

Applications of %

• Check even/odd
• Reverse numbers
• Extract digits

4. Relational Operators
These operators compare two values.

The result is always:

• true
or
• false

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example
class Relational
{
public static void main()
{
int a = 10;
int b = 20;

[Link](a>b);
[Link](a<b);
[Link](a==b);
[Link](a!=b);
}
}
Important Concept
Difference Between = and ==

Symbol Meaning
= Assignment
== Comparison

Example:

a = 5;

Stores value.

a == 5

Checks equality.

5. Logical Operators
Logical operators are used to combine conditions.

Operator Meaning
&& Logical AND
`
! Logical NOT

Logical AND &&


Condition becomes true only if BOTH conditions are true.

Truth Table
Condition 1 Condition 2 Result
true true true
true false false
Condition 1 Condition 2 Result
false true false
false false false

Example
int a = 10;

[Link](a>5 && a<20);

Output:

true

Logical OR ||
Condition becomes true if ANY one condition is true.

Truth Table
Condition 1 Condition 2 Result
true true true
true false true
false true true
false false false

Example
int a = 10;

[Link](a>20 || a<15);

Output:

true
Logical NOT !
Reverses the result.

Original Result
true false
false true

Example:

[Link](!(5>2));

Output:

false

6. Assignment Operators
Used to assign values.

Operator Meaning
= Assign
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign

Examples
int a = 10;

a += 5;

Equivalent to:

a = a + 5;

Full Example
class Assignment
{
public static void main()
{
int a = 10;

a += 2;
[Link](a);

a -= 3;
[Link](a);

a *= 2;
[Link](a);
}
}

7. Increment and Decrement Operators


Operator Meaning
++ Increase by 1
-- Decrease by 1

Types of Increment
Pre Increment
++a

Value increases FIRST.

Post Increment
a++

Value increases LATER.

Example
int a = 5;

[Link](++a);

Output:

int a = 5;

[Link](a++);
[Link](a);

Output:

5
6

Decrement Example
int a = 10;

a--;

[Link](a);

Output:

Important Difference
Pre Increment Post Increment
Value changes immediately Value changes later
++a a++

8. Conditional (Ternary) Operator


Shortcut for if-else.

Syntax
condition ? value1 : value2;

Example
int a = 10;
int b = 20;

int max = (a>b) ? a : b;

[Link](max);

Output:

20

9. Bitwise Operators (Basic Introduction)


Operate on binary values.

Operator Meaning
& Bitwise AND
` `
^ Bitwise XOR

Example
[Link](5 & 3);

Binary:

5 = 101
3 = 011
---------
001

Output:

10. Operator Precedence


Java follows priority rules.

Order of Precedence
Priority Operators
Highest ( )
Next ++ --
Next * / %
Next + -
Next < > <= >=
Next == !=
Next &&
Lowest `

Example
int x = 10 + 5 * 2;

Output:

20

Because multiplication happens first.

11. Common Errors Students Make


Mistake 1
if(a=5)

Wrong.

Correct:

if(a==5)

Mistake 2
Confusing pre and post increment.

Mistake 3
Using integer division unknowingly.

5/2 = 2

not 2.5

12. Important Viva Questions


Q1. What is an operator?

An operator is a symbol used to perform operations on variables and values.

Q2. What is the difference between = and ==?

• = assigns value
• == compares values

Q3. Which operator is used to find remainder?


%

Q4. What is the use of logical operators?

To combine conditions.

Q5. What is the difference between ++a and a++?

• ++a → increment first


• a++ → increment later

13. Practice Programs


Program 1 – Calculator
class Calculator
{
public static void main()
{
int a = 20;
int b = 10;

[Link]("Sum = " + (a+b));


[Link]("Difference = " + (a-b));
[Link]("Product = " + (a*b));
[Link]("Quotient = " + (a/b));
}
}

Program 2 – Largest Number Using Ternary Operator


class Largest
{
public static void main()
{
int a = 15;
int b = 25;

int max = (a>b) ? a : b;

[Link]("Largest = " + max);


}
}

Program 3 – Even or Odd


class EvenOdd
{
public static void main()
{
int n = 18;

if(n%2==0)
[Link]("Even");
else
[Link]("Odd");
}
}

14. Quick Revision Table


Operator Type Example
Arithmetic + - * / %
Relational == != > <
Logical `&&
Assignment += -= *=
Increment ++ --
Conditional ?:

15. Memory Tips


• % gives remainder
• == checks equality
• && means BOTH conditions true
• || means ANY one true
• ++a changes first
• a++ changes later

You might also like