Initialization
初始化
*Allclass-level(member)variablesareinitializedbeforetheycan
beused.
Alllocalvariablesarenotinitializeduntilitisdoneexplicitly.
*所有的主成员在他们使用之前被初始化
所有的局部变量必须通过显式的赋值来初始化
*Anarrayobject(asdistinctfromreference)isalwaysinitialized
(withzeroesornulls)
*数组对象总是能够初始化(零或者null)
*Memberinitializationwiththedeclarationhasexceptionproblems:
-cannotcallmethodsthatthrowacheckedexception.
-cannotdoerrorrecoveryfromruntimeexceptions.
-Ifyouneedtodealwitherrorsyoucanputtheinitializationcode
alongwithtry/catchstatementsineitheracTor(forinstancefields)
orinastaticinitializationblockforstaticfields.Youcanalsohave
instance(non-static)initializationblocksbutcTorsaremore
recognizable.
*需要处理异常的成员初始化
-不能调用会抛出异常的方法
-不能对基本异常做任何处理
-如果你需要处理错误,将初始化的代码放到构造器或者静态初始化块的
try/catch块中,当然,你也可以放到非静态的代码块中,但是构造器似乎更为通用。
————————————————————————
Strings
字符串
*TheStringclass
-Becausestringisanimmutableclass,itsinstancemethodsthat
lookliketheywouldtransformtheobjecttheyareinvokedupon,
donotaltertheobjectandinsteadreturnnewStringobjects.
-Stringhasmethodsconcat(String),trim(),replace(char,char)
-StringhasstaticvalueOfmethodsforawholebunchofprimitives
andforObjecttoo(equivalenttoObject.toString()).
-insubstring(int,int),thesecondargisexclusive.
-indexOfmethodsreturns-1for‘notfound‘
*类String
-类String是不可变的,即使他的某些方法看起来会改变字符串的内容,但实际
上他们返回的是一个新的字符串,而不是改变原来的字符串
-类String的方法:cancat(String),trim(),replace(char,char)
-类String的静态方法valueOf能处理所有的基本类型和对象(调用对象的
toString()方法)
-在substring(int,int)方法中,第二个参数是”不包括”的(译者注:第一个参
数是”包括”的,例如substring(1,4)将会返回字符串从第二个字符开始(包括
第二个字符),到第五个字符结束(不包括第五个字符)的子字符串)
-如果没有找到,indexOf方法将返回-1
*StringPool:
AJVMhasastrin 你所缺少的部分,也早已被我用想像的画笔填满。