Operators: ========== 1) Arithmetic Operators +, -, *, / (normal division = quotient), %(modulo Division = remainder) ++ (Increment Operator) & -- (Decrement Operator) 2) Assignment Operator: ======================== Variable declaration: Datatype Identifier = value; ex: int a = 9; int b = 7; int c = a + b; a+b+c = 0 a = b - c Compound Operator: ================== += -= *= /= %= a = 10 a+=20 ==> a = a + 20 a-=10 ==> a = a - 10 a*=2 ==> a = a * 2 class Operators{ public static void main(String x[]) { int a = 2; System.out.println(a); a+=20; // a = a + 20 System.out.println(a); a-=10; //a = a-10 System.out.println(a); a*=3; //a = a * 3 System.out.println(a); a/=2; // a= a/2; System.out.println(a); // a++=; } } ================================================== 3) Relational Operators: ======================== ==> also called as "comparison operators". 10 and 20 10 is small 20 20 is big 10 10 is not equal to 20 ==> <, >, <=, >=, ==, != ==> return: Boolean values (true/false) class Operators{ public static void main(String x[]) { int a = 100; int b = 100; System.out.println("Result1 = "+(a false 2) when both inputs are: true output ==> true Note: ===== If the first input : true output : second input value If the first input : false output: first input value (without reading the second input) class Operators{ public static void main(String x[]) { System.out.println(true && true); System.out.println(true && false); System.out.println(false && true); System.out.println(false && false); } } ======== Logical or: =========== 1) when any input is "true" output ==> true 2) when both inputs are : false output ==> false Note: ===== 1) if the first input : true output : first input value (without reading the second) 2) if the first input : false output : second input value class Operators{ public static void main(String x[]) { System.out.println(true || true); System.out.println(true || false); System.out.println(false || true); System.out.println(false || false); } } ========================================== a = true b = false c = false d = true a && b && c && d Associativity: left to right (a && b) && c && d (false && c) && d false && d false class Operators{ public static void main(String x[]) { System.out.println('9' && '7'); } } ERROR! /tmp/E9KLAA2Cnf/Operators.java:4: error: bad operand types for binary operator '&&' System.out.println('9' && '7'); ^ first type: char second type: char 1 error === Code Exited With Errors === =============================================== Conditional Operator: ===================== ==> also called as "ternary operator". ==> ?: Syntax: result = test-condition ? expression-1 : expression-2; // WAP in java to check whether the given number is positive or negative. // -1 < 0 // 1 > 0 import java.util.Scanner; class Operators{ public static void main(String x[]) { Scanner sco = new Scanner(System.in); int a; // variable declaration System.out.println("Enter a value for a:"); a = sco.nextInt(); // -9 7 String result = (a > 0) ? "a is positive" : "a is negative"; System.out.println(result); } } ========================================== // WAP to find the biggest number among two integers. // a and b // a is big a > b // b is big a < b import java.util.Scanner; class ConditionalOperator{ public static void main(String[] x) { Scanner obj = new Scanner(System.in); System.out.println("Enter two integers:"); int a = obj.nextInt(); int b = obj.nextInt(); String result = (a > b)?"a is bigger than b" : "b is bigger than a"; System.out.println(result); } } ========================================================= Assignment: =========== 1) WAP in java to check a number is even or odd using the conditional operator. 2) WAP in java to swap values of two variables without the third variable. Hint: a = 9 b = 7 op: a = 7 b = 9 temp = b b = a a = temp