Experience 18--JDK new Features-static import, automatic encapsulation and disassembly, enhanced for loop

Source: Internet
Author: User
Tags wrapper

1. An important theme of "JDK 5.0" is to simplify development by adding a few features, including:

• Static Import

• Automatic packaging/Unpacking

For-each Cycle

• Variable Parameters

• Enumerating

• generic type

• Meta data

static import Syntax is used to import a static property value (method) or all static property values (methods) of a specified class. syntax: import static package name. Class name. static Properties | static method |*; For example: Import the specified class all static properties and methods import staticjava.lang.math.*

Automatic Boxing (autoboxing): assigns a basic data type directly to the corresponding wrapper class variable, or assigns to the Object variable

Auto-unpacking: Assigning a wrapper class object directly to a corresponding base type variable

2. Enhanced for Loop

the reason to introduce an enhanced for loop: In previous versions of JDK5, traversing an array or set of elements, it was cumbersome to get the length of the array or the iterator of the collection . To simplify this type of operation, a new enhanced for loop is added to the JDK5. An enhanced for loop can only be used on an array , or on a collection class that implements the iterator interface

When traversing an array and a collection element using a foreach loop, you do not need to obtain an array and a collection length without accessing the array elements and the collection elements based on the index, and theforeach loop automatically iterates through the array and each element of the collection

L Grammar Format:

• For (type variable name: Collection variable name) {...}

Variable name: Automatic iterative access to each element

Specific case Analysis:

Package com.java.test;

Importjava.util.ArrayList;

Import Java.util.Iterator;

Importjava.util.LinkedHashMap;

Import java.util.List;

Import Java.util.Map;

Import Java.util.Set;

Importjava.util.Map.Entry;

public class Fortest {

public void Test () {

Do not use the generic traversal list:

List List = new ArrayList ();

List.add ("AAA");

List.add ("BBB");

List.add ("CCC");

For (object str:list) {//Because the list is a list object type, it is used to declare variable str with object type

System.out.println (str);

}

To iterate through a list using a generic type:

List<string> List1 = new arraylist<string> ();

List1.add ("AAA");

List1.add ("BBB");

List1.add ("CCC");

for (string str:list1) {///here the generic type of the list is string, so this is the time to iterate over the string declaration

System.out.println (str);

}

}

by Map.keyset () The key value is taken out, traversing

public void Test1 () {

Map map = new Linkedhashmap ();

Map.put ("1", "AAA");

Map.put ("2", "BBB");

Map.put ("3", "CCC");

Set keys = Map.keyset ();

Iterator it = Keys.iterator ();

while (It.hasnext ()) {

String key = (string) it.next ();

String value = (string) map.get (key);

SYSTEM.OUT.PRINTLN (key + "=" + value);

}

}

Through Map.entryset () The key value pair is taken out, a traversal

public void Test2 () {

Map map = new Linkedhashmap ();

Map.put ("1", "AAA");

Map.put ("2", "BBB");

Map.put ("3", "CCC");

Set keys = Map.entryset ();

Iterator it = Keys.iterator ();

while (It.hasnext ()) {

Map.entry me = (Entry) it.next ();

String key = (string) me.getkey ();

String value = (string) me.getvalue ();

SYSTEM.OUT.PRINTLN (key + "=" + value);

}

}

Enhancing the For loop approach

public void Test3 () {

Map map = new Linkedhashmap ();

Map.put ("1", "AAA");

Map.put ("2", "BBB");

Map.put ("3", "CCC");

For (Object Obj:map.entrySet ()) {

Map.entry me = (Entry) obj;

System.out.println (Me.getkey () + "=" +me.getvalue ());

}

}

Using JavaBean traversal

public void Test4 () {

list<person> list = new arraylist<person> ();

List.add (New person ("Jine", 20));

List.add (New person ("Hello", 24);

List.add (New person ("calcium carbide method", 22));

for (person P:list) {

System.out.println (P.getname () + "..." +p.getage ());

}

}

}

Precautions :

An enhanced for loop can only traverse an array or inherit a collection of iterator, cannot assign a value to it, or modify its value

Case One: (Assignment failure case)

intarr[] = new INT[5];

for (Intnum:arr) {

num = 1;

}

System.out.println (Arr[0]); The output is 0, not 1.

Case Two: (Modify value failure case)

List<string>list = new arraylist<string> ();

List.add ("xxx");

for (stringstr:list) {

str = "YYY";

}

System.out.println (list.get (0)); //output result is xxx, not yyy

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.