技巧心得:SCJP培训笔记

文章作者 100test 发表时间 2007:03:14 17:38:44
来源 100Test.Com百考试题网


Initialization
  
  初始化
  
  * All class-level (member) variables are initialized before they can
  be used.
  All local variables are not initialized until it is done explicitly.
  
  
  * 所有的主成员在他们使用之前被初始化
  所有的局部变量必须通过显式的赋值来初始化
  
  * An array object (as distinct from reference) is always initialized
  (with zeroes or nulls)
  
  * 数组对象总是能够初始化(零或者null)
  
  * Member initialization with the declaration has exception problems:
  - cannot call methods that throw a checked exception.
  - cannot do error recovery from runtime exceptions.
  - If you need to deal with errors you can put the initialization code
  along with try/catch statements in either a ctor (for instance fields)
  or in a static initialization block for static fields. You can also have
  instance (non-static) initialization blocks but ctors are more
  recognizable.
  
  * 需要处理异常的成员初始化
  - 不能调用会抛出异常的方法
  - 不能对基本异常做任何处理
  - 如果你需要处理错误,将初始化的代码放到构造器或者静态初始化块的
  try/catch块中,当然,你也可以放到非静态的代码块中,但是构造器似乎更为通用。
  
  ------------------------------------------------------------------------
  
  Strings
  
  字符串
  
  * The String class
  - Because string is an immutable class, its instance methods that
  look like they would transform the object they are invoked upon,
  do not alter the object and instead return new String objects.
  - String has methods concat(String),trim(),replace(char,char)
  - String has static valueOf methods for a whole bunch of primitives
  and for Object too (equivalent to Object.toString()).
  - in substring(int,int), the second arg is exclusive.
  - indexOf methods returns -1 for not found
  
  * 类String
  - 类String是不可变的,即使他的某些方法看起来会改变字符串的内容,但实际
  上他们返回的是一个新的字符串,而不是改变原来的字符串
  - 类String的方法:cancat(String),trim(),replace(char,char)
  - 类String的静态方法valueOf能处理所有的基本类型和对象(调用对象的
  toString()方法)
  - 在substring(int,int)方法中,第二个参数是"不包括"的(译者注:第一个参
  数是"包括"的,例如substring(1,4)将会返回字符串从第二个字符开始(包括
  第二个字符),到第五个字符结束(不包括第五个字符)的子字符串)
  - 如果没有找到,indexOf方法将返回-1
  
  * String Pool:
  A JVM has a string pool where it keeps at most one object of any
  String. String literals always refer to an object in the string
  pool. String objects created with the new operator do not refer to
  objects in the string pool but can be made to using Strings intern()
  method. Two String references to equal strings in the string pool
  will be ==.
  
  * 字符串池
  虚拟机有一个字符串池,保存着几乎所有的字符串对象。字符串表达式总是指向
  字符串池中的一个对象。使用new操作创建的字符串对象不指向字符串池中的对象
  但是可以使用intern方法使其指向字符串池中的对象(译者注:如果池中已经有
  相同的字符串--使用equals方法确定,则直接返回池中的字符串,否则先将字符串
  添加到池中,再返回)。池中两个相等的字符串如果使用==来比较将返回真

相关文章


J2EE建议的学习路线!
技巧心得:Java人才的未来
JAVA题库:格林模拟试题三(上)(8)
Java中的封装之我见
技巧心得:SCJP培训笔记
Java中static、this、super、final用法
JAVA程序员面试之《葵花宝典》
SCJP认证套题解析之二
SCJP认证套题解析之一
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛