字符串串联运算符+,当给定一个String操作数和一个引用时,该运算符将通过调用被引用对象的toString方法(如果引用或toString的结果是一个空引用,则使用“null”),把引用转换成String,然后产生一个最近创建的String,它是两个字符串的串接。
例如:
class Point {
int x,y;
Point(){
System.out.println( "default");
}
Point( int x, int y){
this.x=x;
this.y=y;
}
static Point origin= new Point(0,0);
public String toString(){
return "("+x+ ","+y+ ")";
}
}
int x,y;
Point(){
System.out.println( "default");
}
Point( int x, int y){
this.x=x;
this.y=y;
}
static Point origin= new Point(0,0);
public String toString(){
return "("+x+ ","+y+ ")";
}
}
public
class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Point p= null;
try{
p=(Point)Class.forName( "Point").newInstance();
} catch(Exception ex){
System.out.println(ex);
}
Point a[]={ new Point(0,0), new Point(1,1)};
System.out.println( "p: "+p);
System.out.println( "a: "+a[0]+ " "+a[1]);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Point p= null;
try{
p=(Point)Class.forName( "Point").newInstance();
} catch(Exception ex){
System.out.println(ex);
}
Point a[]={ new Point(0,0), new Point(1,1)};
System.out.println( "p: "+p);
System.out.println( "a: "+a[0]+ " "+a[1]);
}
}
结果:
default
p: (0,0)
a: (0,0) (1,1)
p: (0,0)
a: (0,0) (1,1)
转载于:https://blog.51cto.com/moskn/57635