SCJP总结 1


1. if and switch statements
  • Watch out for boolean assignments (=) that can be mistaken for boolean equality (==) tests
  • Switch statements can evaluate only the byte, short, int, and char data types.
  • The case argument must be a literal or final variable! You cannot have a case that includes a non-final variable, or a range of values.
  • If the condition in a switch statement matches a case value, execution will run through all code in the switch following the matching case statement until a break or the end of the switch statement is ncountered. In other words, the matching case is just the entry point into the case block, but unless there’s a break statement, the matching case is not the only case code that runs.
  • The default block can be located anywhere in the switch block

2. Exception
  • Exceptions come in two flavors: checked and unchecked.
  • Checked exceptions include all subtypes of Exception, excluding classes that extend RuntimeException.Subtypes of Error or RuntimeException are unchecked, so the compiler doesn’t enforce the handle or declare rule.
  • The only exception to the finally-will-always-be-called rule is that a finally will not be invoked if the JVM shuts down.
  • Just because finally is invoked does not mean it will complete. Code in the finally block could itself raise an exception or issue a System.exit().

3. Assertion
  • Do not use assertions to validate arguments to public methods.
  • Do not use assert expressions that cause side effects.
  • Do use assertions?even in public methods?to validate that a particular code block will never be reached. You can use assert false; for code that should never be reached, so that an assertion error is thrown immediately if the assert statement is executed.

4. Overridding and Overloading
  • Polymorphism applies to overriding, not to overloading
  • Reference type determines which overloaded method will be used at compile time.

5. Constructor
  • The constructor calls its superclass constructor, which calls its superclass constructor, and so on all the way up to the Object constructor. The Object constructor executes and then returns to the calling constructor, which runs to completion and then returns to its calling constructor, and so on back down to the completion of the constructor of the actual instance being created.
  • Constructors can use any access modifier (even private!).
  • The first statement of every constructor must be a call to either this() (an overloaded constructor) or super().The compiler will add a call to super() if you do not, unless you
  • have already put in a call to this().
  • this() and super() cannot be in the same constructor. You can have one or the other, but never both.
6. String & StringBuffer
  • StringBuffer equals() is not overridden; it doesn’t compare values.
  • String methods use zero-based indexes, except for the second argument of substring().

7. java.lang.Math
  • The random() method returns a double greater than or equal to 0.0 and less than 1.0.
  • The methods ceil(), floor(), and round() all return integer equivalent floating-point numbers, ceil() and floor() return doubles, round() returns a float if it was passed an int, or it returns a double if it was
  • passed a long. Round means the number add .5 and do a floor().
  • The method sqrt() can return NaN if the argument is NaN or less than zero.
  • NaN is not equal to anything, not even itself.

8. Using Wrappers
  • Wrappers won’t pass equals() if they are in different classes.
  • Radix refers to bases (typically) other than 10; binary is radix 2, octal = 8, hex = 16.

10. equals and ==
  • The compiler will not allow == if the classes are not in the same hierarchy.

posted on 2007-04-13 19:21 肥虫 阅读(246) 评论(0)  编辑  收藏 所属分类: Java Language