Make writing a habit together! This is the 14th day of my participation in the “Gold Digging Day New Plan ยท April More Text Challenge”. Click here for more details.

What is Syntactic Sugar?

Syntax sugar is a piece of computer data, specifically a syntax added to a programming language that has no effect on the language’s functionality but is more convenient for programmers to use. Syntax sugar makes the program more concise and more readable.

The word for sugar is sweet. Syntactic sugar is what makes your code sweeter ๐Ÿถ.

In fact, as mentioned above, it is syntactic sugar if some constructs in a statement can be removed from the statement without affecting the functionality of the language.

Common syntax sugar in Java

The compound assignment operator is the syntactic sugar of the assignment operator

X plus 1 is equal to x plus 1 and x minus 1 is equal to x minus 1Copy the code

The increment/decrement operator is the syntactic sugar of the compound assignment operator

X ++ instead of x+ = 1, x-- instead of x- = 1Copy the code

Ternary operators are the syntactic sugar of if-then-else statements

(conditions? Satisfy: otherwise)Copy the code

The enhanced For loop is the syntactic sugar of the For loop, with an iterator

for (String str:arrayList) {
  
}
Copy the code

The enhanced for loop above is the syntactic sugar of the for loop below with iterators

for (Iterator<String> it = arrayList.iterator(); it.hasNext();) {
  String str = it.next();
  // Implementation logic
}
Copy the code

To highlight

To mash

Language processors, including compilers and static analyzers, often extend the glycosylated structure to more basic structures before processing it, a process sometimes called “deglycosylation.” For example, the enhanced for loop will be cancelled at compile time as a for loop with iterators.

Syntax sugar and runtime exceptions

It is important to understand glycosylation and deglycosylation because glycosylation structures can raise certain runtime exceptions depending on their deglycosylation structures.

Here’s an example:

Everyone knows you can’t add or delete collections in an enhanced for loop, otherwise you throw an exception.

When because of the enhanced For loop iteration the Collection, use the Collection the remove () method is removed from the Collection element is thrown Java. Util. ConcurrentModificationException.

Although in iterative collection when removed from the collection element set (using the remove () method), general with counter for loop will not throw Java. Util. ConcurrentModificationException.

This is because, at compile time, the enhanced for loop is desugared into a generic for loop with a Fail-fast iterator.

Fail – fast iterators in iterative Collection when removed from the Collection element (using the Collection the remove () method) thrown when the Java. Util. ConcurrentModificationException.

More interesting: Syntactic salt

Syntax salt is a design designed to discourage developers from writing bad code. As a simple example, in C and C++ Switch, if a case statement is not broken, the compiler does not generate an error message. Other programmers consider defining variable types to be a form of syntax salt.

For another example, in Java, it is not allowed to assign a float to a variable defined as an int, but in C or C++, a float is automatically assigned to an int by rounding off the decimal.

int num1; Float PI = 3.14159; num1=pi; // Assignment errorCopy the code

Syntax salt can defeat its purpose, making code less readable or wasting code space. In extreme cases, the truly useful code may be shorter than the code added to satisfy the syntax salt requirement.

Another alternative to syntax salt is to have the compiler issue a warning when the code is likely to produce an error — a common practice for C and C++ compilers.