- What is Expression in Java?
- Simple Expressions
- Compound Expressions
- How to Evaluate Expression in Java?
- Types of Operators
- Operator Precedence
- Associativity Rules
- Types of Expressions in Java
- Infix Expression
- Postfix Expression
- Prefix Expression
- Examples
- Expressions That Produce a Value
- Expressions That Assign a Variable
- Java Expression with No Results
- Java Statements
- Difference between Statements and Expressions in Java
- Learn More
- Conclusion
What is Expression in Java?
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.
Postfix Expression
It is an expression in which operators are used after operands.
Prefix Expression
It is an expression in which operators are used before an operand.
Examples
Expressions That Produce a Value
Program to produce a value from an expression.
Here are some of these expressions that produce a valueExplanation:
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