Java is a powerful programming language that provides various operators to perform arithmetic calculations, assign values to variables, compare different values, and evaluate logical expressions. Understanding these operators is essential for writing efficient and effective Java code. In this article, we will explore the commonly used arithmetic, assignment, comparison, and logical operators in Java.
Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operators work on numeric data types like integers (int) or floating-point numbers (float or double).
Here's an example of using arithmetic operators:
int num1 = 10;
int num2 = 5;
int sum = num1 + num2; // Addition
int difference = num1 - num2; // Subtraction
int product = num1 * num2; // Multiplication
int quotient = num1 / num2; // Division
int remainder = num1 % num2; // Modulus
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
The output of the above code will be:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Assignment operators are used to assign values to variables. The most common assignment operator is the equals sign (=). However, there are also compound assignment operators that combine an arithmetic operation with an assignment.
Here's an example of using assignment and compound assignment operators:
int x = 10;
x += 5; // Equivalent to x = x + 5
System.out.println("x: " + x); // Output: 15
int y = 20;
y *= 2; // Equivalent to y = y * 2
System.out.println("y: " + y); // Output: 40
Comparison operators are used to compare two values and return a boolean result (true or false). These operators are often used in conditional statements or loops.
Here's an example of using comparison operators:
int num1 = 10;
int num2 = 5;
boolean isEqual = (num1 == num2); // Equal to
boolean isNotEqual = (num1 != num2); // Not equal to
boolean isGreater = (num1 > num2); // Greater than
boolean isLesser = (num1 < num2); // Less than
boolean isGreaterOrEqual = (num1 >= num2); // Greater than or equal to
boolean isLesserOrEqual= (num1 <= num2);// Less than or equal to
System.out.println("Is Equal? " + isEqual);
System.out.println("Is Not Equal? " + isNotEqual);
System.out.println("Is Greater? " + isGreater);
System.out.println("Is Lesser? " + isLesser);
System.out.println("Is Greater Or Equal? " +isGreaterOrEqual );
System.out.println("Is Lesser Or Equal?" +isLesserOrEqual );
The output of the above code will be:
Is Equal? false
Is Not Equal? true
Is Greater? true
Is Lesser ? false
Is greater Or equal ? true
Is lesser Or equal ?false
Logical operators are used for combining multiple conditions and evaluating logical expressions. The three main logical operators in Java are AND (&&), OR (||), and NOT (!).
Here's an example of using logical operators:
int age = 25;
boolean isStudent = true;
if (age > 18 && isStudent) {
System.out.println("You are eligible for a student discount.");
}
boolean hasLicense = false;
boolean hasExperience = true;
if (hasLicense || hasExperience) {
System.out.println("You can apply for the job.");
}
boolean isValid = !isStudent;
System.out.println("Is Valid? " + isValid);
The output of the above code will be:
You are eligible for a student discount.
You can apply for the job.
Is Valid? false
In conclusion, understanding arithmetic, assignment, comparison, and logical operators in Java is crucial for performing calculations, assigning values to variables, comparing different values, and evaluating logical expressions. By utilizing these operators effectively in your code, you can enhance its functionality and efficiency.
noob to master © copyleft