Logo
Unit 3 – Java Basics: Operators

Java Basics: Operators

Duration: 5 minutes

Hello, budding Java enthusiasts!

Today’s session is all about understanding and utilizing Java’s diverse operators. Operators in Java are like the gears that drive the logic in your code. They help perform operations on variables and values, enabling you to manipulate data and control the flow of your program.Let’s explore three essential types of operators in Java: arithmetic, relational, and logical operators.

1. Arithmetic Operators

Arithmetic operators are used for basic mathematical operations.Addition (+)

int sum = 5 + 3; // sum is 8

Subtraction (-)

int difference = 5 - 3; // difference is 2

Multiplication (*)

int product = 5 * 3; // product is 15

Division (/)

int quotient = 15 / 3; // quotient is 5

Modulo (%)

int remainder = 7 % 3; // remainder is 1

2. Relational Operators

Relational operators are used to compare values and return a boolean result.Equal to (==)

boolean isEqual = (5 == 3); // isEqual is false

Not Equal to (!=)

boolean isNotEqual = (5 != 3); // isNotEqual is true

Greater than (>)

boolean isGreater = (5 > 3); // isGreater is true

Less than (<)

boolean isLess = (5 < 3); // isLess is false

Greater than or equal to (>=)

boolean isGreaterOrEqual = (5 >= 3); // isGreaterOrEqual is true

Less than or equal to (<=)

boolean isLessOrEqual = (5 <= 3); // isLessOrEqual is false

3. Logical Operators

Logical operators are used to combine multiple boolean expressions.AND (&&)

boolean andResult = (5 > 3) && (5 > 4); // andResult is true

OR (||)

boolean orResult = (5 > 3) || (5 < 4); // orResult is true

NOT (!)

boolean notResult = !(5 == 3); // notResult is true

Exercise: Combining Operators

Now, let’s blend these operators in a mini-exercise. Try to create a Java program that uses a combination of arithmetic, relational, and logical operators. Here’s a small challenge to get you started:Determine if the sum of two numbers is greater than 10.Check if the product of two numbers is even or odd.

Conclusion

Fantastic work! You’re now familiar with the basic operators in Java. These tools are fundamental for writing effective and efficient Java code. Experiment with these operators, mix and match them, and see the different outcomes you can create.Remember, every step you take in learning Java opens up new possibilities. Keep coding, keep exploring, and enjoy the journey!Until next time, happy Java coding!

Next Tutorial: Control Structures in Java: Conditional Statements

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!