Definition of desugar : to remove sugar from.
Through a process called API desugaring, the DEX compiler (D8) allows you to include more standard language APIs in apps that support older versions of Android.
sugar, in programming, usually refers to those sweet additions, mostly shortcuts, that make some constructs easier to type and to read (the latter being, in practice, the most important during the life cycle of your program).
Wikipedia has a definition of syntactic sugar but you should note that not all sugar is, in essence, syntactical (not all recent sweet additions were just compiler changes).
Here are a few examples :
i++
and ++i
). Their only purpose is to avoid writing an additional statement. They're pure sugar.+=
, |=
, &=
, etc. are made of the same kind of sugar.Java is widely seen as not being concise enough, especially compared to modern languages. That's why those additions that help make the code faster to read are welcome.
To finish, I'd just note that while a lack of sugar can make your program fat, an excess of sugar, leading to many different ways to write the same things, can make your language queasy and your program less coherent and harder to maintain. Another kind of sugar, API sugar, is most often a plague which makes the API harder to grasp, especially when it's made of additions (overloading for example).
This being said, desugaring refers either to
"Desugaring" appears to have a very specific meaning in Java 8. It seems to be a catch-all term to express the various ways a lambda expression may be bound to an actual concrete method call.
This document on "Translation of Lambda Expressions" has the real details of what's going on if you're interested in specifics.
A key phrase from the document:
The first step of translating lambdas into bytecode is desugaring the lambda body into a method.
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:
You can investigate also what happen with the String switch, type erasure,...
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.")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With