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

Source: Internet
Author: User
Tags integer numeric reference wrappers

Learn how Java next-generation languages reduce boilerplate code and reduce complexity

The limitations faced by the Java programming language at birth are different from those faced by today's developers. Specifically, there are primitive types in the Java language due to the performance and memory limitations of hardware in the middle of the 90 's. Since then, the Java language has evolved, with automatic boxing (Autobox) eliminates a lot of hassle, and the next language (Groovy, Scala, and Clojure) goes a step further, eliminating inconsistencies and conflicts in each language.

In this installment, I'll show you how the next language eliminates some common Java restrictions, both syntactically and by default. The first limitation is the existence of primitive data types.

The demise of the primitive language

The Java language started with 8 pairs of primitives and corresponding type wrapper classes (initially used to address performance and memory limitations) and gradually diluted the differences between them by automatic boxing. The next generation of Java language goes further, making developers feel as if there is no difference at all.

Groovy completely hides the primitive type. For example, an int always indicates that Integer,groovy automatically handles upper transformations of numeric types to prevent numeric overflow errors. For example, check out the Groovy shell interaction in Listing 1:

Listing 1. Groovy's automatic processing of primitives

Groovy:000> 1.class
===> class Java.lang.Integer
groovy:000> 1e12.class
===> class Java.math.BigDecimal

In Listing 1, the Groovy shell shows that even constants are represented by the underlying class. Because all numbers (and other disguised primitives) are real classes, you can use metaprogramming techniques. These techniques include adding a method to a number (which is typically used to build a domain-specific language, or DSL), and supports expressions such as 3.cm. In the article on extensibility later, I'll describe this feature in more detail.

As in Groovy, Clojure automatically masks the differences between primitives and wrappers, allowing method calls to be performed on all types, and automatic handling of type conversions of capacity. Clojure encapsulates a large number of low-level optimizations, which are described in detail in the language documentation. In many cases, type hints can be provided so that the compiler can generate faster code. For example, instead of using the (Defn sum[x] ...) definition method, you can add a type hint, such as (defn sum [^float x] ...), which generates more efficient code for the critical section (critical sections).

Scala also masks the differences between primitives, usually using the underlying primitives for the time-sensitive parts of the code. It also allows methods to be called on constants, just like in 2.toString. The ability to mix primitives and wrappers, such as Integer,scala, is more transparent than Java automatic boxing. For example, the = = operator in Scala can run correctly on primitives and object references (comparing values, not references), unlike the Java version of the same operator. Scala also includes an EQ method (and a symmetric NE method) that always compares the underlying reference type to be equivalent. Basically, Scala switches the default behavior intelligently. In the Java language, = = The reference data is compared, you hardly need to do this, you can use less intuitive equals () to compare values. In Scala, = = can run correctly (compare values), regardless of what the underlying implementation is, it also provides a way to perform a less common reference equality check (reference equality check).

This feature of Scala shows that one of the key advantages of the Java Next generation language is that developers can have more time to think about more advanced issues when unloading low-level details to language and running.

Simplifying default behavior

There is a high level of consensus, and most Java developers believe that common operations in the Java language require too much syntax. For example, attribute definitions and other boilerplate code make class definitions messy and obscure important methods. All Java next-generation languages provide a way to simplify the process of creating and accessing.

Classes and Case classes in Scala

Scala has simplified class definitions and can automatically create access functions, assignment functions, and constructors for you. For example, check out the Java class in Listing 2:

Listing 2. Simple Person class in Java

Class Person 

{
    private String name;
    private int age;
    
    Person (String name, int age) {
        this.name = name;
        This.age = age;
    }
    
    Public String GetName () {return
        name;
    }
    
    public int getage () {return age
        ;
    }
    
    public void Setage (int age) {
        this.age = age;
    }
    
    @Override public
    String toString () {return
        name + ' is ' + age + ' years old. '
    }
}

The only non-boilerplate code in Listing 2 is the overridden ToString () method. Constructors and all methods are generated by the IDE. It's more important to easily understand it later than to quickly generate code. The useless syntax increases the amount of code you must use before you understand the underlying meaning.

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.