引用静态方法和成员
import static java.Math.*;//吧Math的所有静态方法全部引用过来
可变参数:
public class Test{
//args在使用时是当数组使用
public void add(int x,int ... args){
int sum=x;
for (int i=0;i<args.length;i++){
sum+=args[i];
}
System.out.println(sum);
}
public static void main(){
Test t=new Test();
t.add(2.3);
t.add(1,2,3);
}
}
for循环加强:
for(type 变量名:变量名2){
}
例如:
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
[color=cyan]for(int i:list){
System.out.println(i);
};[/color]
上面采用泛型表明list集合里面的对象是Integer;但是for循环里去是int表示:
这里存在一个知识点是jdk1.5的新特性:
基于数据类型的自动装箱和拆箱
Integer i=15;//将整数装箱成一个对象
System.out.println(i+1);//将对象拆箱成一个整数
一个测试:享元模式:经常用到的当成一个对象 如下面的Integer在-128~127就是同一个对象
Integer i1=11;
Integer i2=11;
Integer i3=211;
Integer i4=211;
Integer i5=Integer.valueOf(11);
Integer i6=Integer.valueOf(11);
Integer i7=Integer.valueOf(211);
Integer i8=Integer.valueOf(211);
System.out.println(i1==i2);//true
System.out.println(i3==i4);//false
System.out.println(i5==i6);//true
System.out.println(i7==i8);//false
String s1="abc";
String s2="abc";
String s3=new String("abc");
String s4=new String("abc");
System.out.println(s1==s2);//true
System.out.println(s3==s4);//false
import static java.Math.*;//吧Math的所有静态方法全部引用过来
可变参数:
public class Test{
//args在使用时是当数组使用
public void add(int x,int ... args){
int sum=x;
for (int i=0;i<args.length;i++){
sum+=args[i];
}
System.out.println(sum);
}
public static void main(){
Test t=new Test();
t.add(2.3);
t.add(1,2,3);
}
}
for循环加强:
for(type 变量名:变量名2){
}
例如:
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
[color=cyan]for(int i:list){
System.out.println(i);
};[/color]
上面采用泛型表明list集合里面的对象是Integer;但是for循环里去是int表示:
这里存在一个知识点是jdk1.5的新特性:
基于数据类型的自动装箱和拆箱
Integer i=15;//将整数装箱成一个对象
System.out.println(i+1);//将对象拆箱成一个整数
一个测试:享元模式:经常用到的当成一个对象 如下面的Integer在-128~127就是同一个对象
Integer i1=11;
Integer i2=11;
Integer i3=211;
Integer i4=211;
Integer i5=Integer.valueOf(11);
Integer i6=Integer.valueOf(11);
Integer i7=Integer.valueOf(211);
Integer i8=Integer.valueOf(211);
System.out.println(i1==i2);//true
System.out.println(i3==i4);//false
System.out.println(i5==i6);//true
System.out.println(i7==i8);//false
String s1="abc";
String s2="abc";
String s3=new String("abc");
String s4=new String("abc");
System.out.println(s1==s2);//true
System.out.println(s3==s4);//false