In the previous installment, I introduced the innovative way in which the next generation of Java language was used to eliminate the flashy things and complexities in the Java language. In this installment, I'll show how these languages eliminate some of the flaws in Java: exceptions, statements and expressions, and the edge of the http://www.aliyun.com/zixun/aggregation/19527.html ">null."
Expression
One inheritance of the Java language from the C language is to differentiate between programming languages and programming expressions. Examples of Java statements include lines of code that use if or while, and lines of code that use void to declare methods that do not return any values. An expression, such as 1 + 2, is used to fetch a value.
This distinction has already begun in the earliest programming languages, such as in Fortran, which is based on hardware conditions and a preliminary understanding of programming language design. In many languages, it is kept as an indicator of the action (statement) and the evaluation (expression). But language designers have come to realize that the language can be composed entirely of expressions, ignoring results when not interested in the results. In fact, all functional languages can completely eliminate this distinction, using only expressions.
Groovy if and?:
In the Java next-generation language, the separation between traditional imperative language (Groovy) and functional languages (Clojure and Scala) shows evolution to expressions. Groovy still contains statements that are based on Java syntax, but add more expressions. Scala and Clojure use an expression entirely.
The words contained in statements and expressions add grammatical awkwardness to the language. You can consider the IF statement in Groovy, which inherits from Java. It has two versions, which are compared in Listing 1, and these two are the IF statements for executing judgments, and the mysterious ternary operator?::
Listing 1. Groovy's two IF statements
def x = 5def y = 0if (x% 2 = 0) y = x * 2else y = x-1println y//4y = x% 2 = 0? (x *= 2): (x = 1) println y//4
In Listing 1 of the IF statement, I must set the value of X to a side effect (side multiplying) because the IF statement does not return any values. To perform a judgment and assign the value at the same time, you must use a ternary assignment, as shown in the second assignment statement in Listing 1.
Scala's expression-based if statement
Scala eliminates the need for ternary operators and allows if expressions to be processed in both cases. You can use it, just as you would use an if statement in Java code (ignoring the return value), or use it in an assignment statement, as shown in Listing 2:
Listing 2. Scala's expression-based if statement
Val x = 5val y = if (x 2 = 0) x * 2 else x-1println (y)
Scala, like the other two Java next languages, does not require a clear return statement to be included in the method. Therefore, the last line of the method is the return value, emphasizing the expression-based attributes of the methods in these languages.
When you manipulate and set values in Java and Groovy code, you can encapsulate each response as a block of code, as shown in Listing 3, and include any required side effects:
Listing 3. Scala if + side effects
Val z = if (x 2 = 0) {println (' divisible by 2 ') x * 2} else {println ("not divisible by 2; Odd ") X-1}println (z)
In Listing 3, I also print a status message for each case, in addition to returning the newly computed value. The order of the lines of code in the code block is important: The last line of code blocks represents the return value that meets the criteria. Therefore, you must be very careful when you use an if based on an expression for mixed evaluation and side effects.