Operators in Java

Estimated reading: 3 minutes 25 views

Java is a versatile programming language that provides a rich set of operators to perform various operations on variables and values. Understanding these operators is fundamental for writing efficient and effective Java code. In this article, we’ll explore the different types of operators in Java, their functionalities, and how to use them.

Types of Operators in Java

Java operators can be categorized into the following types:

  1. Arithmetic Operators

  2. Unary Operators

  3. Relational Operators

  4. Logical Operators

  5. Bitwise Operators

  6. Assignment Operators

  7. Ternary Operator

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations:

OperatorDescriptionExampleExplanation
+Additiona + bAdds two values
-Subtractiona - bSubtracts the second value from the first
*Multiplicationa * bMultiplies two values
/Divisiona / bDivides the first value by the second
%Modulusa % bReturns the remainder of the division

Example Code:

				
					int a = 10;
int b = 5;
System.out.println("a + b = " + (a + b)); // Output: 15
				
			

2. Unary Operators

Unary operators operate on a single operand:

OperatorDescriptionExampleExplanation
+Unary plus+aRepresents a positive value (usually redundant)
-Unary minus-aNegates the value
++Increment++a or a++Increases the value by 1
--Decrement--a or a--Decreases the value by 1
!Logical complement!aInverts a boolean value

Example Code:

				
					int a = 10;
System.out.println("++a = " + ++a); // Output: 11
				
			

3. Relational Operators

Relational operators compare two values and return a boolean result:

OperatorDescriptionExampleExplanation
==Equal toa == bChecks if two values are equal
!=Not equal toa != bChecks if two values are not equal
>Greater thana > bChecks if the first value is greater
<Less thana < bChecks if the first value is smaller
>=Greater than or equal toa >= bChecks if the first value is greater or equal
<=Less than or equal toa <= bChecks if the first value is smaller or equal

Example Code:

				
					int a = 10;
int b = 20;
System.out.println("a == b: " + (a == b)); // Output: false
				
			

4. Logical Operators

Logical operators are used to combine multiple boolean expressions:

OperatorDescriptionExampleExplanation
&&Logical ANDa && bReturns true if both conditions are true
` `Logical OR`a b`Returns true if at least one condition is true
!Logical NOT!aInverts the boolean value

Example Code:

				
					boolean a = true;
boolean b = false;
System.out.println("a && b: " + (a && b)); // Output: false
				
			

5. Bitwise Operators

Bitwise operators perform operations on individual bits of integer types:

OperatorDescriptionExampleExplanation
&Bitwise ANDa & bPerforms AND operation on bits
``Bitwise OR`ab`Performs OR operation on bits
^Bitwise XORa ^ bPerforms XOR operation on bits
~Bitwise Complement~aInverts all bits
<<Left Shifta << 2Shifts bits to the left
>>Right Shifta >> 2Shifts bits to the right

Example Code:

				
					int a = 5;
int b = 3;
System.out.println("a & b: " + (a & b)); // Output: 1
				
			

6. Assignment Operators

Assignment operators assign values to variables:

OperatorDescriptionExampleExplanation
=Assigna = bAssigns value of b to a
+=Add and assigna += bAdds and assigns value
-=Subtract and assigna -= bSubtracts and assigns value
*=Multiply and assigna *= bMultiplies and assigns value
/=Divide and assigna /= bDivides and assigns value
				
					int a = 10;
a += 5;
System.out.println("a = " + a); // Output: 15
				
			

7. Ternary Operator

The ternary operator is a shorthand for if-else statements:

OperatorDescriptionExampleExplanation
?:Conditional assignmenta = (b > c) ? b : cAssigns the larger value between b and c to a
?:Conditional assignmenta = (b > c) ? b : cAssigns b to a if condition is true, otherwise assigns c
				
					int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // Output: 20
				
			

Conclusion

Understanding Java operators is essential for writing optimized and concise code. Whether performing arithmetic operations, comparisons, or logical evaluations, using the right operator enhances code efficiency and readability. Practice with different operators to gain a deeper understanding and mastery of Java programming.

Leave a Comment

Share this Doc

Operators in Java

Or copy link

CONTENTS