tags: New features of java5 Java5 new features static reference foreach self
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.
1. Foreach loop statement format
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);
}
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);
}
}
}
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){
}
}
}
Autoboxing, such as: Automatic unboxing, such as: For the full text, please visit: [url]http://www.juziku.com/wiki/220.htm[/url]...
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), ...
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...
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]...
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...
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...
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...
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...
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...
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...