Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /home/bibiizjb/ptutorials.com/en/account/functions/check_point_2.php on line 25
Java Operators and Control Flow Tutorial

Java Operators Tutorial

Operators in Java are symbols that perform operations on variables and values. They are essential for performing calculations, comparisons, and logical decisions in your programs. This tutorial will cover the different types of operators in Java, including arithmetic, relational, logical, bitwise, and assignment operators.

By the end of this tutorial, you'll understand how to use Java operators effectively to manipulate data and control the flow of your programs.

Types of Operators in Java

  • Arithmetic Operators: Perform basic mathematical operations.
  • Relational Operators: Compare two values or expressions.
  • Logical Operators: Perform logical operations on boolean values.
  • Bitwise Operators: Perform operations on individual bits of integer values.
  • Assignment Operators: Assign values to variables.
  • Miscellaneous Operators: Includes ternary and instanceOf operators.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.

  • +: Addition (e.g., 5 + 3 results in 8).
  • -: Subtraction (e.g., 5 - 3 results in 2).
  • *: Multiplication (e.g., 5 * 3 results in 15).
  • /: Division (e.g., 10 / 2 results in 5).
  • %: Modulus (e.g., 10 % 3 results in 1).
  • ++: Increment (e.g., int x = 5; x++; results in x = 6).
  • --: Decrement (e.g., int x = 5; x--; results in x = 4).
int a = 10, b = 3;
System.out.println("Addition: " + (a + b)); // 13
System.out.println("Subtraction: " + (a - b)); // 7
System.out.println("Multiplication: " + (a * b)); // 30
System.out.println("Division: " + (a / b)); // 3
System.out.println("Modulus: " + (a % b)); // 1

Relational Operators

Relational operators are used to compare two values or expressions and return a boolean result (true or false).

  • ==: Equal to (e.g., 5 == 3 results in false).
  • !=: Not equal to (e.g., 5 != 3 results in true).
  • >: Greater than (e.g., 5 > 3 results in true).
  • <: Less than (e.g., 5 < 3 results in false).
  • >=: Greater than or equal to (e.g., 5 >= 5 results in true).
  • <=: Less than or equal to (e.g., 5 <= 3 results in false).
int x = 5, y = 3;
System.out.println("Equal to: " + (x == y)); // false
System.out.println("Not equal to: " + (x != y)); // true
System.out.println("Greater than: " + (x > y)); // true
System.out.println("Less than: " + (x < y)); // false

Logical Operators

Logical operators are used to combine multiple conditions and return a boolean result.

  • &&: Logical AND (e.g., true && false results in false).
  • ||: Logical OR (e.g., true || false results in true).
  • !: Logical NOT (e.g., !true results in false).
boolean a = true, b = false;
System.out.println("AND: " + (a && b)); // false
System.out.println("OR: " + (a || b)); // true
System.out.println("NOT: " + (!a)); // false

Assignment Operators

Assignment operators are used to assign values to variables.

  • =: Simple assignment (e.g., int x = 5;).
  • +=: Add and assign (e.g., x += 3; is equivalent to x = x + 3;).
  • -=: Subtract and assign (e.g., x -= 3; is equivalent to x = x - 3;).
  • *=: Multiply and assign (e.g., x *= 3; is equivalent to x = x * 3;).
  • /=: Divide and assign (e.g., x /= 3; is equivalent to x = x / 3;).
  • %=: Modulus and assign (e.g., x %= 3; is equivalent to x = x % 3;).
int x = 10;
x += 5; // x = 15
x -= 3; // x = 12
x *= 2; // x = 24
x /= 4; // x = 6
x %= 5; // x = 1

Ternary Operator

The ternary operator is a shorthand for an if-else statement.

  • Syntax: condition ? expression1 : expression2.
  • If the condition is true, expression1 is executed; otherwise, expression2 is executed.
int x = 10, y = 5;
String result = (x > y) ? "x is greater" : "y is greater";
System.out.println(result); // x is greater

This tutorial covered the most commonly used operators in Java. Practice using these operators to become proficient in writing Java programs.

0 Interaction 0 Views 0 likes
Heart Button
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials, but please remember that tutorials may not be 100% accurate, as occasional mistakes can happen. Once you've mastered the language, we highly recommend consulting the official documentation to stay updated with the latest changes. If you spot any errors, please feel free to report them to help us improve.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home