学习目标
–掌握jdk5.0中出现的新特性
.泛型(Generics)
.增强的“for”循环(Enhanced For loop)
.自动装箱/自动拆箱(Autoboxing/unboxing)
.类型安全的枚举(Type safe enums)
.静态导入(Static import)
.可变参数(Var args)型
泛型:JDK1.5版本以后出现新特性。用于解决安全问题,是一个类型安全机制。
好处:1.将运行时期出现问题ClassCastException(类型转换错误),转移到了编译时期。,方便于程序员解决问题。让运行时问题减少,安全。
2.避免了强制转换麻烦。
泛型格式:通过<>来定义要操作的引用数据类型。
在使用java提供的对象时,什么时候写泛型呢?
通常在集合框架中很常见,只要见到<>就要定义泛型。其实<> 就是用来接收类型的。当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可。
<span style="font-size:14px">public class GenericsFoo<T>
{
private T foo;
public T getFoo()
{
return foo;
}
public void setFoo(T foo)
{
this.foo = foo;
}
public static void main(String[] args)
{
GenericsFoo<Boolean> foo1=new GenericsFoo<Boolean>();
GenericsFoo<Integer> foo2=new GenericsFoo<Integer>();
foo1.setFoo(new Boolean(false));
foo2.setFoo(new Integer(3));
boolean b=foo1.getFoo();
Integer in=foo2.getFoo();
System.out.println(b);
System.out.println(in);
//foo1=foo2;
//Type mismatch: cannot convert from GenericsFoo<Integer> to GenericsFoo<Boolean>
GenericsFoo a=new GenericsFoo();
a.setFoo("hello");
String a1=(String)a.getFoo();
System.out.println(a1);
}
}
</span>
增强for循环
其語法如下
–for(type element : array)
{
System.out.println(element).
...
}
例子如下:
<span style="font-size:14px">import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ForDemo
{
/**
* @param args
*/
public static void main(String[] args)
{
int[] arr=new int[]{4,7,6,5,3,1};
//第一种方式
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("------------------");
//第二种方式
for(int element:arr)
{
System.out.println(element);
}
System.out.println("------------------");
//二维数组遍历
int[][] arr1=new int[][]{{3,7,9},{2,4,6},{4,8,9}};
for(int[] row:arr1)
{
for(int col:row)
{
System.out.println(col);
}
}
List<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("d");
list.add("e");
//第一种方式
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
System.out.println("-------------");
//第二种方式
for(Iterator<String> iter=list.iterator();iter.hasNext();)
{
System.out.println(iter.next());
}
System.out.println("-------------");
//第三种方式
for(String str:list)
{
System.out.println(str);
}
}
}
</span>
自动的装箱拆箱
自动装箱/拆箱大大方便了基本类型数据和它们包装类的使用。
自动装箱:基本类型自动转为包装类.(int >> Integer)
自动拆箱:包装类自动转为基本类型
<span style="font-size:14px">/**
* 自动的装箱拆箱
*/
import java.util.ArrayList;
import java.util.Collection;
public class BoxTest
{
/**
* @param args
*/
public static void main(String[] args)
{
int a=3;
Collection<Integer> c=new ArrayList<Integer>();
c.add(3);
c.add(a+3);
c.add(10);
for(int ele:c){
System.out.println(ele);
}
}
}</span>
类型安全的枚举
public enum Color
.然后可以这样来使用Color myColor = Color.Red.
for (Color c : Color.values()) {
静态导入
要使用静态成员(方法和变量)我们必须给出提供这个静态成员的类。使用静态导入可以使被导入类的所有静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名.例子如下:
<span style="font-size:14px">import static java.lang.Math.max;
import static java.lang.Math.abs;
public class Demo
{
/**
* 静态导入
*/
public static void main(String[] args)
{
//原本的第一种方式
// System.out.println(Math.max(3,9));
// System.out.println(Math.abs(3-6));
//使用了静态导入的第二种方式
System.out.println(max(3, 6));
System.out.println(abs(3 - 6));
}
}
</span>
可变参数(本质上就是个数组)
<span style="font-size:14px">public class VariableParamter
{
public static void main(String[] args)
{
System.out.println(add(2,3));
System.out.println(add(2,3,4));
}
public static int add(int a,int...args)
{
int sum=0;
for(int i=0;i<args.length;i++)
{
sum+=args[i];
}
return sum;
}
}
</span>
总结:
1. 当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式
来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息。2. Integer类有一个缓存,它会缓存介于-128~127之间的整数。
3. 可变参数:可变参数本质上就是一个数组,对于某个声明了可变参数的方法来说,
我们既可以传递离散的值,也可以传递数组对象。但如果将方法中的参数定义为数
组,那么只能传递数组对象而不能传递离散的值。
4. 可变参数必须要作为方法参数的最后一个参数,即一个方法不可能具有两个或两个
以上的可变参数。
5. 枚举(Enum):我们所定义的每个枚举类型都继承自java.lang.Enum类,枚举中的每
个成员默认都是public static final的。
6. 而每个枚举的成员其实就是您定义的枚举类型的一個实例(Instance)。换句话说,
当定义了一个枚举类型后,在编译时刻就能确定该枚举类型有几个实例,分别是什
么。在运行期间我们无法再使用该枚举类型创建新的实例了,这些实例在编译期间
就已经完全确定下来了。
7. 静态导入:
a) import static com.common.Common.Age;
b) import static com.Common.output;
8. 表示导入Common类中的静态成员变量AGE以及静态方法output。注意:使用import
static时,要一直导入到类中的静态成员变量或静态方法。
9. Java中,无论生成某个类的多少个对象,这些对象都会对应于同一个Class对象。