Java operators are one of the most important Java programming concepts you can and will ever need, as they are performed in every piece of software you will ever make. Furthermore, every Java program you’ve ever used was built with Java operators.
This post will cover everything you need to know to get started with using Java operators and how you can use them. You will also learn about the different types of things you can and cannot do with them and how that can improve your code. Finally, you will see code examples of Java operators in practical use and learn ways they can improve your code.
Without further ado, let’s jump right in.
What are Java operators?
Java operators are symbols that perform operations for various tasks, such as running test expressions in conditional blocks or even performing arithmetic.No matter how you pour the cup, Java operators are unavoidable, and you will undoubtedly need them to create any piece of software. Check out this video for a brief introduction to Java operators.
There are many different types of Java operators, and each serves a specific purpose let’s take a look at all the types of operators you will encounter.
Arithmetic operators are exactly what they sound like, operators that allow you to perform arithmetic operations like addition, subtraction, and multiplication. There are several different arithmetic operators, and the list below covers them in more detail.
Java Operators List: Arithmetic
+ The plus symbol is called the addition operator, which allows you to add two or more numbers. But it can also be used with strings to concatenate strings together.
– The minus symbol is used to perform subtraction operations with two or more numbers.
* The asterisk symbol is used to perform multiplication operations using two or more numbers.
/ The forward slash is used to perform division operations against two or more numbers.
% The Modulo operator is used to calculate the remainder after a division operation on two or more numbers.
++ The increment operator shorthand increments the value by 1
— The decrement operator shorthand decrements the value by 1
Assignment operators are used to assign a value to something; usually, the value is saved to a variable. These operators are great for assigning values while performing mathematic operations at the same time. Let’s take a look at some assignment operators and discuss how they work.
Java Operators List: Assignment
= The equal symbol is used to assign a value directly to a variable.
+= The plus and equal symbol together is a shorthand for an addition assignment. That is to say, this symbol combination means a variable is equal to its current value plus a new value.
-= The minus and equal symbol together is a shorthand for a subtraction assignment. That is to say, this symbol combination means a variable is equal to its current value subtracted from a new value.
*= The asterisk and equal symbol together is a shorthand for a multiplication assignment. That is to say, this symbol combination means a variable is equal to its current value times a new value.
/= The forward slash and equal symbol together is a shorthand for a division assignment. That is to say, this symbol combination means a variable is equal to its current value divided by a new value.
%= The percentage and equal symbol together is a shorthand for a modulo assignment. That is to say, this symbol combination means a variable is equal to its current value plus the remainder of the current value divided by a new value.
Relational Operators are used to make comparisons between two or more values and are frequently used in if statements and loops to set conditions for performing tasks. Conditional tasks are a lifeblood of any programming language and are often used to control the flow of the software.
Java Operators List: Relational
== The double equal symbol represents a relational operation used to compare two values for equality of type and data.
!= The exclamation point is used to reverse the meaning of an expression. In this case, the two symbols represent the not-equal expression used to compare inequality.
> The right arrow represents a greater than comparison, used to determine if the first value is greater than the second.
< The left arrow represents a less-than comparison, used to determine if the first value is less than the second.
>= The right arrow and equal symbol represent the greater-or-equal comparison, which determines if the first number is greater than or equal to the second.
<= The left arrow and equal symbol represent the less-or-equal comparison, which determines if the first number is less than or equal to the second.
Logical operators are used to help make conditional statements more robust by allowing additional conditions to statements. While there aren’t many logical operators — three to be exact — they provide a lot of extra power to your conditional statements.
Java Operators List: Logical
&& The logical-and operator combines two expressions. It returns true only if both expression1 and expression2 pass.
|| The logical-or operator combines two expressions. It returns true if either expression1 or expression2 pass.
! The logical not operator is used to inverse a boolean expression, which returns true if the expression is false and vice versa.
In Java, Bitwise operators perform operations on individual bits. Bitwise operators are not commonly used in Java but are complex and used to perform tasks on the bitwise level.
Java Operators List: Bitwise
~ The bitwise complement operator is used to invert bitwise values. For example, when used on a binary code, each one and zero gets reversed.
<< The left shift operator which moves the bits of an integer or enumeration type expression to the left
>> The right shift operator which moves the bits of an integer or enumeration type expression to the right
>>> The unsigned right shift operator evaluates the left-hand operand as an unsigned number and shifts the binary representation of that number by the number of bits.
& The bitwise AND operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the resulting bit is set to 1, or else it’s set to 0.
^ The bitwise exclusive OR operator compares each bit of its first operand to the corresponding bit of its
second operand. If the bit in one of the operands is 0 and the other is 1, the corresponding result is set to 1; else, it’s set to 0.
Java Operators With Examples
Below is a list of Java operator examples; these examples showcase how you can use each of the most common operators you will come across.
This block of code can serve as a cheat sheet you can save and use when you need to remind yourself of the syntax for each type of operator.
Next Steps For Using Java Operators
This post has covered the basics of understanding Java operators and how they are used in software development. You have also learned about the different types of operators, what they do, and ways you can use them.
Moving forward, you can perform different types of operations and expressions to learn more about how they work. Practicing different types of expressions in your code can help you keep your code clean and improve the performance of your software applications.