Operators are symbols used to perform operations on variables and values in Java. Expressions are combinations of variables, values, and operators that can be evaluated to produce a result. In this section of the Core Java tutorial, we'll explore various operators and expressions, providing detailed explanations and examples.
Arithmetic Operators:
Arithmetic operators are used to perform basic mathematical operations on numeric values. Java supports the following arithmetic operators:
Addition ( '+'): Adds two numbers together.
Subtraction ( '-'): Subtracts the right operand from the left operand.
Multiplication ( '*'): Multiplies two numbers.
Division ( '/'): Divides the left operand by the right operand.
Modulus ( '%'): Computes the remainder when the left operand is divided by the right operand.
Example:
int num1 = 10;
int num2 = 5;
int sum = num1 + num2; // sum = 15
int difference = num1 - num2; // difference = 5
int product = num1 * num2; // product = 50
int quotient = num1 / num2; // quotient = 2
int remainder = num1 % num2; // remainder = 0
Assignment Operators:
Assignment operators are used to assign values to variables. The basic assignment operator is '='. There are also compound assignment operators that combine arithmetic operations with assignment.
Basic Assignment ( '='): Assigns the value on the right to the variable on the left.
Compound Assignment ( '+=', '-=', '*=', '/=', '%='): Performs an operation and assigns the result to the left operand.
Example:
int x = 5;
x += 3; // Equivalent to x = x + 3; (x is now 8)
x -= 2; // Equivalent to x = x - 2; (x is now 6)
x *= 4; // Equivalent to x = x * 4; (x is now 24)
x /= 3; // Equivalent to x = x / 3; (x is now 8)
x %= 5; // Equivalent to x = x % 5; (x is now 3)
Comparison Operators:
Comparison operators are used to compare two values and return a Boolean result ( 'true' or 'false').
Equal to ( '=='): Checks if two values are equal.
Not equal to ( '!='): Checks if two values are not equal.
Greater than ( '>'): Checks if the left operand is greater than the right operand.
Less than ( '<'): Checks if the left operand is less than the right operand.
Greater than or equal to ( '>='): Checks if the left operand is greater than or equal to the right operand.
Less than or equal to ('<='): Checks if the left operand is less than or equal to the right operand.
Example:
int a = 5;
int b = 3;
boolean isEqual = (a == b); // isEqual is false
boolean isNotEqual = (a != b); // isNotEqual is true
boolean isGreater = (a > b); // isGreater is true
boolean isLess = (a < b); // isLess is false
boolean isGreaterOrEqual = (a >= b); // isGreaterOrEqual is true
boolean isLessOrEqual = (a <= b); // isLessOrEqual is false
Logical Operators:
Logical operators are used to perform logical operations on Boolean values.
Logical AND ('&&'): Returns 'true' if both operands are 'true'.
Logical OR ( '||'): Returns 'true' if at least one operand is 'true'.
Logical NOT ( '!'): Inverts the Boolean value of the operand.
The conditional operator ( '? :') is a shorthand way to write an 'if-else' statement. It evaluates a Boolean expression and returns one of two values depending on whether the expression is 'true' or 'false'.
Example:
int x = 10;
int y = 5;
int max = (x > y) ? x : y; // max is 10
In this example, 'max' is assigned the value of 'x' because the condition '(x > y)' is 'true'.
These are some of the fundamental operators and expressions in Java. Understanding and mastering these operators is essential for writing effective Java programs. In the following sections of this Core Java tutorial, we will explore more advanced concepts and how to use these operators in real-world programming scenarios.