Evaluating expressions in java

What is Expression in Java?

Java Course - Mastering the Fundamentals

Here marks=90 is an expression that returns an int value. Example:

Here in this example, a+b-3.4 is an expression.

Simple Expressions

A simple expression is a literal method call or variable name without any usage of an operator. For example:

A simple expression in java has a type that can either be a primitive type or a reference type. In this example, 43 is a 32-bit integer, java is a string, 32L is a long 64-bit integer, etc.

Compound Expressions

It generally involves the usage of operators. It comprises one or more simple expressions, which are then integrated into a larger expression by using the operator. Consider another example in order to understand more clearly.

How to Evaluate Expression in Java?

Types of Operators

  • Additive operators It increases or decreases the numeric value by addition or subtraction. Here are a few additive operators:
    • Addition — syntax: operand1 + operand2 , it returns sum. For example 3+2=5.
    • Subtraction — syntax: operand1 — operand2 , it returns subtraction. For example 5-3=2.
    • Postdecrement — syntax: variable — — , it subtracts 1 from the variable’s value and returns the original value.
    • Postincrement- syntax: variable + + , it adds 1 to the variable’s value, stores and returns the result in the variable.
    • Predecrement- syntax: — — variable , it adds 1 to the variable’s value, stores the result in the variable and returns the updated value.
    • Preincrement — syntax: ++variable , it adds 1 to the variable, stores and returns the incremented value.
    • String concatenation- syntax: operand1 + oeprand2 , it returns the concatenation of two strings.
    • Bitwise AND- syntax: operand1 & operand2 , here operands can be the type of integer or char. The resultant bit is set to 1 when the corresponding operand’s bit is 1, else 0.
    • Bitwise complement- syntax: ~ operand , it flips the bit of operand. If the bit is 1 then it flips to 0 and wise-versa.
    • Bitwise exclusive OR- syntax: operand1 ^ operand2 , the resultant bit is set to 1 if the corresponding bit of the operand is 1 and the other operand’s bit is 0, else 0.
    • Bitwise inclusive OR- syntax: operand1 | operand2 , here the bit is set to 1 when both or either operand’s bit is 1 else the resultant bit is set to 0.
    • Conditional- syntax: operand1 ? operand2 : operand3 , where operand1 is of type boolean when operand1 is true, it returns operand2 else it returns operand3. Example: boolean status = true; int statusInt = (status)? 1 : 0;.
    • Conditional AND- syntax: operand1 && operand2 , here each operand is of type boolean and it returns true when both the operands are true, else false. It is also known as short-circuiting, for example: false && true= false.
    • Conditional OR- syntax: operand1 || operand2 , when one of the operands is true, it returns true, else it returns false. Example: true || false= true
    • Equality — syntax: operand1 == operand2 , where both operands must be comparable. It returns when both the values are true, else false. Example: ‘a’ == ‘b’, it returns false
    • Inequality — syntax: operand1 != operand2 , for example: ‘a’ != ‘b’, it returns true.
    • Logical AND- syntax: operand1 & operand2, here each operand is of type boolean. It returns true if both the operands are true, else false. For example, true and false= false.
    • Logical complement- syntax: !operand, it flips the value of operand, i.e if the value of the operand is true then the output is false, and vice-versa.
    • Multiplication — syntax: operand1 * operand2 . It returns the output as a multiplication of two values. Example, «3*4=12».
    • Division — syntax: operand1 / operand2 . It divides operand1 by operand2. For example, 15/5=3.
    • Remainder — syntax: operand1 % operand2 . It divides operand1 by operand2 leaving behind the remainder. For example, 14%3= 2.
    • Greater than- syntax: operator1 > operator2 . It returns true if operand1 > operand2, else false.
    • Greater than or equal to- syntax: operator1 >= operator2 . It returns true if operator1 is greater than or equal to operator2, else false.
    • Less than- syntax: operand1 < operand2 . It returns true if operand1 is less than operand2, else false.
    • Less than or equal to- syntax: operator1
    • Type checking- syntax: operand1 instanceof operator2 . Here operator1 is an object and operator2 is a class. It returns true if operator1 is an object of class operand2, else false.
Оцените статью