30 Sep Operators in Java
Operators perform operations by taking one or more value, to give another value. These operations are performed on variables and values. Operators in Java are discussed here. For example:
1 2 3 |
a + b |
Here are the operators in Java:
Java Arithmetic Operators
Arithmetic operators perform arithmetical operations, such as addition, subtraction, division, multiplication etc.
Arithmetic Operator | Name | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
** | Exponentiation | a ** b |
Here’s an example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class StudyDemo1 { public static void main(String []args) { int a, b, c; a = 50; b = 100; c = 150; System.out.println("Studyopedia Java Tutorial"); System.out.println("Arithmetic Operators:\n"); System.out.println("Addition Arithmetic Operator"); System.out.println("Value of (a + b) is "+(a+b)); System.out.println("\nSubtract Arithmetic Operator"); System.out.println("Value of (b - a) is "+(b-a)); System.out.println("\nDivision Arithmetic operator"); System.out.println("Value of (c / a) is "+(c/a)); System.out.println("\nMultiplication Arithmetic operator"); System.out.println("Value of (a * b) is "+(a*b)); System.out.println("\nModulus Arithmetic operator"); System.out.println("Value of (c % b) is "+(c%b)); } } |
Here’s the output,
Java Assignment Operator
Use the Assignment Operator when you need to assign a value to a variable. Assignment means assigning the value of right operand(s) to the left operand.
Assignment Operator | Name | Example |
---|---|---|
= | Assignment | a = b |
Java Arithmetic Assignment Operator
Use the Arithmetic Assignment Operator when you need to perform arithmetic operations such as add, subtract, divide, multiple, etc and at the same time assign a value.
Arithmetic Assignment Operator | Name | Example |
---|---|---|
+= | Add AND assignment | a += b is same as a = a + b |
-= | Subtract AND assignment | a -= b is same as a = a – b |
*= | Multiply AND assignment | a *= b is same as a = a * b |
/= | Divide AND assignment | a /= b is same as a = a / b |
%= | Modulus AND assignment | a %= b is same as a = a % b |
Here’s an example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
class StudyDemo1 { public static void main(String []args) { int a, b, c, d, e; a = 5; b = 10; c = 15; d = 20; e = 30; System.out.println("Studyopedia Java Tutorial"); System.out.println("Arithmetic Assignment Operators:\n"); a += 5; System.out.println("Add AND assignment operator"); System.out.println("Value of a: = "+a); b -= 5; System.out.println("\nSubtract AND assignment operator"); System.out.println("Value of b: = "+b); c *= 5; System.out.println("\nMultiple AND assignment operator"); System.out.println("Value of c: = "+c); d /= 10; System.out.println("\nDivide AND assignment operator"); System.out.println("Value of d: = "+d); e %= 9; System.out.println("\nModulus AND assignment operator"); System.out.println("Value of e: = "+e); } } |
Here’s the output,
Java Relational/ Comparison operators
Compare two values with Java relational operators, which is also known as Comparison Operators.
Java Relational Operator | Name | Example |
---|---|---|
= =' | Equal | a == b isn’t true |
!= | Not Equal | a != b returns true |
> | Greater Than | a > b isn’t true |
< | Less Than | a < b is true |
>= | Greater than or equal to | a >= b isn’t true |
<= | Less than or equal to | a <= b is true |
Here’s an example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class StudyDemo1 { public static void main(String []args) { int a, b, c, d; a = 5; b = 10; c = 15; System.out.println("Studyopedia Java Tutorial"); System.out.println("\nRelational Operators:"); System.out.println("Value of (a == b) is " + (a == b) ); System.out.println("Value of (a != b) is " + (a != b) ); System.out.println("\nValue of (b > c) is " + (b > c) ); System.out.println("Value of (c < a) is " + (c < a) ); System.out.println("\nValue of (c >= b) is " + (c >= b) ); System.out.println("Value of (b <= c) is " + (b <= c) ); } } |
Here’s the output,
Java Logical operators
Logical operators combine conditional statements. Considering Boolean variables a and b,
Java Logical Operators | Name | Example |
---|---|---|
&& | Logical AND operator | a && b is true if a and b are both true |
|| | Logical OR Operator | a || b is true if either a or b is true |
^ | Logical XOR operator | a xor b is true if either a or b is true (not both) |
! | Logical NOT Operator | !a is true if a is not true |
Here’s the output,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class StudyDemo1 { public static void main(String []args) { boolean a, b, c, d; a = true; b = false; c = true; d = false; System.out.println("Studyopedia Java Tutorial"); System.out.println("\nLogical Operators:"); System.out.println("Value of (a && b) = " + (a&&b)); System.out.println("Value of (b || c) = " + (b||c) ); System.out.println("Value of !(a && d) = " + !(a && d)); System.out.println("Value of (a ^ d) = " + (a ^ d)); } } |
Here’s the output,
Java Unary operators
Unary operators include pre as well as post increment and decrement operators,
Here’s the example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class StudyDemo1 { public static void main(String []args) { int a, b; a = 5; System.out.println("Studyopedia Java Tutorial"); System.out.println("Unary Operators:"); System.out.println("\nUnary Increment Operators"); System.out.println(a++); System.out.println(++a); System.out.println("\nUnary Decrement Operators\n"); System.out.println(a--); System.out.println(--a); } } |
Here’s the output,
Java Ternary/ Conditional Operator
Conditional operator evaluates Boolean expressions, with has three operands. It is also known as the ternary operator. The role is to assign a value to a variable, from two given set options.
Here’s how to use it,
1 2 3 |
variable = (expression) ? value1 ( if true) : value2 (if false) |
Here’s an example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class StudyDemo1 { public static void main(String []args) { int a, b; a = 5; System.out.println("Studyopedia Java Tutorial"); System.out.println("Conditional Operator"); b = (a > 2) ? 100: 50; System.out.println( " b = " + b ); } } |
Here’s the output,
Bitwise Operator
These operators only work with the following data types,
- byte
- short
- int
- long
- char
Bitwise operators are discussed in the next lesson.
No Comments