The next generation of Java: What Groovy, Scala, and Clojure have in common (i)

Source: Internet
Author: User
Tags class definition constructor require

Explore how these next-generation JVM languages handle operator overloading

Good ideas in programming languages can be extended and expanded into other languages, just like wine. Therefore, it is not surprising that the Java next-generation languages-groovy, Scala and clojure-have many common features. In this and next installment of the Java Next generation article, I'll explore the consistency of the feature list in each language syntax. I start with the ability to overload the operator-overcoming a long-standing shortcoming in the Java language.

Operator overloading

If you have modified the Java BigDecimal class, you may have seen code similar to listing 1:

Listing 1. Lackluster BigDecimal support in Java code

BigDecimal OP1 = new BigDecimal (1e12);
BigDecimal OP2 = new BigDecimal (2.2e9);
(OP1 + (OP2 * 2))/(op1/(OP1 + (OP2 * 1.5e2))
BigDecimal LHS = Op1.add (Op2.multiply (bigdecimal.valueof (2)));
BigDecimal RHS = op1.centeride (
        op1.add (op2.multiply (1.5e2)),
            bigdecimal.valueof);
BigDecimal result = Lhs.centeride (RHS);
System.out.println (String.Format ("%,.2f", result));

In Listing 1, I tried to implement this formula in the annotation. In Java programming, I can only turn to method calls because of the inability to overload mathematical operators. Static imports can solve the problem, but for the context you choose, it is clear that you need the appropriate operator overload. The original Java engineer deliberately ignored the operator overloads from the language, but this feeling added too much complexity. However, experience has shown that the complexity imposed on developers for lack of this feature is greater than the potential for abuse.

In a slightly different way, all three Java next-generation languages implement operator overloading.

Scala's operator

Scala allows operator overloading by discarding the difference between the operator and the method. An operator is simply a method with a special name. For example, to rewrite the multiplication operator, you can override the * method. [* is a valid method name, which is one of the reasons Scala uses the underscore (_) symbol instead of the Java asterisk (*) symbol to represent the import. ]

I use complex numbers to illustrate overloads. A complex number is a mathematical representation, including a real part and a imaginary part, such as a form usually written in 3 + 4i. Complex numbers are common in many fields of science, including engineering, physics, Electromagnetics, and other theories. Listing 2 shows the Scala implementation of the plural:

Listing 2. Scala plural

 Final Class Complex (Val real:int, Val imaginary:int) {require (real!= 0 | | imaginary!= 0) def + (operand : Complex) = new Complex (real + operand.real, imaginary + operand.imaginary) def + (operand:int) = new Com Plex (real + operand, imaginary) def-(Operand:complex) = new Complex (Real-operand.real, Imaginary-operand.
      Imaginary Def-(operand:int) = new Complex (Real-operand, imaginary) def * (Operand:complex) = New Complex (Real * operand.real-imaginary * operand.imaginary, Real * operand.imaginary + imaginary * operand. Real) override Def toString () = Real + (if (imaginary < 0) "" Else "+") + imaginary + "I" Overrid E def equals (that:any) = "Match {case Other:complex =>" (real = = other.real) && (imaginary = = other.im Aginary) Case _ => false} override Def hashcode (): Int = ((+ real) + imaginary)} 

Scala dramatically reduces the level of verbose Java language by folding unnecessary scaffolding code. For example, in Listing 2, the constructor parameters and fields in the class appear with the class definition. In this case, the body of the class acts as a constructor, so calls to the Require () method validate the existence of the value during the first instantiation operation. Because Scala automatically provides fields, the rest of the class contains method definitions. For the +,-and * operators, I declare a method with the same name that accepts the number of Complex as a parameter. Multiplication of complex numbers is not as intuitive as addition and subtraction. The overloaded * method in Listing 2 implements the formula:

(x + yi) (U + vi) = (XU-YV) + (xv + Yu) i

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.