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

Java Chapter4 Notes

it provides information regarding chapter 4

Uploaded by

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

Java Chapter4 Notes

it provides information regarding chapter 4

Uploaded by

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

Chapter 4: Operators – Detailed Notes

Introduction
Operators are special symbols that perform operations on variables and values.

Categories of operators in Java: Arithmetic, Relational, Logical, Bitwise, Assignment,


Conditional (ternary), Miscellaneous.

Arithmetic Operators
Basic math operations: +, -, *, /, %.

class BasicMath {
public static void main(String args[]) {
System.out.println("Integer Arithmetic:");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);

System.out.println("\nFloating Point Arithmetic:");


double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}

Explanation: Shows arithmetic with integers and doubles.

class Modulus {
public static void main(String args[]) {
int x = 42;
double y = 42.25;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}

Explanation: % gives remainder for both integers and floating-point numbers.

Relational and Logical Operators


Relational: ==, !=, >, <, >=, <=.

Logical: &, |, ^, !, &&, ||.

class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("a|b = " + c);
System.out.println("a&b = " + d);
System.out.println("a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println("!a = " + g);
}
}

Explanation: Demonstrates AND, OR, XOR, and NOT.

Assignment Operators
Basic assignment: =. Compound assignment: +=, -=, *=, /=, %=.

class OpEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;

a += 5; // a = a + 5
b *= 4; // b = b * 4
c += a * b; // c = c + (a*b)
c %= 6; // c = c % 6
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}

Explanation: Compound assignments combine arithmetic and assignment.

Increment and Decrement


++ increments by 1, -- decrements by 1.

Prefix (++x): increment before use.

Postfix (x++): increment after use.

Conditional (Ternary) Operator


Format: condition ? expr1 : expr2.

class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // absolute value
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ? -i : i;
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
}
}

Explanation: Finds absolute value of a number.

Bitwise Operators
Bitwise operators: &, |, ^, ~, <<, >>, >>>.

class BitLogic {
public static void main(String args[]) {
int a = 3; // 011
int b = 6; // 110
System.out.println("a & b = " + (a & b)); // 010 = 2
System.out.println("a | b = " + (a | b)); // 111 = 7
System.out.println("a ^ b = " + (a ^ b)); // 101 = 5
System.out.println("~a = " + (~a)); // invert bits
}
}

Explanation: Demonstrates bitwise operations.

Operator Precedence
Highest precedence: [], (), .

Unary: ++, --, +, -, !, ~

Multiplication/division/modulus: *, /, %

Addition/subtraction: +, -

Shifts: <<, >>, >>>

Relational: <, <=, >, >=

Equality: ==, !=

Bitwise: &, ^, |

Logical: &&, ||

Conditional: ?:

Assignment: =, +=, -=, *=, etc.

Summary
Java supports arithmetic, relational, logical, bitwise, assignment, increment/decrement, and
ternary operators.

Programs illustrated usage with integers, floating-point numbers, booleans, and bits.

Operator precedence defines evaluation order.

You might also like