Operators and Expressions
In this section, we will dive into the world of operators and expressions in Java. Understanding how operators work and how to construct expressions is essential for writing effective and efficient code. Let's explore this fundamental topic in more detail.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical calculations in Java. Here are the commonly used arithmetic operators:
Addition (+)
The addition operator is used to add two numbers together. For example, int result = 5 + 3;
Subtraction (-)
The subtraction operator is used to subtract one number from another. For example, int result = 7 - 2;
Multiplication (*)
The multiplication operator is used to multiply two numbers. For example, int result = 4 * 6;
Division (/)
The division operator is used to divide one number by another. For example, double result = 10 / 3;
Modulus (%)
The modulus operator is used to find the remainder of a division operation. For example, int result = 15 % 4;
Assignment Operators
Assignment operators are used to assign values to variables. Here are some commonly used assignment operators:
Assignment (=)
The assignment operator is used to assign a value to a variable. For example, int age = 25;
Addition Assignment (+=)
The addition assignment operator adds the value on the right-hand side to the variable on the left-hand side and assigns the result to the variable. For example, int count = 10; count += 5;
Subtraction Assignment (-=)
The subtraction assignment operator subtracts the value on the right-hand side from the variable on the left-hand side and assigns the result to the variable. For example, int price = 100; price -= 20;
Comparison Operators
Comparison operators are used to compare values in Java. They return a boolean value (true or false) based on the comparison result. Here are some commonly used comparison operators:
Equal to (==)
The equal to operator checks if two values are equal. For example, boolean result = 5 == 5;
Not equal to (!=)
The not equal to operator checks if two values are not equal. For example, boolean result = 10 != 12;
Greater than (>)
The greater than operator checks if one value is greater than another. For example, boolean result = 8 > 5;
Less than (<)
The less than operator checks if one value is less than another. For example, boolean result = 3 < 7;
Greater than or equal to (>=)
The greater than or equal to operator checks if one value is greater than or equal to another. For example, boolean result = 6 >= 4;
Less than or equal to (<=)
The less than or equal to operator checks if one value is less than or equal to another. For example, boolean result = 9 <= 10;
Logical Operators
Logical operators are used to combine multiple boolean expressions and evaluate them as a single expression. Here are the three logical operators in Java:
Logical AND (&&)
The logical AND operator returns true if both boolean expressions are true. For example, boolean result = (5 > 3) && (10 < 15);
Logical OR (||)
The logical OR operator returns true if at least one of the boolean expressions is true. For example, boolean result = (8 == 8) || (6 != 7);
Logical NOT (!)
The logical NOT operator reverses the boolean value of an expression. For example, boolean result = !true;
These are just some of the operators available in Java. By using them effectively, you can perform complex calculations, make decisions, and control the flow of your programs. Practice using these operators and expressions to improve your proficiency in Java programming.