Saturday, March 7, 2015

OCPJP7 Localization

Thursday, March 5, 2015

OCPJP7 String Processing

1.    String.length()
       String[].length---->Array

2.    String.charAt(): start from 0
       String.concat()
       String.equalIgnoreCase()
       String.length()
       String.replace()
       String.substring(including_index, excluding_index): start from 0
       String.toLowerCase()
       String.toUpperCase()
       String.trim()
       String.toString()

3.    new StringBuilder(); default capacity 16 chars(16*16 bits)
       new StringBuilder("ab");
       new StringBuilder(int capacity);

4.    Following methods are not used in String!!!
       StringBuilder.append()
       StringBuilder.delete(inclusive, exclusive): start from 0
       StringBuilder.reverse()
       StringBuilder.replace(inclusive_index, exclusive_index, String str)
       StringBuilder.insert(offset from 0 to length, String str): start from 0,
       (runtime exception thrown if offset is out of current length)

5.    Searching
       Pattern p = Pattern.compile(target);
       Matcher m = p.matcher(source);
       while(m.find()) {
           System.out.println(m.start() + m.end() + m.group());
       }
     
       Target
        a. Metacharacter:         1) \d (0-9)
                                           2) \D (NOT \d)
                                           3) \s (\s,\t, \n, \r, \f)
                                           4) \S (NOT \s)
                                           5) \w (a-z, A-Z, 0-9, underscore)
                                           6) \W (NOT \w)

         b. Boundary Macthing: \b:  start from position of first Word character, after that record position when previous character is different with current one.
                                           \B:  (NOT \b)

         c. Range: [abc] or [a-f] or [afAF] (-is needed)

         d. Quantifiers:
                                 + one or more (greedy)
                                 * zero or more (greedy)
                                 ? zero or one (greedy)
                                 ^ negative
                                 +? one or more (reluctant)
                                 *? zero or more (reluctant)
                                 ?? zero or one (reluctant)
                                  . any one not null


6. Tokenizing:

    a) String[] tokens = source.split(tokenizing);
        for(String s: tokens) System.out.println(">" + s + "<")

    b) Scanner(default whitespace, usedelimiter(string) to set delimiter)
        Scanner s1 = new Scanner(source);
        s1.hasNext()
        s1.hasNextInt() (1.3 IS OKAY)
        s1.nextInt()
        s1.nextBoolean
        s1.next()

     c) StringTokenizer(default whitespace)
         StringTokenizer st = new StringTokenizer("source");
         st.countTokens()
         st.hasMoreTokens()
         st.nextToken()

7. System.out.println("\" \\"); ----> " \
    Source.split("\\."); search for period.

8. System.out.printf() == System.out.format()

9. %[arg_index$] [flags][width][.precision] conversion char

    flags: - left justify
             + include a sign (+/-)
             0 pad with zero
             ,  use locale-specific
             ( enclose negative numbers in parentheses )
    width:
    precision: (floating point)
    conversion : b|c|d|f|s  (Barring use of boolean, mismatch will get a run time exception)

10. System.out.printf("%d", 123.2);  ERROR!!!
      System.out.printf("%f", 123);      ERROR!!!
      System.out.printf("%b", 123.1);  GOOD!!!

Wednesday, March 4, 2015

OCPJP7 Assertion

OCPJP7 Exception Handling



  1. Closeable (Java 5) throws IOException

    AutoCloseable (Java 7) throws Exception

  2. Closeable extends AutoCloseable.

  3. try-with-resources : close when get out of the try block in reverse order.

  4. If more than one exception is thrown in a try-with-resources block

    All later exception will be added as suppressed.

  5. try {

    }catch(Exception 1 | Exception 2 e) {

    }

    There should be no subclass relationship between Exception 1 and Exception 2. Do not assign a new value to "e" in multi-catch.

  6. If exception throws in catch block or finally block, no suppressions happen.

    The last exception thrown get sent to the caller.

  7. If a method throws a exception in the declaration, it must be included in try catch block. Even if it is in the finally block.


  8. Try-with-resource Object must not have close method if that Object is declared in Try block.

Sunday, February 22, 2015

OCPJP7 JDBC

1. JDBC 3.0: Class.forName("org.gjt.mm.mysql.Driver")
2. JDBC 4.0: META-INF/services/java.sql.Driver, Transaction, Driver, Connection, Statement and ResultSet Interface.
3. DatabaseMetaData can get default transaction isolation level, the name of JDBC driver and the name of stored procedures in the database.
4. When ResultSet returns the cursor point to the first row before the ResultSet. You must call next().
5. executeupdate(sql) takes INSERT, UPDATE, DELETE AND return int, executequery(sql) take only select and return ResultSet.
6. When connection is close, ResultSet.next() will return SQLException.
7. PreparedStatement和CallableStatement execute 不带参数
8. RowSetFactory does not extend AutoClosable