Skip to main content

Java Exception Handling MCQ Test

  Loading…

Java Expressions

Java Expressions

In Java, an expression is a combination of variables, operators, and method invocations that evaluates to a single value. Expressions can be as simple as a single variable or a more complex combination of operations. Understanding expressions is fundamental to programming in Java.

Types of Expressions:

1. Arithmetic Expressions

Arithmetic expressions involve numerical operations.

int a = 5, b = 3;

  • int sum = a + b;      // Addition
  • int difference = a - b; // Subtraction
  • int product = a * b;   // Multiplication
  • int quotient = a / b;  // Division
  • int remainder = a % b; // Modulus

2. Relational Expressions

Relational expressions compare values and result in a boolean value.

int x = 10, y = 20;

  • boolean isEqual = x == y;     // Equal to
  • boolean isNotEqual = x != y;  // Not equal to
  • boolean isGreaterThan = x > y; // Greater than


3. Logical Expressions

Logical expressions involve boolean operators.

boolean condition1 = true, condition2 = false;

  • boolean andResult = condition1 && condition2; // Logical AND
  • boolean orResult = condition1 || condition2;  // Logical OR
  • boolean notResult = !condition1;               // Logical NOT

4. Assignment Expressions

Assignment expressions assign values to variables.

int num = 5;

  • num += 3; // Compound assignment (num = num + 3)

5. Conditional (Ternary) Expressions

Conditional expressions provide a compact way of writing if-else statements.

int a = 10, b = 5;

  • int max = (a > b) ? a : b; // If a is greater than b, assign a; otherwise, assign b.

6. Bitwise Expressions

Bitwise expressions operate on individual bits of integer values.

int p = 5, q = 3;

  • int bitwiseAnd = p & q; // Bitwise AND
  • int bitwiseOr = p | q;  // Bitwise OR
  • int bitwiseXor = p ^ q; // Bitwise XOR
  • int bitwiseComplement=~p;  //Bitwise 1's Complement


7. Method Call Expressions

Method call expressions involve invoking methods.

String greeting = "Hello, ";

String name = "John";

  • String message = greeting.concat(name); // Method invocation


8. Object Creation Expressions

Object creation expressions create instances of classes.

Date currentDate = new Date(); // Creating a new Date object


9. Array Creation Expressions

Array creation expressions create arrays.


  • int[] numbers = new int[5]; // Creating an integer array of size 5


These are just a few examples of the diverse types of expressions in Java. Expressions form the core of Java programs, allowing developers to manipulate data, make decisions, and control the flow of their applications. Understanding how to construct and use expressions is crucial for effective Java programming.

Comments

Popular posts from this blog

The Collection Interface.

  The Collection Interface. 

OracleJDK vs OpenJDK

Oracle JDK (Java Development Kit): Oracle JDK is the official reference implementation of the Java Platform, Standard Edition (Java SE). It included the JRE along with development tools. OpenJDK: An open-source alternative to Oracle JDK, OpenJDK is a community-driven project. It provides a free and open-source implementation of the Java Platform, and many other JDKs, including Oracle JDK, are derived from OpenJDK. Below is a simple table highlighting some key points of comparison between Oracle JDK and OpenJDK: Feature Oracle JDK OpenJDK Vendor Oracle Corporation OpenJDK Community Licensing Commercial (Paid) with Oracle Binary Code License Agreement Open Source (GNU General Public License, version 2, with the Classpath Exception) Support Commercial support available with Oracle Support subscription Community support, may have commercial support options from other vendors Updates and Patches Regular updates with security patches provided by Oracle Updates and patches contributed by the ...