What are “sugar”, “desugar” terms in context of Java 8?

前端 未结 4 876
予麋鹿
予麋鹿 2021-01-30 10:03

I hear about \'sugaring\' and \'desugaring\' more often in Java 8, what does these terms mean ? are they conceptual or syntactical.

Some Example:

Defa

4条回答
  •  孤城傲影
    2021-01-30 10:38

    In general "desugaring" in javac allows representing some language features with preexisting ones. This allows representing them in the bytecode without making big changes to the class file format. Also for this reason the back-end of the compiler is more stable than the front-end. This doesn't mean that every new language feature is just syntactic sugar, as is definitely not the case of lambdas and method references. There are more examples of "desugaring" in the compiler:

    • for each loops are "desugared" to C style for loops
    • assertions are "desugared" to an if sentence
    • inner classes are represented as a standalone class

    You can investigate also what happen with the String switch, type erasure,...

提交回复
热议问题