您的位置:首页 > 编程语言 > Java开发

JAVA复习之JAVA中的一些小细节

2011-12-15 11:07 316 查看
import java.util.*;
public class BerylliumSphere {
private static long counter;
private final long id=counter++;
public String toString(){
return "Sphere "+id;
}

}
import java.util.*;
import java.io.*;
public class ContianerComparion {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BerylliumSphere [] spheres=new BerylliumSphere[10];
for(int i=0;i<5;i++)
spheres[i]=new BerylliumSphere();
System.out.print(Arrays.toString(spheres));
System.out.print(spheres[4]);
}

}

输出结果:Sphere 0, Sphere 1, Sphere 2, Sphere 3, Sphere 4, null, null, null, null, null]Sphere 4

最近搞JAVA复习,看了些小例子,以下是几个小的细节:

1.java中,对于方法的局部变量,在使用之前必须进行初始化,否则会有编译错误。而对于类的数据成员情况则不同,如果类的数据成员是基本类型,则JAVA会给默认的初始值。boolean—false,char,byte,short,int,long初值为0,float,double初值为0.0.

2.在定义对象的引用时,如果不将其初始化,此引用会获得一个特殊值null.

3.print()函数输出的是对象toString()函数的返回值。注意,JAVA中每个类都有toString(),如果没有对其重载将使用默认的toString()。例如,上例中如果不将toString()承载,输出结果将为:[test.BerylliumSphere@c17164,
test.BerylliumSphere@1fb8ee3,
test.BerylliumSphere@61de33, test.BerylliumSphere@14318bb,
test.BerylliumSphere@ca0b6, null, null, null, null, null]test.BerylliumSphere@ca0b6;貌似是类名+地址。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: