{
public static void main(String[] args)
{
System.out.println(new varargs().plus("me"," and ","you"));
}
public String plus(String...args)
{
String str=null;//new String();
for(String x:args)
{
str+=x;
}
return str;
}
}
说明在文字池中内容相同的字符创常量是被合并了的实例
{
public static void main(String[] args)
{
if ("abc"=="abc")
System.out.println("equals");
else
System.out.println("unequals");
}
}
jdk1.5新增了对基本数据的装箱和拆箱,即可以写Interger i1=2;而不用对象封装后再赋值了,比如打印i1+3,也会自动转换成整数然后打出,也就是拆箱;赋值在-128到127之间的会被指向同一个对象,以节省内存;如果使用new来转换,则仍然会被分配给两个对象,如果使用valueOf函数,就是享元模式;
享元模式
将很多内容相同的东西存放在一个对象中,可以减少内存开销;比如java的自动装箱拆箱对一个字节的内容,或者是window的图标;
亨元模式是对象的结构模式。亨元模式以共享的方式高效地支持大量的细粒度对象。
枚举可以保证在编译阶段编译器就可以检查出一些在运行阶段才能发现的赋值不合法之类的问题;
模拟交通灯的枚举程序实例:
enum MyEnum
{
RED(3000)
{
//Thread.sleep(time);
public void nextLamp()
{
System.out.println(time/1000+" second has passed;it's time for GREEN");
}
},
GREEN(2000)
{
// Thread.sleep(time);
public void nextLamp()
{
System.out.println(time/1000+ " second has passed;it's time for YELLOW");
}
},
YELLOW(1000)
{
// Thread.sleep(time);
public void nextLamp()
{
System.out.println(time/1000+" second has passed;it's time for RED");
}
};
public abstract void nextLamp();
int time;
private MyEnum(int time)
{
this.time=time;
}
}
class test
{
public static void main(String[] args)
{
System.out.println("now the traffic lamp is RED");
while(true)
{
for(MyEnum e:MyEnum.values())
{
try{Thread.sleep(e.time);}
catch(Exception error)
{}
e.nextLamp();
}
}
}
}
@SuppressWarnings("deprecation"):告诉编译器我知道这个方法过时了,不要提醒了。
@Deprecated:提醒使用者这个方法过时了。
@override:提醒下面的方法是覆盖父类的方法,如果不是会提醒,比如hash类中的自定义key改写时要覆盖equals,里面的参数必须是object(容易错写为自己所写的类名),否则就没有覆盖,而调用object类本来的equals方法,相当于==;
注解就像源程序要调用的一个类,@interface就可以定义一个注解。注解的注解就是元注解。
@Retension注解设置注解的存活周期;
注解有三个阶段:java源程序(RetensionPolicy.SOURCE)--》class文件(RetensionPolicy.CLASS)--》内存中的字节码
(RetensionPolicy.RUNTIME);默认是CLASS;
@Target设置注解能放的位置;
为注解增加属性:
注解的属性,在@interface内部像接口的抽象方法一样定义,在使用注解时想属性一样赋值,在使用时又像调用类的方法一样使用;如果某个注解中赋值时只有value属性【需要】赋值,就连value=都可以不写。也可以用为其他属性指定缺省属性的方法来达到效果,比如Stringcolor() default "blue";
注解类可以设置数组类型的属性(如果数组部分只有一个元素,可以省略大括号),可以是枚举类型的,可以是注解类型的,可以是class,注解的详细语法可以通过看java语言规范来了解,即java的language specification
用类加载器加载处于classpath处的配置文件,只能读,不能写;
【内省】
IntroSpector-->JavaBean-->特殊的Java类(方法的名字符合特定的规则)
JavaBean的特点是方法名去掉set和get后就是属性名,如getAge(),其属性名就是age,首字母要变成小写,再比如getCPU,其属性名是CPU
在Java EE开发中,经常要用到JavaBean,很多环境也这么要求,所以你没有选择,而且jdk提供了一套相应的API,比普通类的方式更方便,还有值对象也常弄成JavaBean
import java.beans.*;
import java.lang.reflect.*;
class MyPoint
{
int x;
int y;
public MyPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX()
{
return x;
}
public void setX(int x)
{
this.x=x;
}
public int getY()
{
return y;
}
public void setY(int y)
{
this.y=y;
}
}
class IntroSpectorTest
{
public static void main(String[] args) throws Exception
{
MyPoint p1=new MyPoint(3,5);
String propertyName="x";
PropertyDescriptor pd=null;
pd=new PropertyDescriptor(propertyName,p1.getClass());
Method mx=pd.getReadMethod();
Object retVal= mx.invoke(p1);
System.out.println(retVal);
propertyName="y";
pd=new PropertyDescriptor(propertyName,p1.getClass());
mx=pd.getWriteMethod();
mx.invoke(p1,7);
System.out.println(p1.getY());
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------