Java Expressions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Expressions

Expression – a construct made up of variables and operators that evaluates to a single value. For example, 11 +
13 and
x = 12 are expressions. An expression can also return other types of values, such as Boolean or String.
Arithmetic Expressions
Arithmetic expression – an expression that returns a numeric value. These expressions consist of arithmetic
operators, operands, and assignment operators. There are two (2) types of arithmetic expressions:
 Integral expression – If all operands in an expression are integers and the expression returns an integer
type value, the expression is an integral expression. For example:
x = 11 + 3 * 24 – 5 / 4 //the result is an integer type and value is 82
 Floating-point expression – if all operands in an expression are floating-point numbers, and the
expression returns a floating-point type value, the expression is a floating-point expression. For example:
y = 2.8 * 17.5 – 1.40 //the result is a floating-point and the value is 47.6
Mixed Expressions
Mixed expression – an expression that has operands of different data types. These contain both integers and
floating-point numbers and returns a floating-point number. For example, 11.5 + 3 * 2.25 and 6 / 4 + 3.9 have an
integer type operand and a floating-point operand.
There are two (2) rules to apply when evaluating mixed expressions:
Rule 1. If the operator has the same types of operands, the operator is evaluated according to the type of the
operand.
For example:
25 / 2 * 2.0 //evaluation: 12 * 2.0 = 24.0
25.0 / 2.0 * 2 //evaluation: 12.5 * 2 = 25.0
Rule 2. If the operator has both types of operands, during calculation, the integer is treated temporarily as a
floating- point number, and the final result is a floating-point number. For example:
25.0 / 2 * 2 //evaluation: 12.5 * 2 = 25.0
Logical Expressions
Logical expression – an expression that returns a Boolean value when evaluated. These expressions can
consist of logical operators and relational expressions. Logical operators have low precedence and are evaluated
after other operations have been evaluated. For example, the expression 14 >= 5 && 12 != 3 evaluates to true.
If two (2) or more logical operators appear in an expression, the expression is evaluated from left to right. For
example, the expression 3 > 3 || 4 < 2 && 12 > 3 || 4 < 5 evaluates to true.
Parentheses are used to group expressions and to control the operators in the expression. For example, (3 > 3 ||
4 < 2) && (12 > 3 || 4 < 5) evaluates to false.

REFERENCES:
Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning java programming: The object-oriented approach.
Indiana: John Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th edition. Boston: Course Technology, Cengage Learning.
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th edition. California: Pearson
Education, Inc.

You might also like