Skip to main content

Java Exception Handling MCQ Test

  Loading…

Operators in Java

Operators in Java are symbols that perform operations on variables or values. Java provides a wide range of operators, categorized into several types based on their functionality.

1. Arithmetic Operators

Arithmetic operators are used for mathematical operations.

  • + (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): Returns the remainder of the division.

int a = 10, b = 4;
  • int sum = a + b;    // 14
  • int difference = a - b; // 6
  • int product = a * b; // 40
  • int quotient = a / b; // 2
  • int remainder = a % b; // 2

2. Relational Operators

Relational operators are used to establish relationships between values.

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)


int x = 5, y = 10;

  • boolean isEqual = x == y;     // false
  • boolean isNotEqual = x != y;  // true
  • boolean isGreaterThan = x > y; // false

3. Logical Operators

Logical operators are used for logical operations.

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)


boolean condition1 = true, condition2 = false;

  • boolean andResult = condition1 && condition2; // false
  • boolean orResult = condition1 || condition2;  // true
  • boolean notResult = !condition1;               // false

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.
  • +=, -=, *=, /=, %= (Compound Assignment): Performs the operation and assigns the result to the left operand.


int num = 5;

  • num += 3; // num = num + 3;  // 8
  • num -= 2; // num = num - 2;  // 6
  • num *= 4; // num = num * 4;  // 24
  • num /= 3; // num = num / 3;  // 8
  • num %= 5; // num = num % 5;  // 3

5. Bitwise Operators

Bitwise operators perform operations at the bit level.

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise NOT)
  • << (Left shift)
  • >> (Right shift)


int p = 5, q = 3;

  • int bitwiseAnd = p & q;   // 1
  • int bitwiseOr = p | q;    // 7
  • int bitwiseXor = p ^ q;   // 6
  • int bitwiseNot = ~p;      // -6
  • int leftShift = p << 1;   // 10
  • int rightShift = p >> 1;  // 2

These are some of the essential Java operators. Understanding their usage is fundamental for writing effective and efficient Java programs.

Comments

Popular posts from this blog

Passing and Returning Objects in Java Methods

Passing and Returning Objects in Java Methods In Java, objects can be passed as parameters to methods and returned from methods just like other primitive data types. This allows for flexibility and the manipulation of object state within methods. Let's explore how passing and returning objects work in Java. Passing Objects as Parameters When you pass an object as a parameter to a method, you are essentially passing a reference to that object. This means that changes made to the object inside the method will affect the original object outside the method.  Example: class Car {     String model;     Car(String model) {         this.model = model;     } } public class CarProcessor {     // Method to modify the Car object     static void modifyCar(Car car, String newModel) {         car.model = newModel;     }     public static void main(String[] args) {       ...

Why is String[] args necessary in main() method in Java?

  Why is String[] args necessary in main() method in Java? In Java, the main method serves as the entry point for the program. The correct syntax for the main method is: public static void main (String[] args) { System.out.println( "Hello, Java!" ); } 🔹 Breaking it down: public → Accessible from anywhere. static → No need to create an object of the class to run it. void → No return value. main → Special method recognized by the JVM as the starting point. String[] args → Stores command-line arguments (optional but required by JVM). Why Can't We Skip String[] args ? JVM looks for main(String[] args) When you run a Java program, the JVM searches for the main method with exactly this signature : public static void main (String[] args) If you change or remove String[] args , the JVM cannot find the correct main() method and throws a runtime error (NoSuchMethodError). Java Specification Requires It The Java Language Specification (JLS) defines that main...