Java record -77-enhanced for loop and auto-unboxing

Source: Internet
Author: User

Introduction to new features in JDK5.0:

generic type (generics);

Enhanced "for" loops (enhanced for loop);

Automatic packing/automatic unpacking (autoboxing/unboxing);

Type safety enumeration (type safe enums);

Static import (statically import);

Variable parameters (Var args);

Enhanced for Loop:

The addition of the For-each loop simplifies the traversal of the collection

Its syntax is as follows:

for (type Element:array) {

SYSTEM.OUT.PRINTLN (Element);

}

When iterating through a collection or array, if you need to access the index of a collection or array, you end up looping or iterating using the old-fashioned way, rather than using an enhanced for loop because it loses subscript information.


Automatic boxing/unpacking greatly facilitates the role of basic type data and their wrapper classes.

Automatic Boxing: The base type is automatically converted to the wrapper class. (int >> Integer)
Automatic unpacking: The wrapper class is automatically converted to the basic type. (Integer >> int)


Java record -77-enhanced for loop and auto-unboxing

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.