Summary of new features of java5 static reference, foreach, auto-boxing and generic enumeration and variable parameters

tags: New features of java5  Java5 new features static reference foreach self

Static reference

Import means which class to find, which package to find which class is used.

There is a language core package in java: java.lang.

Using the API in the java.lang package, you don’t need to refer to it, you can find it directly. However, if the API we use does not call for the java.lang package or even the java.lang package (java.lang.reflect), at this time, we You have to import the used API into the current java file

grammar:

import the fully qualified name of the referenced class

For example: import package*: Go to which package to find which classes are used. At this time, it does not mean that * means all classes/interfaces in the specified package, but refers to the package used in the current folder. API (commonly speaking, it will point to the required class/interface only when it is used).

If we need to use static members (fields/methods, inner classes) in a certain class, we have a simpler way to reference

grammar:

import static The fully qualified name of the imported class. Static members

Such as: import java.util.Date;

import static The fully qualified name of the imported class.*

Static introduction is syntactic sugar. It is just for the convenience of the program and simple operation. It is not recommended to use static references in development, because you don't know which class the static members come from.

Practice code:

package com.test;
import static java.lang.Math.PI;
import static java.util.Arrays.*;

import java.util.ArrayList;
import java.util.List;
public class StaticDemo {
	public static void main(String[] args) {
		 /*No more Math.PI*/
		System.out.println(PI);
		List<Integer> a = new ArrayList<>();
		 /*No need to write Arrays.asList at this time*/
		a=asList(1,4,2,3);
		 /*Traditional writing in order from big to small*/
/*		a.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return Integer.compare(o2, o1);
			}
		});*/
		 /*Lambda expression writing*/
		a.sort((o1,o2)->Integer.compare(o2, o1));
		System.out.println(a);
	}
}
Originally wanted to see the decompiled content, but unfortunately jad has a problem, so I cut off the pictures in the video for reference!



It can be seen from the figure that it is still traditionally written after compilation.

Method variable parameters

Reference blog:Click to open the link

String tool class StringUtil

Reference blog:Click to open the link

foreach operation

1. Foreach loop statement format

for (data type variable name: object)
{
//The loop body is a Java statement that references a variable
}
note:
a. The variable name can be arbitrary but the data type must be the same as the data type of the traversal object.
b. The object is a container, such as an array or a collection, etc.
c. It is not possible to change the value in the object in the loop, that is, any change in the value of the object in the loop body is invalid, and the value in the object will not be changed. This is also an important difference between foreach loop and for loop. Foreach implements the traversal code of the object more concisely, and the for loop can change the value in the object.

2. Code implementation

		Set<String> set = new HashSet<String>();
		set.add("1");
		set.add("2");
		set.add("3");
		set.add("4");
		set.add("5");
		set.add(String.valueOf((int)Math.random()*10));
		for (String string : set) {
			System.out.println(string);
		}


Foreach and Iterator selection

  1. The for loop is generally used to deal with relatively simple ordered collections or arrays of predictable size
  2. Foreach can be used to traverse any collection or array, and the operation is simple and easy to understand. Its only downside is that it needs to understand the internal type of the collection
  3. Iterator is the most powerful. It can modify or delete the elements in the collection at any time, and it is performed without knowing the type of the elements and the collection (for the reason, please refer to the third point: polymorphic difference). When you need to implement the same traversal method for different containers, iterators are the best choice
Code exercise:
package com.test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

public class MainDemo {
	public static void main(String[] args) {
		Set<String> set = new HashSet<String>();
		set.add("1");
		set.add("2");
		set.add("3");
		set.add("4");
		set.add("5");
		set.add(String.valueOf((int)Math.random()*10));
		 System.out.println("------------Previous state--------------");
		for (String string : set) {
			System.out.println(string);
		}
		for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
			String next = (String)iterator.next();
			Integer of = Integer.valueOf(next);
			if(of>3){
				 iterator.remove();//Delete operation
			}
		}
		 System.out.println("-------------Deleted state--------------");
		for (String string : set) {
			System.out.println(string);
		}
	}
}


Automatic boxing and unboxing design and cache design

Recommended blog:Click to open the link

Generic

My article:Click to open the link

enumerate

Recommended blog:Click to open the link
Code exercise:
package com.test;
enum Week{
	Mon,Stu,THU,FOu,FIR,SAT,SUN;
}
public class xx {
	Week we;
	void setWeek(Week we){
		this.we=we;
	}
	Week getWeek(){
		return we;
	}
	public static void main(String[] args) {
		xx x = new xx();
		x.setWeek(Week.Mon);
		if(x.getWeek().equals(Week.Mon)){
			System.out.println("Monday");
		}
		 //Switch supports enumeration types
		switch(x.getWeek()){
		 case Mon:System.out.println("Monday");break;
		 case Stu:System.out.println("Tuesday");break;
		 case THU:System.out.println("Wednesday");break;
		default :break;
		
		}
		 //Get enumeration object
		Week wk = Week.valueOf("Mon");
		System.out.println(wk);
		String str ="hello";
		switch(str){
		
		}
	}
}















Intelligent Recommendation

New Java5 features (automatic boxing and unboxing)

Autoboxing, such as: Automatic unboxing, such as: For the full text, please visit: [url]http://www.juziku.com/wiki/220.htm[/url]...

[Java5 new features] automatic boxing / unboxing

Auto Packing / Unboxing Overview Java has basic types (int, double, float, long, boolean, char, byte, short) and basic type wrapper classes (Integer, Double, Float, Long, Boolean, Char, Byte, Short), ...

New Java5 features----static import

When I read the book today, I saw the concept of "static import". I found out that it is a new feature of JDK5.0 when I checked it on the Internet. It is really ignorant, so I have studied i...

New Java5 features (static import)

Static import: You can statically import the specified method or all methods, such as: For the full text, please visit: [url]http://www.juziku.com/wiki/198.htm[/url]...

Jdk new features (1) static import automatic boxing / unboxing advanced for loop variable parameters

1, static import import static 2, automatic boxing: the basic data type is directly assigned to the corresponding packaging class Automatic unboxing: The wrapper object is directly assigned to the cor...

More Recommendation

[Summary] Summary of new features of Java5

Copyright statement: This article is a small anomaly original article, non-commercial free reprint - keep the signature - indicate the source, thank you! This article URL: Article directory First, aut...

New features in JDK 1.5 (variable parameters and foreach)

JDK1.5 new features Variable parameter 2. foreach (enhanced for loop) Variable parameter If there is such a requirement, the result of adding the integers of any parameter is calculated. This requirem...

Java new features-foreach, variable parameters

foreach 1. ``The efficiency of for loop is higher than foreach when fixed length or length does not need to be calculated. 2. ``When the length is uncertain or the calculated length has performance lo...

No3.foreach output / JAVA5 new features

Knowledge points: Enhanced for loop use. specific contents:      The foreach output is the first concept introduced by c#, the purpose of which is to perform array or output of aggregat...

Java advanced features (generic, lambda, enumeration, method variable parameters, static import)

Generic What is a generic: parameterize the type, that is, the data type is existing in the form of parameters, so that the data type can vary. The generic is a new feature of JDK1.5. Do not use gener...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top