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

前端 未结 4 886
予麋鹿
予麋鹿 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:41

    As others have noted, in computer programming and in this context, "sugar" refers to language features that make the code nicer to read/write. "Desugaring" refers to automatically translating "sugar" constructs into other constructs when the compiler or runtime lacks native support for the sugared versions.

    These concepts come up frequently for Java in the context of Android. Android doesn't include a JDK, but instead is a re-implementation of the Java runtime. Therefore support for new Java language features depends on Android supporting the new language features. Currently all Android apps can support all Java 7 features, and a subset of Java 8 features, using desugaring. See "Use Java 8 language features and APIs" for details.

    Here's an article going into the details of desugaring Java features in Android: "Android's Java 8 Support". According to the article, lambdas are actually always desugared in Android binaries ("This is why desguaring always happens at compile-time regardless of your minimum API level.")

提交回复
热议问题