Operators in JAVA
OperatorAn operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators to manipulate variables.
OperandsAn operands are the values on which the operators act upon.An operand can be:A numeric variable - integer, floating point or character
Any primitive type variable - numeric and boolean
Reference variable to an object
A literal - numeric value, boolean value, or string.
An array element, "a[2]“
char primitive, which in numeric operations is treated as an unsigned two byte integerTypes of OperatorsAssignment OperatorsIncrement Decrement OperatorsArithmetic OperatorsBitwise OperatorsRelational OperatorsLogical OperatorsTernary OperatorsComma OperatorsInstanceof Operators
Assignment OperatorsThe assignment statements has the following syntax:<variable> = <expression>
Assigning values Example
Increment and Decrement operators++ and --The increment and decrement operators add an integer variable by one.increment operator: two successive plus signs, ++decrement operator: --
Increment and Decrement operators++ and --Common Shorthanda = a + 1;	a++; or ++a;a = a - 1;	a--; or --a;
Example of ++ and -- operatorspublic class Example {	public static void main(String[] args) { }}int j, p, q, r, s;j = 5;p = ++j; // j = j + 1; p = j;System.out.println("p = " + p);q = j++; // q = j; j = j + 1;System.out.println("q = " + q);System.out.println("j = " + j);r = --j; // j = j -1; r = j;System.out.println("r = " + r);s = j--; // s = j; j = j - 1;System.out.println("s = " + s);> java examplep = 6q = 6j = 7r = 6s = 6>
Arithmetic OperatorsThe arithmetic operators are used to construct mathematical expressions as in algebra. Their operands are of numeric type.
Arithmetic Operators
Simple Arithmeticpublic class Example {	public static void main(String[] args) {	int j, k, p, q, r, s, t;	j = 5;	k = 2;	p = j + k;	q = j - k;	r = j * k;	s = j / k;	t = j % k;	System.out.println("p = " + p);	System.out.println("q = " + q);	System.out.println("r = " + r);	System.out.println("s = " + s);	System.out.println("t = " + t);	}} > java Example p = 7 q = 3 r = 10 s = 2 t = 1 >
Bitwise OperatorsJava's bitwise operators operate on individual bits of integer (int and long) values. If an operand is shorter than an int, it is promoted to int before doing the operations.
Bitwise Operators
Example of Bitwise Operatorsclass 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 */ System.out.println("a & b = " + c ); c = a | b; /* 61 = 0011 1101 */ System.out.println("a | b = " + c );
Example Cont.,	c = a ^ b; /* 49 = 0011 0001 */	System.out.println("a ^ b = " + c );	c = ~a; /*-61 = 1100 0011 */	System.out.println("~a = " + c ); c = a << 2; /* 240 = 1111 0000 */	System.out.println("a << 2 = " + c ); c = a >> 2; /* 215 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 215 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } }
Relational OperatorsA relational operator compares two values and determines the relationship between them. For example, != returns true if its two operands are unequal. Relational operators are used to test whether two values are equal, whether one value is greater than another, and so forth. 
Relational Operators
Relational Operators
Example of Relational Operatorspublic LessThanExample {publicstatic void main(String args[]) {	int a = 5; int b = 10;  	if(a < b)	{System.out.println("a is less than b");	}	} }
Logical OperatorsThese logical operators work only on boolean operands. Their return values are always boolean.
Logical Operators
Example of Logical OperatorspublicclassANDOperatorExample{	publicstatic void main(String[] args){  	char ans = 'y';	int count = 1;  	if(ans == 'y' & count == 0){	System.out.println("Count is Zero.");}	if(ans == 'y' & count == 1) { System.out.println("Count is One."); }  	if(ans == 'y' & count == 2) { System.out.println("Count is Two."); } } }
Ternary OperatorsJava has a short hand way by using ?: the ternary aka conditional operator for doing ifs that compute a value. Unlike the if statement, the conditional operator is an expression which can be used for
Example of Ternary Operator// longhand with if:int answer; if ( a > b ){answer = 1; }else{answer = -1;	}// can be written more tersely with the ternary operator as:int answer = a > b ? 1 : -1;
Comma OperatorsJava has an often look past feature within it’s for loop and this is the comma operator. Usually when people think about commas in the java language they think of a way to split up arguments within a functions parameters

Operators in java

  • 1.
  • 2.
    OperatorAn operator isa symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators to manipulate variables.
  • 3.
    OperandsAn operands arethe values on which the operators act upon.An operand can be:A numeric variable - integer, floating point or character
  • 4.
    Any primitive typevariable - numeric and boolean
  • 5.
  • 6.
    A literal -numeric value, boolean value, or string.
  • 7.
  • 8.
    char primitive, whichin numeric operations is treated as an unsigned two byte integerTypes of OperatorsAssignment OperatorsIncrement Decrement OperatorsArithmetic OperatorsBitwise OperatorsRelational OperatorsLogical OperatorsTernary OperatorsComma OperatorsInstanceof Operators
  • 9.
    Assignment OperatorsThe assignmentstatements has the following syntax:<variable> = <expression>
  • 10.
  • 11.
    Increment and Decrementoperators++ and --The increment and decrement operators add an integer variable by one.increment operator: two successive plus signs, ++decrement operator: --
  • 12.
    Increment and Decrementoperators++ and --Common Shorthanda = a + 1; a++; or ++a;a = a - 1; a--; or --a;
  • 13.
    Example of ++and -- operatorspublic class Example { public static void main(String[] args) { }}int j, p, q, r, s;j = 5;p = ++j; // j = j + 1; p = j;System.out.println("p = " + p);q = j++; // q = j; j = j + 1;System.out.println("q = " + q);System.out.println("j = " + j);r = --j; // j = j -1; r = j;System.out.println("r = " + r);s = j--; // s = j; j = j - 1;System.out.println("s = " + s);> java examplep = 6q = 6j = 7r = 6s = 6>
  • 14.
    Arithmetic OperatorsThe arithmeticoperators are used to construct mathematical expressions as in algebra. Their operands are of numeric type.
  • 15.
  • 16.
    Simple Arithmeticpublic classExample { public static void main(String[] args) { int j, k, p, q, r, s, t; j = 5; k = 2; p = j + k; q = j - k; r = j * k; s = j / k; t = j % k; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("t = " + t); }} > java Example p = 7 q = 3 r = 10 s = 2 t = 1 >
  • 17.
    Bitwise OperatorsJava's bitwise operators operateon individual bits of integer (int and long) values. If an operand is shorter than an int, it is promoted to int before doing the operations.
  • 18.
  • 19.
    Example of BitwiseOperatorsclass 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 */ System.out.println("a & b = " + c ); c = a | b; /* 61 = 0011 1101 */ System.out.println("a | b = " + c );
  • 20.
    Example Cont., c =a ^ b; /* 49 = 0011 0001 */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = 1100 0011 */ System.out.println("~a = " + c ); c = a << 2; /* 240 = 1111 0000 */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 215 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 215 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } }
  • 21.
    Relational OperatorsA relationaloperator compares two values and determines the relationship between them. For example, != returns true if its two operands are unequal. Relational operators are used to test whether two values are equal, whether one value is greater than another, and so forth. 
  • 22.
  • 23.
  • 24.
    Example of RelationalOperatorspublic LessThanExample {publicstatic void main(String args[]) { int a = 5; int b = 10;   if(a < b) {System.out.println("a is less than b"); } } }
  • 25.
    Logical OperatorsThese logicaloperators work only on boolean operands. Their return values are always boolean.
  • 26.
  • 27.
    Example of LogicalOperatorspublicclassANDOperatorExample{ publicstatic void main(String[] args){   char ans = 'y'; int count = 1;   if(ans == 'y' & count == 0){ System.out.println("Count is Zero.");} if(ans == 'y' & count == 1) { System.out.println("Count is One."); }   if(ans == 'y' & count == 2) { System.out.println("Count is Two."); } } }
  • 28.
    Ternary OperatorsJava hasa short hand way by using ?: the ternary aka conditional operator for doing ifs that compute a value. Unlike the if statement, the conditional operator is an expression which can be used for
  • 29.
    Example of TernaryOperator// longhand with if:int answer; if ( a > b ){answer = 1; }else{answer = -1; }// can be written more tersely with the ternary operator as:int answer = a > b ? 1 : -1;
  • 30.
    Comma OperatorsJava hasan often look past feature within it’s for loop and this is the comma operator. Usually when people think about commas in the java language they think of a way to split up arguments within a functions parameters
  • 31.
    Example of CommaOperator//: c03:CommaOperator.java// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002// www.BruceEckel.com. See copyright notice in CopyRight.txt.public class CommaOperator {  public static void main(String[] args) {    for(int i = 1, j = i + 10; i < 5;        i++, j = i * 2) {      System.out.println("i= " + i + " j= " + j);    }  }} ///:~                    
  • 32.
    Instanceof OperatorsThis operatoris used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). InstanceOf operator is wriiten as:
  • 33.
  • 34.