Syntax sugar in Java

Source: Internet
Author: User

definition: refers to the addition of a grammar in the language of a computer, which makes it easier for programmers to develop programs using language, while enhancing the readability of program code and avoiding the chance of error, but this syntax has no effect on the functionality of the language.
Generics in Java , variable-length parameters, automatic unpacking/Boxing, conditional compilation , etc. are

generics:Ava generics only exist in source code, only for editor check, compiled bytecode file erased generic type

 Public Static void Main (string[] args) {      List<String>newarraylist<string >();      Stringlist.add ("Oliver");      System.out.println (Stringlist.get (0));  }  

To decompile the bytecode of the above code:

 Public Static void Main (String args[])  {      new  ArrayList ();      Stringlist.add ("Oliver");      System.out.println (String) stringlist.get (0));  }  

Automatic Unpacking/Packing

Packing Process: The basic types are packaged with their corresponding package types, so that the basic types have object characteristics .
Unpacking process: In contrast to the boxing process, the package type is converted to a basic type.
    • The difference between a basic data type and an object

The base data type is not an object, which is a variable, constant, defined using an int, double, boolean, and so on.

There is no callable method for the base data type.

Eg:int t = 1; t. There is no way to drip behind.

Integer t =1; t. There are a number of ways that you can invoke it later.

    • When to automatically pack

For example: Integer i = 100;

The equivalent compiler automatically compiles the following syntax for you: Integer i = integer.valueof (100);

    • When to automatically remove the box

Auto-unpacking (unboxing), that is, the basic data in the object is automatically removed from the object. Automatic unpacking can be implemented as follows:

1 Integer i = 10; Packing
2 int T=I Unpacking, actually executing an int t = I.intvalue ();

It is also possible to disassemble the container while the operation is in progress.

1 Integer i = 10;
2 System.out.println (i++); Note: the "= =" operation of the package type does not automatically remove the box without encountering the arithmetic operator
    • Automatic boxing of integers

Description

Equals () compares the values ( contents ) of two objects. (The method here is already rewritten .)

"= =" compares the two-object reference (memory address) with the same, and is used to compare the values of variables of two base data types for equality. The comparison is not the same object

As mentioned earlier, the automatic boxing of int, is the system executes the integer.valueof (int i), first look at the source code of Integer.java:

123456 publicstaticInteger valueOf(inti) {    if(i >= -128&& i <= IntegerCache.high)  // 没有设置的话,IngegerCache.high 默认是127        returnIntegerCache.cache[i + 128];    else        return new Integer(i);}

  

For values between –128 to 127 (by default, 127), integer.valueof (int i) returns a cached Integer object (not a new object)

So in the example, i3 and I4 actually point to the same object.

In other values, the execution integer.valueof (int i) returns a new integer object, so in the example, I1 and I2 point to a different object.

The use of the automatic disassembly box and other objects is the same

1 integer i3 =new integer (100);
2 integer i4 =new integer (100);
3 System.out.println ("I3==i4:" + (I3==I4));//Display Falsecyclic calendar (foreach)


public static void Main (string[] args) {
list<integer> list = new arraylist<integer> ();
for (Integer num:list) {
SYSTEM.OUT.PRINTLN (num);
}
}

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.