Синтаксический сахар java примеры

Java: Синтаксический сахар

Подобные конструкции index = index + 1 в Java используются довольно часто, поэтому создатели языка добавили сокращенный вариант записи: index += 1 . Такие сокращения принято называть синтаксическим сахаром, потому что они делают процесс написания кода немного проще и приятнее.

Существуют сокращенные формы для всех арифметических операций и для конкатенации строк:

  • a = a + 1 → a += 1
  • a = a — 1 → a -= 1
  • a = a * 2 → a *= 2
  • a = a / 1 → a /= 1
  • a = a + «foo» → a += «foo»

Задание

Реализуйте статический метод App.filterString() , принимающую на вход строку и символ, и возвращающую новую строку, в которой удален переданный символ во всех его позициях.

var str = "If I look back I am lost"; App.filterString(str, 'I'); // "f look back am lost" App.filterString(str, 'o'); // "If I lk back I am lst" 

Если вы зашли в тупик, то самое время задать вопрос в «Обсуждениях». Как правильно задать вопрос:

  • Обязательно приложите вывод тестов, без него практически невозможно понять что не так, даже если вы покажете свой код. Программисты плохо исполняют код в голове, но по полученной ошибке почти всегда понятно, куда смотреть.

Тесты устроены таким образом, что они проверяют решение разными способами и на разных данных. Часто решение работает с одними входными данными, но не работает с другими. Чтобы разобраться с этим моментом, изучите вкладку «Тесты» и внимательно посмотрите на вывод ошибок, в котором есть подсказки.

Это нормально 🙆, в программировании одну задачу можно выполнить множеством способов. Если ваш код прошел проверку, то он соответствует условиям задачи.

В редких случаях бывает, что решение подогнано под тесты, но это видно сразу.

Читайте также:  Закладка

Создавать обучающие материалы, понятные для всех без исключения, довольно сложно. Мы очень стараемся, но всегда есть что улучшать. Если вы встретили материал, который вам непонятен, опишите проблему в «Обсуждениях». Идеально, если вы сформулируете непонятные моменты в виде вопросов. Обычно нам нужно несколько дней для внесения правок.

Кстати, вы тоже можете участвовать в улучшении курсов: внизу есть ссылка на исходный код уроков, который можно править прямо из браузера.

Источник

Syntactic Sugars in JAVA

In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language «sweeter» for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

A construct in a language is «syntactic sugar» if it can be removed from the language without any effect on what the language can do: «functionality» and «expressive» power will remain the same.

Following are some of the syntactic sugars used in JAVA programming languages,

  • Compound Assignment Operators are syntactic sugars of Assignment Operators ( x += 1 and x -= 1 instead of x = x + 1 and x = x — 1)
  • Increment/Decrement Operators are syntactic sugars of Compound Assignment Operators ( x++ and x— instead of x += 1 and x -= 1)
  • Ternary Operator is a syntactic sugar of If-then-else statements (condition ? statisfied : otherwise)
  • Enhanced For Loop is a syntactic sugar of For Loop with an Iterator

The above for-each loop is a syntactic sugar for the following for loop with iterator

Note:

Language processors, including compilers and static analyzers, often expand sugared constructs into more fundamental constructs before processing, a process sometimes called «de-sugaring».

For example, enhanced for loop will be de-sugared to a for loop with iterator at compile time.

Syntactic Sugars and Runtime Exceptions

It is important to understand the sugaring and de-sugaring as sugared constructs throw certain runtime exceptions based on their de-sugared constructs.

Enhanced For loop throws a java.util.ConcurrentModificationException when removing an element from a Collection using the Collection remove( ) method while iterating over the Collection.

While the generic for loop with counter doesn’t throw a java.util.ConcurrentModificationException when removing an element from a Collection (using the Collection remove( ) method) while iterating over the Collection.

This is because at the compile time, enhanced for loop is de-sugared into a generic for loop with fail-fast iterator. Fail-fast iterator throws a java.util.ConcurrentModificationException when removing an element from a Collection (using the Collection remove( ) method) while iterating over the Collection.

Источник

Syntactic Sugar

In a programming language syntactic sugar is a convenient syntax/construct which makes things easier for the programmer.

Java language provides many syntactic constructs. For example consider following Java enum class:

Let’s compile above class and analyze compiled bytecode via javap :

D:\java-syntactic-sugar-examples>javac EnumTest.java D:\java-syntactic-sugar-examples>javap EnumTest Compiled from "EnumTest.java" public final class EnumTest extends java.lang.Enum < public static final EnumTest ElementA; public static final EnumTest ElementB; public static final EnumTest ElementC; public static EnumTest[] values(); public static EnumTest valueOf(java.lang.String); static <>; >

The compiler has converted the enum class to a normal Java class extending java.lang.Enum . Each enum element is converted to static final constant of the enclosing class type.

Foreach loop is another example:

public class ForEachLoop < public static void main(String[] args) < Listlist = List.of(1, 3, 6, 8, 9); for (int i : list) < System.out.println(i); >> >

If we compile above class and analyze the compiled code via javap :

D:\java-syntactic-sugar-examples>javac ForEachLoop.java D:\java-syntactic-sugar-examples>javap -v ForEachLoop Classfile /D:/java-syntactic-sugar-examples/src/main/java/ForEachLoop.class Last modified Oct 2, 2018; size 876 bytes MD5 checksum 3c591f0e683054877602faac8cc45075 Compiled from "ForEachLoop.java" public class ForEachLoop minor version: 0 major version: 55 flags: (0x0021) ACC_PUBLIC, ACC_SUPER this_class: #11 // ForEachLoop super_class: #12 // java/lang/Object interfaces: 0, fields: 0, methods: 2, attributes: 1 Constant pool: #1 = Methodref #12.#24 // java/lang/Object."":()V #2 = Methodref #7.#25 // java/lang/Integer.valueOf:(I)Ljava/lang/Integer; #3 = InterfaceMethodref #20.#26 // java/util/List.of:(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/List; #4 = InterfaceMethodref #20.#27 // java/util/List.iterator:()Ljava/util/Iterator; #5 = InterfaceMethodref #21.#28 // java/util/Iterator.hasNext:()Z #6 = InterfaceMethodref #21.#29 // java/util/Iterator.next:()Ljava/lang/Object; #7 = Class #30 // java/lang/Integer #8 = Methodref #7.#31 // java/lang/Integer.intValue:()I #9 = Fieldref #32.#33 // java/lang/System.out:Ljava/io/PrintStream; #10 = Methodref #34.#35 // java/io/PrintStream.println:(I)V #11 = Class #36 // ForEachLoop #12 = Class #37 // java/lang/Object #13 = Utf8 #14 = Utf8 ()V #15 = Utf8 Code #16 = Utf8 LineNumberTable #17 = Utf8 main #18 = Utf8 ([Ljava/lang/String;)V #19 = Utf8 StackMapTable #20 = Class #38 // java/util/List #21 = Class #39 // java/util/Iterator #22 = Utf8 SourceFile #23 = Utf8 ForEachLoop.java #24 = NameAndType #13:#14 // "":()V #25 = NameAndType #40:#41 // valueOf:(I)Ljava/lang/Integer; #26 = NameAndType #42:#43 // of:(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/List; #27 = NameAndType #44:#45 // iterator:()Ljava/util/Iterator; #28 = NameAndType #46:#47 // hasNext:()Z #29 = NameAndType #48:#49 // next:()Ljava/lang/Object; #30 = Utf8 java/lang/Integer #31 = NameAndType #50:#51 // intValue:()I #32 = Class #52 // java/lang/System #33 = NameAndType #53:#54 // out:Ljava/io/PrintStream; #34 = Class #55 // java/io/PrintStream #35 = NameAndType #56:#57 // println:(I)V #36 = Utf8 ForEachLoop #37 = Utf8 java/lang/Object #38 = Utf8 java/util/List #39 = Utf8 java/util/Iterator #40 = Utf8 valueOf #41 = Utf8 (I)Ljava/lang/Integer; #42 = Utf8 of #43 = Utf8 (Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/List; #44 = Utf8 iterator #45 = Utf8 ()Ljava/util/Iterator; #46 = Utf8 hasNext #47 = Utf8 ()Z #48 = Utf8 next #49 = Utf8 ()Ljava/lang/Object; #50 = Utf8 intValue #51 = Utf8 ()I #52 = Utf8 java/lang/System #53 = Utf8 out #54 = Utf8 Ljava/io/PrintStream; #55 = Utf8 java/io/PrintStream #56 = Utf8 println #57 = Utf8 (I)V < public ForEachLoop(); descriptor: ()V flags: (0x0001) ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."":()V 4: return LineNumberTable: line 3: 0 public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: (0x0009) ACC_PUBLIC, ACC_STATIC Code: stack=5, locals=4, args_size=1 0: iconst_1 1: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; . >

The above analysis shows that foreach loop has been replaced by java.util.Iterator . The equivalent code would be:

List list = List.of(1, 3, 6, 8, 9); Iterator itr = list.iterator(); while(itr.hasNext())

Syntactic sugar is a mean which allows Java to provide new syntax without implementing that on JVM level. The syntactic syntax is converted into another more general or low level construct by the compiler which can be understood by JVM. That way syntactic constructs enable Java compilers to implement new Java language features without changes to the JVM.

Источник

Оцените статью