Day 1: Introduction to Java

icon picker
Day 3: Operators

Welcome to Day 3 of our journey to mastering Java! Today, we'll explore the topic of operators, which are an important part of any programming language. Operators allow you to do different things with your data, making your programs more flexible and capable. There are four main types of operators:
Arithmetic, Relational, Logical, and Assignment.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers. Java supports the following arithmetic operators:
Addition +: Adds two values together.
Subtraction -: Subtracts the second value from the first.
Multiplication *: Multiplies two values.
Division /: Divides the first value by the second.
Modulus %: Returns the remainder of the division operation.
Let's see some code examples to illustrate these operators:
javaCopy code
int a = 10;
int b = 5;
int sum = a + b; // 15
int difference = a - b; // 5
int product = a * b; // 50
int quotient = a / b; // 2
int remainder = a % b; // 0

Relational Operators

Relational operators are used to compare values and determine the relationship between them. These operators return a boolean result (true or false). Java supports the following relational operators:
Equal to ==: Checks if two values are equal.
Not equal to !=: Checks if two values are not equal.
Greater than >: Checks if the first value is greater than the second.
Less than <: Checks if the first value is less than the second.
Greater than or equal to >=: Checks if the first value is greater than or equal to the second.
Less than or equal to <=: Checks if the first value is less than or equal to the second.
Here are some code examples:
javaCopy code
int x = 5;
int y = 10;
boolean isEqual = x == y; // false
boolean isNotEqual = x != y; // true
boolean isGreaterThan = x > y; // false
boolean isLessThan = x < y; // true
boolean isGreaterOrEqual = x >= y; // false
boolean isLessOrEqual = x <= y; // true

Logical Operators

Logical operators are used to perform logical operations on boolean values. They allow you to combine or modify the result of boolean expressions. Java has three main logical operators:
Logical AND &&: Returns true if both operands are true.
Logical OR ||: Returns true if at least one operand is true.
Logical NOT !: Negates the value of a boolean expression.
Here's how you can use them:
javaCopy code
boolean isJavaFun = true;
boolean isLearning = true;
boolean result1 = isJavaFun && isLearning; // true
boolean result2 = isJavaFun || isLearning; // true
boolean result3 = !isJavaFun; // false

Assignment Operators

Assignment operators are used to assign values to variables. In addition to the basic assignment operator =, Java also provides compound assignment operators, which combine an operation with assignment. Here are a few examples:
javaCopy code
int value = 5;
value += 3; // Equivalent to: value = value + 3; (value is now 8)
value -= 2; // Equivalent to: value = value - 2; (value is now 6)
value *= 4; // Equivalent to: value = value * 4; (value is now 24)
value /= 3; // Equivalent to: value = value / 3; (value is now 8)
value %= 5; // Equivalent to: value = value % 5; (value is now 3)

Exercise Questions

Calculate the area of a rectangle given its length and width. Use appropriate arithmetic operators.
Check if a given number is even or odd using relational and logical operators.
Implement a basic calculator that performs addition, subtraction, multiplication, and division based on user input.

Exercise Answers

To calculate the area of a rectangle:
javaCopy code
int length = 10;
int width = 5;
int area = length * width;
System.out.println("The area of the rectangle is: " + area);

To check if a number is even or odd:
javaCopy code
int number = 7;
boolean isEven = (number % 2 == 0);
System.out.println("Is the number even? " + isEven);

Basic calculator example:
javaCopy code
import java.util.Scanner;

public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();

System.out.print("Enter the operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);

double result = 0;

switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Division by zero is not allowed.");
return;
}
break;
default:
System.out.println("Invalid operator.");
return;
}

System.out.println("Result: " + result);
}
}

That concludes our exploration of operators in Java for Day 3. Operators are fundamental tools for performing calculations, comparisons, and logical operations in your Java programs. Be sure to practice using these operators to build your programming skills!
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.