Core Java - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Operators

Java Operators

shape Description

Java Operators, As of now SPlesson taught basics of the Java programs such as variable declarations, method declarations, constructor declarations and basic structure of the program, now Splesson is going to teach different operators that are utilized to provide the relation between two values. Java Operators, Operator is like a symbol that is used to perform some operations, In Java many operators are available such as Arithmetic Operators, Relational Operators, Bit-wise Operators, Logical Operators, Assignment Operators.

Arithmetic Operators

shape Description

In Java Operators, arithmetic operators are used to perform some mathematical tasks such as addition, subtraction, division. Following are the different arithmetic operators.
Operator Description
+ To add the two values.
- To subtract the two values.
* To Multiply two values.
/ To divide the values.
% To give the remainder value.
++(Increment) To increment the operand value by 1.
--(Decrement) To decrease the operand value by 1

shape Example

Following is an example to understand the arithmetic, Java Operator. arthematicoperator.java [java] public class arthematicoperator { public static void main(String args[]) { int a = 100; int b = 200; int c = 250; int d = 250; System.out.println("Following are the values."); System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); System.out.println("b / a = " + (b / a) ); System.out.println("b % a = " + (b % a) ); System.out.println("c % a = " + (c % a) ); System.out.println("a++ = " + (a++) ); System.out.println("b-- = " + (a--) ); // Check the difference in d++ and ++d System.out.println("d++ = " + (d++) ); System.out.println("++d = " + (++d) ); } } [/java] Output When compile the code following is the output will be generated. [java]Following are the values. a + b = 300 a - b = -100 a * b = 20000 b / a = 2 b % a = 0 c % a = 50 a++ = 100 b-- = 101 d++ = 250 ++d = 252 [/java]

Relational Operators

shape Description

In Java Operators, relational operators are like a programming strategy to define the relationship between two entities. Following are the different types of relational operators.
Operator Description
== (equal to) To check the operand values are equal or not, if the condition is yes then it results true.
!= (not equal to) To check the operand values are equal or not, if the values are not equal then it results true.
> (greater than) Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
< (less than) Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
>= (greater than or equal to) Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
<= (less than or equal to) Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

shape Example

Following is an example which describes more about the relational operators. relationaloperator.java [java] public class relationaloperator { public static void main(String args[]) { int a = 100; int b = 200; System.out.println("a == b = " + (a == b) ); System.out.println("a != b = " + (a != b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a = a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) ); } } [/java] Output When compile the code following is the output will be generated. [java]a == b = false a != b = true a > b = false a = a = true b <= a = false[/java]

Bit-wise Operators

shape "Description"

In Java there are many bit-wise operators which can be applied to the integer types, long, etc. Following are the different types of bit-wise operators.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
< left shift
>> right shift

shape Example

Following is an example which describes more about the bit-wise operators. bitwise.java [java] public class bitwise { 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 ); 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; /* 15 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 15 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } } [/java] Output When compile the code following is the output will be generated. [java]a | b = 61 a ^ b = 49 ~a = -61 a <> 2 = 15 a >>> 2 = 15 [/java]

Logical Operators

shape Description

In Java Operator, Following are the three logical operators.
Operator Description
&& Logical AND(Example:(a && b) is false)
|| Logical OR(Example:(a || b) is true)
! Logical NOT(Example:((!a) is false)

shape Example

logicaloperator.java [java] public class logicaloperator { public static void main(String args[]) { boolean a = true; boolean b = false; System.out.println("a && b = " + (a&&b)); System.out.println("a || b = " + (a||b) ); System.out.println("!(a && b) = " + !(a && b)); } } [/java] Output When compile the code following is the output will be generated. [java]a && b = false a || b = true !(a && b) = true[/java]

Assignment Operators

shape Description

In Java Operators, Following are the assignment operators.
Operator Description
= To assign values from right side operands to left side operand.
+= To add right operand to the left operand and assign the result to left.
-= To subtract right operand from the left operand and assign the result to left operand
*= To multiply left operand with the right operand and assign the result to left operand.
/= To divide left operand with the right operand and assign the result to left operand.
%= To calculate modulus using two operands and assign the result to left operand.

shape Example

assignmentoperator.java [java] public class assignmentoperator { public static void main(String args[]) { int a = 100; int b = 200; int c = 0; c = a + b; System.out.println("c = a + b = " + c ); c += a ; System.out.println("c += a = " + c ); c -= a ; System.out.println("c -= a = " + c ); c *= a ; System.out.println("c *= a = " + c ); a = 10; c = 15; c /= a ; System.out.println("c /= a = " + c ); a = 10; c = 15; c %= a ; System.out.println("c %= a = " + c ); c <<= 2 ; System.out.println("c <>= 2 ; System.out.println("c >>= 2 = " + c ); c >>= 2 ; System.out.println("c >>= a = " + c ); c &= a ; System.out.println("c &= 2 = " + c ); c ^= a ; System.out.println("c ^= a = " + c ); c |= a ; System.out.println("c |= a = " + c ); } } [/java] Output When compile the code following is the result will be generated. [java]c = a + b = 300 c += a = 400 c -= a = 300 c *= a = 30000 c /= a = 1 c %= a = 5 c <>= 2 = 5 c >>= a = 1 c &= 2 = 0 c ^= a = 10 c |= a = 10 [/java]

Summary

shape Key Points

  • The instance of Operator is utilized for object reference variables.
  • The logical && operator doesn't check second condition if first condition is false. It checks second condition only if first one is true.