【ThinkingInJava】8、对象的初始化为null,基本类型默认初始化为0
2015-04-14 10:36
375 查看
/** * 书本:《Thinking In Java》 * 功能:对象的初始化为null,基本类型默认初始化为0 * 1、在定义对象的地方。 * 2、在类的构造器中 * 3、在使用这些对象之前,这个叫惰性初始化 * 4、使用实例初始化 * 文件:Bath.java * 时间:2014年10月12日15:21:17 * 作者:cutter_point */ package Lesson7ReusingClasses; import static net.mindview.util.Print.*; //静态引用 class Soap { private String s; Soap() //构造函数 { print("Soap()"); s="Constructed"; } public String toString() { return s; } } public class Bath { private String s1="Happy", s2="Happy!", s3, s4; private Soap castille; private int i; private float toy; public Bath() { print("Inside Bath()"); s3="Joy"; toy=3.14f; castille=new Soap(); } { i=47; } //这个初始化,略屌 public String toString() { if(s4 == null) s4="cutter_point"; return "s1="+s1+"\n"+ "s2="+s2+"\n"+ "s3="+s3+"\n"+ "s4="+s4+"\n"+ "i="+i+"\n"+ "toy="+toy+"\n"+ "castille="+castille+"\n"; } public static void main(String[] args) { Bath b=new Bath(); print(b); } }
输出:
Inside Bath() obj1
Soap() obj1
s1=Happy
s2=Happy!
s3=Joy
s4=cutter_point
i=47
toy=3.14
castille=Constructed
obj1
相关文章推荐
- java中基本数据类型与对象引用类型的默认初始化值学习
- 【Java】【TIJ】验证Java对类的基本数据类型的成员的默认初始化结果
- Java 对象数组初始化 和 基本类型数组初始化的区别
- JAVA学习第三十二课(常用对象API)- 基本数据类型对象包装类
- 黑马程序员--Java基础之String类和基本数据类型对象包装类
- 对Java中引用、对象、基本类型等的理解
- JAVA 对象定义、访问对象、基本类型和引用类型变量区别
- 黑马程序员——Java String、StringBuffer、StringBuilder、基本数据类型对象包装类
- String对象内存分配和基本数据类型的默认值与初始化相关问题
- java和C++互调----(传递基本数据类型和对象)
- Java中字符串对象和基本数据类型包装类
- Java---24---基本数据类型对象包装类
- 黑马程序员_日记29_Java基本数据类型对象包装类
- java基本数据类型对象包装类总结
- 黑马程序员_Java基础_泛型应用 和 基本数据类型对象包装类
- 黑马程序员——Java基础——String类、StringBuffer类、StringBuilder类、基本数据类型对象包装类操作、正则表达式应用
- 黑马程序员——Java基础——对象序列化、管道流、RandomAccessFile类、操作基本数据类型的流对象、字符编码
- java基本数据类型及其所对应对象的自动装箱,new,valueof操作研究
- JAVA高级01_04 基本数据类型的对象包装类 2011-4-20
- java 对象类型Boolean与基本类型boolean的联系、区别