All expression types 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.
    • Left shift- the syntax of left shift is: operand1
    • Right shift- it is of two types, signed and unsigned.
      • Signed right shift- the syntax of signed right shift is operand1 >> operand2 , again each operand is of type integer of character. It shifts the binary representation of operand1 right by a number of bits specified by operand2.
      • Unsigned right shift- the syntax of this is: operand1 >>> operand2 . In each shift, a rightmost bit is discarded and a zero is shifted to the leftmost bit. This shift doesn’t preserve negative value.

      Operator Precedence

      It is used for determining the order in which the operators in the expression are evaluated. For example:

      Here two operand shares a command operand which is 4 in this case. The operator with the highest precedence is executed first (which in this case is *) or the expression is evaluated from left to right.

      Associativity Rules

      It specifies the order in which the expression is executed which is generally from left to right. There are 3 types of associativity:

      • Left-associative: operators are accessed from left to right.
      • Right-associative: operators are accessed from right to left.
      • No associativity: operators are executed in random order.

      Types of Expressions in Java

      Infix Expression

      It is an expression in which the operators are used between operands.

      infix-expression

      Postfix Expression

      It is an expression in which operators are used after operands.

      postfix-expression

      Prefix Expression

      It is an expression in which operators are used before an operand.

      prefix-expression

      Examples

      Expressions That Produce a Value

      Program to produce a value from an expression.
      Here are some of these expressions that produce a value

      Explanation:

      In this, a wide variety of arithmetic, comparison, and conditional operators are used. The 1/2 is an expression that produces a value as a result of division while 6%3 produces a remainder as a result of the mod. The pi+(10*2) is an equation and produces a value as a result.

      Expressions That Assign a Variable

      Program to assign a variable by using an expression

      Explanation: In this example, the assignment operator is used for assigning the value to a variable. It assigns the value on the right to the variable on left.

      Java Expression with No Results

      Program in Java to produce no result by using an expression

      Explanation: Here, the variable product is the only variable that is changed. The variables y and z are unchanged.

      Java Statements

      A statement in java is a command that is to be executed and can include one or more expressions. Types of statements in java:

      • Expression statement that changes the value of variables, method calls, and objects.
      • Declaration statement is used for declaring variables.
      • Control flow statements are used for determining the order in which the statements are executed. Example:

      Difference between Statements and Expressions in Java

      Statements Expressions
      It is used for performing actions. Is it used for evaluating a value.
      It cannot be made up by using other statements. It can be made using various expressions.
      Statements are of type void in most programming languages. There are types of expression in every programming language.
      Statements are pre-defined in most programming languages. These are usually defined by the users.
      These are evaluated based on the order or sequence of statements. These are evaluated based on rules of operations.

      Learn More

      Conclusion

      • An expression in Java is a series of operators, variables, and method calls constructed according to the syntax o the language for evaluating a single value.
      • 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 expression 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.
      • Types of Operators:
        • Additive operators
        • Array index operator
        • Assignment operator
        • Bitwise operator
        • Cast operator
        • Conditional operator
        • Equality operator
        • Logical operator
        • Member access operator
        • Method call operator
        • Multiplicative operator
        • Object creation operator
        • Relational operator
        • Shift operator
        • Unary minus/plus operator

        Источник

        Expressions, Statements, and Blocks

        Now that you understand variables and operators, it’s time to learn about expressions, statements, and blocks. Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks.

        Expressions

        An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. You’ve already seen examples of expressions, illustrated in bold below:

        int cadence = 0; anArray[0] = 100; System.out.println("Element 1 at index 0: " + anArray[0]); int result = 1 + 2; // result is now 3 if (value1 == value2) System.out.println("value1 == value2"); 

        The data type of the value returned by an expression depends on the elements used in the expression. The expression cadence = 0 returns an int because the assignment operator returns a value of the same data type as its left-hand operand; in this case, cadence is an int . As you can see from the other expressions, an expression can return other types of values as well, such as boolean or String .

        The Java programming language allows you to construct compound expressions from various smaller expressions as long as the data type required by one part of the expression matches the data type of the other. Here’s an example of a compound expression:

        In this particular example, the order in which the expression is evaluated is unimportant because the result of multiplication is independent of order; the outcome is always the same, no matter in which order you apply the multiplications. However, this is not true of all expressions. For example, the following expression gives different results, depending on whether you perform the addition or the division operation first:

        You can specify exactly how an expression will be evaluated using balanced parenthesis: ( and ). For example, to make the previous expression unambiguous, you could write the following:

        (x + y) / 100 // unambiguous, recommended

        If you don’t explicitly indicate the order for the operations to be performed, the order is determined by the precedence assigned to the operators in use within the expression. Operators that have a higher precedence get evaluated first. For example, the division operator has a higher precedence than does the addition operator. Therefore, the following two statements are equivalent:

        x + y / 100 
        x + (y / 100) // unambiguous, recommended

        When writing compound expressions, be explicit and indicate with parentheses which operators should be evaluated first. This practice makes code easier to read and to maintain.

        Statements

        Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon ( ; ).

        • Assignment expressions
        • Any use of ++ or —
        • Method invocations
        • Object creation expressions

        Such statements are called expression statements. Here are some examples of expression statements.

        // assignment statement aValue = 8933.234; // increment statement aValue++; // method invocation statement System.out.println("Hello World!"); // object creation statement Bicycle myBike = new Bicycle();

        In addition to expression statements, there are two other kinds of statements: declaration statements and control flow statements. A declaration statement declares a variable. You’ve seen many examples of declaration statements already:

        // declaration statement double aValue = 8933.234;

        Finally, control flow statements regulate the order in which statements get executed. You’ll learn about control flow statements in the next section, Control Flow Statements

        Blocks

        A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following example, BlockDemo , illustrates the use of blocks:

        class BlockDemo < public static void main(String[] args) < boolean condition = true; if (condition) < // begin block 1 System.out.println("Condition is true."); > // end block one else < // begin block 2 System.out.println("Condition is false."); > // end block 2 > >

        Previous page: Questions and Exercises: Operators
        Next page: Questions and Exercises: Expressions, Statements, and Blocks

        Источник

        Читайте также:  Градиент
Оцените статью