Chapter1

Un article de Sometimes Kitties Think Too.

SCJP


Sommaire

Chapter 1



Legal Identifiers

       + variable names (identifiers) can start with "_" and "$" and A-Za-z  but no numbers
       + case-sensitive
       + no keywords
       abstract
       boolean
       break
       byte
       case
       catch
       char
       class
       const
       continue
       default
       do
       double
       else
       extends
       final
       finally
       float
       for
       goto
       if
       implements
       import
       instanceof
       int
       interface
       long
       native
       new
       package
       private
       protected
       public
       return
       short
       static
       strictfp
       super
       switch
       synchronized
       this
       throw
       throws
       transient
       try
       void
       volatile
       while
       assert
       enum
       + camelCase should be used for method and variable identifiers

JavaBean Naming Standards

       + getter/setter methods for properties (private variables) that are non-boolean
       + boolean property getter methods are "get" or "is"
       + setter methods are public, void
       + getter methods are public and take no arguments.  They return the same type of arg that the setter method takes                
       + listener methods take as an argument the type of listener

Source File Declaration Rules

       + package statement is first before all others
       + import and import static statements come next
       + more than one non-public class can be in a file
       + a file with no public classes doesn't have to have the filename match any of the classes contained within the file


Importing

       + syntax is 
            import java....
            import static java...*
       + static import allows you to import all the static members of a class without qualification

Class Declarations & Modifiers

       + top-level (non-inner) classes can only use "public" or "default" or "strictfp"
       Access modifiers:
               default, public, protected, private.
       default --
               package-level access
       public --
               full access to all packages and classes

       Public >> Protected >> Default >> Private


       Non-access modifiers
               (including strictfp, final, and abstract).
       strictfp --
               class conforms to the IEEE 754 standard for floating points
       final --
               class cannot be subclassed
       abstract --
               class can never be instantiated
               abstract's class methods are declared with semi-colons not {}
        + any non-abstract class becomes abstract if it contains at least one abstract method
        + abstract classes can contain non-abstract methods
        + classes declared final and abstract will generate a compile-time error

Implementing Interfaces

       + an interface is a 100% abstract class
       + all intf methods must be implemented
       + all intf methods are public and abstract NOT STATIC
       + intfs can only declare constant variables, thereby all variables are
               _implicitly_ public, static and final
       + intf methods cannot be static, final, strictfp or native
       + intfs can extend only other intfs, but they cannot implement other intfs or classes

Class Member Modifiers

(for class methods and variables)

Access Modifiers for Class Members

       + protected -- 
               can be accessed by a subclass (through inheritance) by any class even if 
               it's in a different package and by members of the same package
               must use instance of subclass to access protected variables
       + default 
       + private -- Members marked private can't be accessed by code in any class other than the class in which the private member was declared.  Inner classes can be private

Local Variables

       + only modifier appliable to a local variable is final
       + a final argument means that the value cannot be manipulated/reassigned                                                                              in the method
       + created on the stack and scope is limited to the method in which they were declared  
       + compiler will throw an error if an local variable is used before it's initialized
       + do not get a default value

Non-access Member Modifiers

       abstract, final, strictfp, synchronized, static, transient, native
       + abstract and static == illegal combination
       + native -- method is implmented in native code.  Declaration ends in semi-colon
       + final -- makes it impossible to reinitialize that variable once it has been initialized with an explicit value. 
                        For primitives, this means that once the variable is assigned a value, the value can't be altered.
                        A reference variable marked final can't ever be reassigned to refer to a different object.

Instance Variables

       + Allowed:      public, protected, private, static, final, transient, volatile
       + Forbidden:    abstract, synchronized, strictfp, native

Var-args

       1) var-arg declaration must go at the end of the list of parameters
       2) ( <type>... variable_name )
       3) can take an array or nothing.  
          e.g.   void meth(int...)  can take meth(new int[2]) or meth()

Constructors

       + never have a return type
       + can't be marked static, final or abstract
       + can throw exceptions

Primatives

      Type |     Bits    Bytes   Minimum         Maximum
     =====================================================
      byte    |  8      1       -2^7            2^7-1
      boolean | 16      2        0              2^16-1
      short   | 16      2       -2^15           2^15-1
      char    | 16      2       \u0000          \uffff
      int     | 32      4       -2^31           2^31-1
      float   | 32      4       n/a             n/a
      long    | 64      8       -2^63           2^63-1
      double  | 64      8       n/a             n/a


Array Declarations

       + hold objects of the same type
       + live on heap


  • final variables cannot be reassigned once declared. Nor can they be repointed to a new object

Enums

       + can be declared as own separate class, class member, but NOT in a method !!
                                                                           
  • Declaring Enums outside a class
       e.g.
          enum Something { THIS, THAT, OTHER}         <--- semi-colon is optional  
          class SomethingElse {                                                    
            ...
            Something s = Something.THIS;
            ...
          }
       + enums are compile-time constants
       + enums have the method values() which returns an array of the enum's values (all of the same type as the enum)
       + enum values have a method ordinal() which returns position of particular value
       + enums can be used as the case value in switch statements
         !! However, use the value w/o the enum class nomenclature!!
       e.g 
          enum Cars { MALIBU, YUGO, LADA, PEUGEOT }
          ...
          switch(c) {
          case YUGO:
          case LADA:
          etc...


Enum Tools

       + values() , ordinal()
       + static <E extends Enum<E>> EnumSet<E>.range( E from, E to)

Access Levels


   Image:access_modifiers.jpg
   Access      (public, private, protected, default)
   non-Access  (abstract, final, strictfp, synchronized, static, transient, native)


                         |      Access Modifier        |       Non-Access Modifier                                                                          
   ------------------------------------------------------------------------------------------                                                                            
   non-Inner Classes   |       public, default         |       strictfp,abstract,final
   ------------------------------------------------------------------------------------------
   Inner Classes       |       protected, public,      |       static (except method
                       |       default, private        |               inner classes)
   ------------------------------------------------------------------------------------------
   Constructors        |
   ------------------------------------------------------------------------------------------
   Interfaces          |       public                  |       abstract
                       |                               |
   ==========================================================================================
   Methods             |                               |       static
   ------------------------------------------------------------------------------------------
   Instance Variables  |       protected, public,      |       final, transient, volatile
                       |       default, private        |
   ------------------------------------------------------------------------------------------
   Local Variables     |                               |       final, <none>
   ------------------------------------------------------------------------------------------

Compile-time Constants

+expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

   * Literals of primitive type and literals of type String (§3.10.5)
   * Casts to primitive types and casts to type String
   * The unary operators +, -, ~, and ! (but not ++ or --)
   * The multiplicative operators *, /, and %
   * The additive operators + and -
   * The shift operators <<, >>, and >>>
   * The relational operators <, <=, >, and >= (but not instanceof)
   * The equality operators == and !=
   * The bitwise and logical operators &, ^, and |
   * The conditional-and operator && and the conditional-or operator ||
   * The ternary conditional operator ? :
   * Parenthesized expressions whose contained expression is a constant express                                                                             ion.
   * Simple names that refer to constant variables (§4.12.4).
   * Qualified names of the form TypeName . Identifier that refer to constant v                                                                             ariables (§4.12.4).</pre>