Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Operator

  • Operators in Java are special symbols or keywords used to perform operations on variables and values. Java supports a wide range of operators for tasks like arithmetic calculations, comparison, logical operations, and more. Here's a detailed explanation of the various types of operators in Java:

    1. Arithmetic Operators:

    Arithmetic operators are used for mathematical calculations.

    • + (Addition): Adds two operands.

    • - (Subtraction): Subtracts the right operand from the left operand.

    • * (Multiplication): Multiplies two operands.

    • / (Division): Divides the left operand by the right operand.

    • % (Modulus): Divides the left operand by the right operand and returns the remainder.

    2. Relational Operators:

    Relational operators are used to compare two values.

    • == (Equal to): Checks if two operands are equal.

    • != (Not equal to): Checks if two operands 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.

    3. Logical Operators:

    Logical operators are used to perform logical operations on boolean values.

    • && (Logical AND): Returns true if both conditions on the left and right are true.

    • || (Logical OR): Returns true if either of the conditions on the left or right is true.

    • ! (Logical NOT): Reverses the logical state of its operand.

    4. Assignment Operators:

    Assignment operators are used to assign values to variables.

    • = (Assignment): Assigns the value on the right to the variable on the left.

    • += (Add and Assign): Adds the right operand to the left operand and assigns the result to the left operand.

    • -= (Subtract and Assign): Subtracts the right operand from the left operand and assigns the result to the left operand.

    • *= (Multiply and Assign): Multiplies the left operand with the right operand and assigns the result to the left operand.

    • /= (Divide and Assign): Divides the left operand by the right operand and assigns the result to the left operand.

    • %= (Modulus and Assign): Performs modulus on the left operand with the right operand and assigns the result to the left operand.

    5. Unary Operators:

    Unary operators operate on a single operand.

    • ++ (Increment): Increases the value of the operand by 1.

    • -- (Decrement): Decreases the value of the operand by 1.

    • + (Positive): Indicates a positive value. (Usually, numbers are already positive, so this is rarely used.)

    • - (Negation): Negates the value of the operand.

    6. Bitwise Operators:

    Bitwise operators perform operations on individual bits of a number.

    • & (Bitwise AND): Performs a bitwise AND operation.

    • | (Bitwise OR): Performs a bitwise OR operation.

    • ^ (Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.

    • ~ (Bitwise NOT): Flips the bits of the operand.

    7. Conditional (Ternary) Operator:

    The conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement.

    • ? : (Conditional Operator): Evaluates a condition and returns one of two values based on whether the condition is true or false.

    Understanding and mastering these operators are essential for writing efficient and effective Java code. They enable developers to perform a wide array of operations, from basic arithmetic calculations to complex logical evaluations.

PreviousDate and FormatNextCondition

Last updated 1 year ago