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

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;貌似是类名+地址。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值