1. 静态导入 import static java.lang.Math.*;
2. 可变参数 如: int add(int x,int … args)
3. for循环增强功能 for(type x: array) { System.out.println(x+” ”)}
4. 自动装箱 Integer I = 10; (保证是同一个对象 -128~127) 自动拆箱 Integer I = 10; int x= I;
package java高新技术;
public class TestInteger {
public static void main(String[] args) {
int x1 = 1;
int x2 = 1;
Integer x3 = 1;
Integer x4 = 1;
Integer x5 = 128;
Integer x6 = 128;
int x7 = Integer.valueOf(128);
int x8 = Integer.valueOf(128);
System.out.println(x1==x2);
//System.out.println(x2==x3);
System.out.println(x3==x4);
System.out.println(x5==x6);
System.out.println(x7==x8);
}
}
5. 枚举类
一个枚举类中的对象就是这个枚举类的子类对象,枚举类中可以有构造方法,但是必须要是private。
package java高新技术;
/**
* 用枚举可以简单实现单例 ,一个枚举中只有一个对象
*/
public class TestEnum {
public enum Week
{
SUN(1),MON(1),TUE(1),WED,THU,FRI,SAR; //都是Week的子类对象
//构造方法
private Week(){System.out.println("this is first constructor");};
private Week(int day){System.out.println("this is second constructor");};
}
public enum TrafficLamp{
RED (20){
@Override
public TrafficLamp nextLamp() {
return GREEN;
}
}, GREEN(30){
@Override
public TrafficLamp nextLamp() {
// TODO Auto-generated method stub
return YELLOW;
}
},YELLOW(40){
@Override
public TrafficLamp nextLamp() {
// TODO Auto-generated method stub
return RED;
}
};
public abstract TrafficLamp nextLamp();
private int time;
private TrafficLamp(int time){
this.time = time;
}
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(Week.FRI);
System.out.println(Week.FRI.ordinal());
System.out.println(Week.valueOf("SUN"));
System.out.println(Week.values().length);
}
}
6. Class
8个基本类型 + void 都属于9 个预定义的Class对象,原始类。
package java高新技术;
public class TestClass {
public static void main(String[] args)throws Exception {
/**
* 三种方法得到Class 字节码,因为是同一个字节码 所以相同
*/
String s = "test";
Class c1 = s.getClass();
Class c2 = String.class;
Class c3 = Class.forName("java.lang.String");
System.out.println(c1 == c2);
System.out.println(c1 == c3);
System.out.println("int.class == Integer.class? "+(int.class == Integer.class));
System.out.println("int.class == Integer.TYPE? "+(int.class == Integer.TYPE));
System.out.println("int[].class is primitive? "+int[].class.isPrimitive());
System.out.println("int[].class is primitive? "+int[].class.isArray());
System.out.println("void.class is primitive?"+ void.class.isPrimitive());
}
}
7. 反射调用一个类中的域值
package java高新技术;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
class B{
private int x;
public B(){};
public B(int i){this.x = i;};
private int getX(){
return this.x;
}
}
public class TestReflect {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String s1 = new String(new StringBuffer("abc"));
Constructor<?> constructor = String.class.getConstructor(StringBuffer.class);
String s2 =(String)constructor.newInstance(new StringBuffer("abc"));
String s3 = new String(new StringBuffer("abc"));
System.out.println(s1.equals(s2)+" "+(s1 == s2));
System.out.println(s1.equals(s3));
B b = B.class.newInstance();
//System.out.println(b.getX()); //getX()为私有的
B b2 = new B(10);
//Field field = B.class.getField("x");
Field field = B.class.getDeclaredField("x");
field.setAccessible(true);//暴力反射
System.out.println(field.get(b2) );
}
}
8. 利用反射调用一个类中的私有方法
package java高新技术;
import java.lang.reflect.Method;
/**
* 利用反射改变 一个类中字符串的单个字符 把所有的b 改成 a
* @author jzm
*/
class TestMethodClass{
private String x;
private String y;
public TestMethodClass(String x, String y) {
super();
this.x = x;
this.y = y;
}
private void print(int i){
System.out.println("hello"+ i);
}
@Override
public String toString() {
return "x="+x+" y="+y;
}
}
public class TestUserPrivateMethod {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
TestMethodClass t2 = new TestMethodClass("1", "2");
//t.print();
Method method = TestMethodClass.class.getDeclaredMethod("print",new Class[]{int.class});
method.setAccessible(true);
method.invoke(t2,new Object[]{1});
}
}
9. 注解 Annotation
JDK1.5的新特性
包括三个注解类型
可以自定义注解 如:
@Retention(RetentionPolicy.RUNTIME)
public @ interface MyAnnotation {
String color() default "blue";
}
10. 泛型
11 类加载器
JVM 提供了三个类加载器 BootStrap(C++编写的) -à ExtClassLoader(java类), à AppClassLOader(java类)
可以自己写一个类加载器 。
12 动态代理 参照我的另一篇文章