Syntax sugar in Java

Source: Internet
Author: User

The definition of grammar sugar by Baidu Encyclopedia

Grammatical Sugars ( Syntactic sugar), also translated as sugar-coated grammar, was made by British computer scientist Peter • John · Lanta ( Peter J. Landin The invention of a term, refers to a computer language added to a grammar, which has no effect on the function of the language, but more convenient for programmers to use. In general, the use of syntactic sugars can increase the readability of the program, thus reducing the chance of program code errors.

syntax sugar in Java is also a new syntax that makes it easier for programmers to use. But the bottom of the computer is still implemented using basic syntax. Here we discuss several commonly used syntactic sugars, variable parameters , auto- boxing / unpacking , and enhanced for loops .

variable Parameters

Without mutable parameters, we need to overload the method with the same functionality. There will be a lot of duplicate code that violates the dry (Don ' t repeat yourself) principle. But with mutable parameters we can pass in a different number of parameters and call the same method.

Source:

 Public class Varialbeargumentsdemo {    publicstaticvoid doWork (int... a) {//  variable parameter        System.out.println ("Same function! ");    }      Public Static void Main (string[] args) {        doWork (1);        DoWork (a); // different number of parameters, called the same method     }}
Output Result:
Same Function!
Same Function!

The code to decompile the bytecode of the file:

 Public classvarialbeargumentsdemo{ Public Static voidDoWork (int... a) {System.out.println ("????????????"); }     Public Static voidMain (string[] args) {doWork (New int[] {1 }); DoWork (New int[] {1, 2, 3});//The bottom of the computer is implemented by arrays, and variable parameters are passed  }}
we find that the essence of a mutable parameter is an array pass. automatic packing \ unpacking

There are 8 basic data types in Java, which are not objects. So there is no corresponding method, cannot distinguish between '0' and 'NULL', it is inconvenient to operate, so Java encapsulates them into object byte, Short, Integer, Long, Character, Float, Double, Boolean, and give the corresponding method. We assign a basic data type to the encapsulated class object, which requires a type conversion, and assigning a wrapper class object to a basic data type requires parsing. We refer to these two processes separately, boxing and unpacking.

because these two operations are often used, the loading / disassembling operation is cumbersome and not difficult, so Java gives the automatic loading / unpacking operation, We use the integer discussion, the other 7 kinds.

Source:

 Public class Test {    publicstaticvoid  main (string[] args) {        = 123;   assigns a basic data type to an integer object        int num2 = NUM1;   assigns an integer object to the Shaping variable     }}

The code to decompile the bytecode of the file:

 Public class test{  publicstaticvoid  main (string[] args) {    = Integer.valueof (123); // Boxing Operations    int num2 = Num1.intvalue (); // Unboxing Operation   }}
the bottom of the computer is still manual packing and unpacking. Enhanced for Loop

Enhanced for Loop (foreach) makes it easier to make arrays and iterator instances easier. Syntax format: for (data type type name: array name/iterator instance name) {}

We traverse an array of integers with an enhanced for loop.

Source:

 Public class Test {    publicstaticvoid  main (string[] args) {        int New int []{1,2,3,4,5};          for (int ele:arr) {// traversal of arr output             System.out.print (ele);     }}}

The code to decompile the bytecode of the file:

 Public classtest{ Public Static voidMain (string[] args) {int[] arr = {1, 2, 3, 4, 5 }; int[] arrayOfInt1; intj = (ArrayOfInt1 = arr). length;//copies a copy of the original array to prevent damage to the original data for(inti = 0; I < J; i++) {      intEle =Arrayofint1[i];    System.out.print (ele); }  }}
enhanced for loop for loops that are still in use at the bottom

============================================

Syntactic sugars are designed to simplify the operation of certain fixed formats, making it easier for programmers to program efficiently.

Syntax sugar in Java also has many conditional compilations, inner classes, enumeration classes, generics, and so on.

Syntax sugar in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.