Tuesday, March 3, 2020
Java Expressions Introduced
Java Expressions Introduced Expressions are essential building blocks of any Java program, usually created to produce a new value, although sometimes an expression assigns a value to a variable. Expressions are built using values, variables, operators and method calls. Difference Between Java Statements and Expressions In terms of the syntax of the Java language, an expression is akin to aà clause in the English languageà which portrays a specific meaning. With the right punctuation, it can sometimes stand on its own, although it can also be a part of a sentence. Some expressions equate to statements by themselves (by adding a semicolon at the end), but more commonly, theyà comprise part of a statement. For example,(a * 2) is an expression. b à (a * 2); is a statement.à You could say that the expression is a clause, and the statement is the complete sentence since it forms the complete unit of execution. A statement doesnt have to include multiple expressions, however. You can turn a simple expression into a statement by adding a semi-colon:à (a * 2);à Types of Expressions While an expression frequently produces a result, it doesnt always. There are three types of expressions in Java: Those that produce a value, i.e., the result of (1 1)Those that assign a variable, for example (v 10)Those that have no result but might have a side effect because an expression can include a wide range of elements such as method invocations or increment operators that modify the state (i.e., memory) of a program.à Examples of Expressions Here are some examples of various types of expressions. Expressions that Produce a Value Expressions that produce a value use a wide range of Java arithmetic, comparison or conditional operators. For example, arithmetic operators includeà , *, /, , , and %. Someà conditional operatorsà are ?, ||, and the comparison operators are , and .à See the Java specification for a complete list. These expressions produce a value: 3/2 5% 3 pi (10 * 2)à Note the parentheses in the last expression. This directs Java first to compute the value of the expression within the parentheses (just like the arithmetic you learned in school), then complete the rest of the computation. Expressions that Assign a Variable This program here contains plenty of expressions (shown in bold italics) that each assigns a value. int secondsInDay 0;int daysInWeek 7;int hoursInDay 24;int minutesInHour 60;int secondsInMinute 60; boolean calculateWeek true;secondsInDay secondsInMinute * minutesInHour * hoursInDay; //7System.out.println(The number of seconds in a day is: secondsInDay);if (calculateWeek true){à à System.out.println(The number of seconds in a week is: secondsInDay * daysInWeek); } The expressions in the first six lines of the code above, all use the assignment operator to assign the value on the right to the variable on the left. The line denoted with //7 is an expression that can stand on its own as a statement. It also shows that expressions can be built up through the use of more than one operator. The final value of the variable secondsInDay is the culmination of evaluating each expression in turn (i.e., secondsInMinute * minutesInHour 3600, followed by 3600 * hoursInDay 86400). Expressions with No Result While some expressions produce no result, they can have a side effect which occurs when an expression changes the value of any of its operands. For example, certain operators are considered to always produce a side effect, such as the assignment, increment and decrement operators. Consider this: int product a * b; The only variable changed in this expression is the product; a and b are not changed. This is called a side effect.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.