Expressions in Java

Def of Expression: An expression is evaluated to obtain a value . In addition, evaluation of an expression may change the computer's state: the values of variables, fields, and array elements, the contents of files, and so on. More precisely, evaluation of an expression
  • terminates normally, producing a value; or
  • terminates abruptly by throwing an exception; or
  • does not terminate at all (for instance, because it calls a method that does not terminate).
Expressions are built from literals , variables, fields, operators, method calls,array accesses, conditional expressions, the new operator, and so on; see the table of expression forms on the facing page.
One must distinguish the compile-time type of an expression from the run-time class of an object. An expression has a type inferred by the compiler. When this is a reference type t, and the value of the expression is an object o, then the class of object o will be a subtype of t but not
necessarily equal to t. For instance, the expression (Number) (new Integer (2) ) has type
Number, but its value is an object whose class is Integer, a subclass of Number.

Expression Forms:

The table of expression forms shows the form, meaning, associativity, argument (operand) types, and result types for expressions. The expressions are grouped according to precedence, as indicated by the horizontal rules, from high precedence to low precedence. Higher-precedence forms are evaluated before lower-precedence forms. Parentheses may be used to emphasize or force a particular order of evaluation.

When an operator (such as +) is left-associative, a sequence el + e2 + e3 of operators is evaluated as if parenthesized (el + e2) + e3. When an operator (such as =) is right-associative, a sequence el = e2 = e3 of operators is evaluated as if parenthesized el = (e2 = e3).

In the argument type and result type columns of the table, integer stands for any of char, byte, short, int, or long; and numeric stands for integer or float or double.For an operator with one integer or numeric operand, the promotion type is double if the operand has type double; it is float if the operand has type float; it is long if the operand has type long; otherwise it is int (that is, if the operand has type byte, char, short, or int).

For an operator with two integer or numeric operands (except the shift operators), the promotion type is double if any operand has type double; otherwise, it is float if any operand has
type float; otherwise, it's long if any operand has type long; otherwise it' s int.Before the operation is performed, the operands are promoted, that is, converted to the promotion type
by a widening type conversion .If the result type is given as numeric also, it equals the promotion type.

For example, 10 / 3 has type int,
whereas 10 / 3.0 h
as type double,
and c + (byte)1 has type int when c has type char.









Type Cast Expressions and Type Conversion:

A type conversion converts a value from one type to another. A widening conversion converts from a type to a super type. A narrowing conversion converts from a type to another type. This requires an explicit type cast (except in an assignment x = e or initialization where e is a compile-time integer constant.

Type Cast Between Primitive Types:
When e is an expression of primitive type and t is a primitive type, then a type cast of e to t is done using the expression (t)e
This expression, when legal, has type t. The legal type casts between primitive types are shown in the following table, where C marks a narrowing conversion that requires a type cast (t)e, W marks a widening conversion that preserves the value, and L marks a widening conversion that may cause a loss of precision.























No comments:

Post a Comment