
- •Contents at a Glance
- •Contents
- •About the Authors
- •About the Technical Reviewer
- •Acknowledgments
- •Introduction
- •Oracle Java Certifications: Overview
- •FAQ 1. What are the different levels of Oracle Java certification exams?
- •FAQ 4. Is OCPJP 7 prerequisite for other Oracle certification exams?
- •FAQ 5. Should I take the OCPJP 7 or OCPJP 6 exam?
- •The OCPJP 7 Exam
- •FAQ 7. How many questions are there in the OCPJP 7 exam?
- •FAQ 8. What is the duration of the OCPJP 7 exam?
- •FAQ 9. What is the cost of the OCPJP 7 exam?
- •FAQ 10. What are the passing scores for the OCPJP 7 exam?
- •FAQ 11. What kinds of questions are asked in the OCPJP 7 exam?
- •FAQ 12. What does the OCPJP 7 exam test for?
- •FAQ 13. I’ve been a Java programmer for last five years. Do I have to prepare for the OCPJP 7 exam?
- •FAQ 14. How do I prepare for the OCPJP 7 exam?
- •FAQ 15. How do I know when I’m ready to take the OCPJP 7 exam?
- •Taking the OCPJP 7 Exam
- •FAQ 16. What are my options to register for the exam?
- •FAQ 17. How do I register for the exam, schedule a day and time for taking the exam, and appear for the exam?
- •The OCPJP 7 Exam: Pretest
- •Answers with Explanations
- •Post-Pretest Evaluation
- •Essentials of OOP
- •FunPaint Application: An Example
- •Foundations of OOP
- •Abstraction
- •Encapsulation
- •Inheritance
- •Polymorphism
- •Class Fundamentals
- •Object Creation
- •Constructors
- •Access Modifiers
- •Public Access Modifier
- •Private Access Modifier
- •Protected and Default Access Modifier
- •Overloading
- •Method Overloading
- •Constructor Overloading
- •Overload resolution
- •Points to Remember
- •Inheritance
- •Runtime Polymorphism
- •An Example
- •Overriding Issues
- •Overriding: Deeper Dive
- •Invoking Superclass Methods
- •Type Conversions
- •Upcasts and Downcasts
- •Casting Between Inconvertible Types
- •Using “instanceof” for Safe Downcasts
- •Java Packages
- •Working with Packages
- •Static Import
- •Summary
- •Abstract Classes
- •Points to Remember
- •Using the “final” Keyword
- •Final Classes
- •Final Methods and Variables
- •Points to Remember
- •Using the “static” Keyword
- •Static Block
- •Points to Remember
- •Flavors of Nested Classes
- •Static Nested Classes (or Interfaces)
- •Points to Remember
- •Inner Classes
- •Points to Remember
- •Local Inner Classes
- •Points to Remember
- •Anonymous Inner Classes
- •Points to Remember
- •Enum Data Types
- •Points to Remember
- •Summary
- •Interfaces
- •Declaring and Using Interfaces
- •Points to Remember
- •Abstract Classes vs. Interfaces
- •Choosing Between an Abstract Class and an Interface
- •Object Composition
- •Composition vs. Inheritance
- •Points to Remember
- •Design Patterns
- •The Singleton Design Pattern
- •Ensuring That Your Singleton Is Indeed a Singleton
- •The Factory Design Pattern
- •Differences Between Factory and Abstract Factory Design Patterns
- •The Data Access Object (DAO) Design Pattern
- •Points to Remember
- •Summary
- •Generics
- •Using Object Type and Type Safety
- •Using the Object Class vs. Generics
- •Container Implementation Using the Object Class
- •Container Implementation Using Generics
- •Creating Generic Classes
- •Diamond Syntax
- •Interoperability of Raw Types and Generic Types
- •Generic Methods
- •Generics and Subtyping
- •Wildcard Parameters
- •Limitations of Wildcards
- •Bounded Wildcards
- •Wildcards in the Collections Class
- •Points to Remember
- •The Collections Framework
- •Why Reusable Classes?
- •Basic Components of the Collections Framework
- •Abstract Classes and Interfaces
- •Concrete Classes
- •List Classes
- •ArrayList Class
- •The ListIterator Interface
- •The LinkedList Class
- •The Set Interface
- •The HashSet Class
- •The TreeSet Class
- •The Map Interface
- •The HashMap Class
- •Overriding the hashCode() Method
- •The NavigableMap Interface
- •The Queue Interface
- •The Deque Interface
- •Comparable and Comparator Interfaces
- •Algorithms (Collections Class)
- •The Arrays Class
- •Methods in the Arrays Class
- •Array as a List
- •Points to Remember
- •Summary
- •Generics
- •Collections Framework
- •Processing Strings
- •String Searching
- •The IndexOf() Method
- •The regionMatches() Method
- •String Parsing
- •String Conversions
- •The Split() Method
- •Regular Expressions
- •Understanding regex Symbols
- •Regex Support in Java
- •Searching and Parsing with regex
- •Replacing Strings with regex
- •String Formatting
- •Format Specifiers
- •Points to Remember
- •Summary
- •Reading and Writing from Console
- •Understanding the Console Class
- •Formatted I/O with the Console Class
- •Special Character Handling in the Console Class
- •Using Streams to Read and Write Files
- •Character Streams and Byte Streams
- •Character Streams
- •Reading Text Files
- •Reading and Writing Text Files
- •“Tokenizing” Text
- •Byte Streams
- •Reading a Byte Stream
- •Data Streams
- •Writing to and Reading from Object Streams: Serialization
- •Serialization: Some More Details
- •Points to Remember
- •Summary
- •A Quick History of I/O APIs
- •Using the Path Interface
- •Getting Path Information
- •Comparing Two Paths
- •Using the Files Class
- •Checking File Properties and Metadata
- •Copying a File
- •Moving a File
- •Deleting a File
- •Walking a File Tree
- •Revisiting File Copy
- •Finding a File
- •Watching a Directory for Changes
- •Points to Remember
- •Summary
- •Introduction to JDBC
- •The Architecture of JDBC
- •Two-Tier and Three-Tier JDBC Architecture
- •Types of JDBC Drivers
- •Setting Up the Database
- •Connecting to a Database Using a JDBC Driver
- •The Connection Interface
- •Connecting to the Database
- •Statement
- •ResultSet
- •Querying the Database
- •Updating the Database
- •Getting the Database Metadata
- •Points to Remember
- •Querying and Updating the Database
- •Performing Transactions
- •Rolling Back Database Operations
- •The RowSet Interface
- •Points to Remember
- •Summary
- •Define the Layout of the JDBC API
- •Connect to a Database by Using a JDBC driver
- •Update and Query a Database
- •Customize the Transaction Behavior of JDBC and Commit Transactions
- •Use the JDBC 4.1 RowSetProvider, RowSetFactory, and RowSet Interfaces
- •Introduction to Exception Handling
- •Throwing Exceptions
- •Unhandled Exceptions
- •Try and Catch Statements
- •Programmatically Accessing the Stack Trace
- •Multiple Catch Blocks
- •Multi-Catch Blocks
- •General Catch Handlers
- •Finally Blocks
- •Points to Remember
- •Try-with-Resources
- •Closing Multiple Resources
- •Points to Remember
- •Exception Types
- •The Exception Class
- •The RuntimeException Class
- •The Error Class
- •The Throws Clause
- •Method Overriding and the Throws Clause
- •Points to Remember
- •Custom Exceptions
- •Assertions
- •Assert Statement
- •How Not to Use Asserts
- •Summary
- •Introduction
- •Locales
- •The Locale Class
- •Getting Locale Details
- •Resource Bundles
- •Using PropertyResourceBundle
- •Using ListResourceBundle
- •Loading a Resource Bundle
- •Naming Convention for Resource Bundles
- •Formatting for Local Culture
- •The NumberFormat Class
- •The Currency Class
- •The DateFormat Class
- •The SimpleDateFormat Class
- •Points to Remember
- •Summary
- •Introduction to Concurrent Programming
- •Important Threading-Related Methods
- •Creating Threads
- •Extending the Thread Class
- •Implementing the Runnable Interface
- •The Start( ) and Run( ) Methods
- •Thread Name, Priority, and Group
- •Using the Thread.sleep() Method
- •Using Thread’s Join Method
- •Asynchronous Execution
- •The States of a Thread
- •Two States in “Runnable” State
- •Concurrent Access Problems
- •Data Races
- •Thread Synchronization
- •Synchronized Blocks
- •Synchronized Methods
- •Synchronized Blocks vs. Synchronized Methods
- •Deadlocks
- •Other Threading Problems
- •Livelocks
- •Lock Starvation
- •The Wait/Notify Mechanism
- •Let’s Solve a Problem
- •More Thread States
- •timed_waiting and blocked States
- •waiting State
- •Using Thread.State enum
- •Understanding IllegalThreadStateException
- •Summary
- •Using java.util.concurrent Collections
- •Semaphore
- •CountDownLatch
- •Exchanger
- •CyclicBarrier
- •Phaser
- •Concurrent Collections
- •Apply Atomic Variables and Locks
- •Atomic Variables
- •Locks
- •Conditions
- •Multiple Conditions on a Lock
- •Use Executors and ThreadPools
- •Executor
- •Callable, Executors, ExecutorService, ThreadPool, and Future
- •ThreadFactory
- •The ThreadLocalRandom Class
- •TimeUnit Enumeration
- •Use the Parallel Fork/Join Framework
- •Useful Classes of the Fork/Join Framework
- •Using the Fork/Join Framework
- •Points to Remember
- •Summary
- •Using java.util.concurrent Collections
- •Applying Atomic Variables and Locks
- •Using Executors and ThreadPools
- •Using the Parallel Fork/Join Framework
- •Chapter 3: Java Class Design
- •Chapter 4: Advanced Class Design
- •Chapter 5: Object-Oriented Design Principles
- •Chapter 6: Generics and Collections
- •Chapter 7: String Processing
- •Chapter 8: Java I/O Fundamentals
- •Chapter 9: Java File I/O (NIO.2)
- •Chapter 10: Building Database Applications with JDBC
- •Chapter 11: Exceptions and Assertions
- •Chapter 12: Localization
- •Chapter 13: Threads
- •Chapter 14: Concurrency
- •OCPJP7 Exam (1Z0-804 a.k.a. Java SE 7 Programmer II) Topics
- •OCPJP 7 Exam (1Z0-805, a.k.a. Upgrade to Java SE 7 Programmer) Topics
- •Answers and Explanations
- •Answer Sheet
- •Answers and Explanations
- •Index
Chapter 7 ■ String proCeSSing
This snippet prints the following:
255.245.188.123 is valid? True 255.245.188.273 is valid? False
The first string (ipStr1) is a valid IP address and the second string (ipStr2) is not a valid IP address. The regex specified by you successfully identified the valid IP address. Don’t be alarmed at the lengthy regex; it will be easy once you understand it.
Let’s begin with the start and end symbols—"\b" is a boundary marker, as you saw earlier. Now, let’s look at the first group, "((25[0–5]|2[0–4]\d|[01]?\d\d?)(\.))", which has two parts: the first part specifies the regex for a number less than 256 and second part specifies a dot. The first part of the first group has 3 subexpressions.
The first subexpression specifies that the number could be in the range between 250 and 255. The second subexpression specifies that the number could be 200 to 249, and the third subexpression specifies that the number could be 0 to 199. You want to repeat this first group three times, so you place "{3}" immediately after the first group. The second group is nothing but the first group without a dot.
String Formatting
So far we have discussed how to search or parse a String object. What if you would like to format a string in a predefined template? For example, let’s assume that you are computing the area of a circle and you want to print the computed area with only two fractional digits. If you try the usual System.out.println method, it will print a quite big float number. As another example, say you want to separate the digits of a big number with separator such as a comma to print it to the end user. In such cases, you can use the C-style printf() (print formatted) method that was introduced in Java 5.
The method printf() uses string-formatting flags to format strings. It is quite similar to the printf() function provided in the library of the C programming language. The printf() method is provided as part of the PrintStream class. Here is its signature:
PrintStream printf(String format, Object... args)
The first parameter of the printf() method is a format string. A format string may contain string literals and format specifiers. The actual arguments are passed as the second arguments (args parameter here). This method can throw IllegalFormatException if the passed format is not correct.
Format specifiers are the crux of the string formatting concepts. They define the placeholder for a specific data type and its format (such as alignment and width). The remaining parameters of the printf() method are the variables (or literals) that provide the actual data to fill in the placeholders in sequence of the format specifiers.
Format Specifiers
Let’s investigate the template of format specifiers in the printf() method:
%[flags][width][.precision]datatype_specifier
As you can see, each format specifier starts with % sign followed by flags, width, and precision information and ends with data type specifier. In this string, the flags, width, and precision information is optional while the % sign and data type specifiers are mandatory.
Flags are single-character symbols that specify characteristics such as alignment and filling character. For instance, flag “-” specifies left alignment, “^” specifies center alignment, and “0” pads the number with leading zeroes.
The width specifier indicates the minimum number of characters that will span in the final formatted string.
If the input data is shorter than the specified width, then it is padded with spaces by default. In case the input data is bigger than the specified width, the full data appears in the output string without trimming.
218

Chapter 7 ■ String Processing
The precision field specifies the number of precision digits in output string. This optional field is particularly useful with floating point numbers.
Finally, the data type specifier indicates the type of expected input data. The field is a placeholder for the specified input data. Table 7-6 provides a list of commonly used data type specifiers.
Table 7-6. Commonly Used Data Type Specifiers
Symbol Description
%b |
Boolean |
%c |
Character |
%d |
Decimal integer (signed) |
%e |
Floating point number in scientific format |
%f |
Floating point numer in decimal format |
%g |
Floating point numer in decimal or scientific format (depending on the |
|
value passed as argument) |
%h |
Hashcode of the passed argument |
%n |
Line separator (new line character) |
%o |
Integer formatted as an octal value |
%s |
String |
%t |
Date/time |
%x |
Integer formatted as an hexadecimal value |
|
|
Let’s use the printf() method to format the output of the area() method of a Circle class. By default, the area() method calculates the area of the circle and prints using the System.out.println() method. However, you want to control the format of the output. More specifically, you want to print the area with only two precision points. Listing 7-14 shows how to achieve this.
Listing 7-14. Circle.java
// This program shows the usage of formatting string in printf() method class Circle {
private int x, y, radius;
public Circle(int x, int y, int radius) { this.x = x;
this.y = y; this.radius = radius;
}
void area() {
double tempArea = Math.PI * radius * radius;
System.out.println("Cirle area using default formatting with println: " + tempArea); System.out.printf("Circle area using format specifier with printf: %.2f", tempArea);
}
public static void main(String[] str) { Circle circle = new Circle(10,10,5); circle.area();
}
}
219
Chapter 7 ■ String Processing
This program produces following output:
Cirle area using default formatting with println: 78.53981633974483 Circle area using format specifier with printf: 78.54
Well, you can see that the first line contains the unformatted long output while the second line contains formatted output using formatting string in printf() method.
Let’s use more formatting options in an example. Suppose that you want to print a table of soccer players along with their names, played matches, scored goals, and goals per match information. However, there are a
few constraints:
•You want to print the name of players to the left (left aligned).
•You want to specify at least 15 characters for the name of the players.
•You want to print each column at a distance of a tab-stop.
•You want to specify only one precision point in goals per match info. Listing 7-15 shows how to implement this.
Listing 7-15. FormattedTable.java |
|
// This program demonstrates the use of format specifiers in printf |
|
class FormattedTable { |
|
static void line() { |
|
System.out. |
|
println("----------------------------------------------------------------- |
"); |
} |
|
static void printHeader() { |
|
System.out.printf("%-15s \t %s \t %s \t %s \n", |
|
"Player", "Matches", "Goals", "Goals per match"); |
|
|
|
}
static void printRow(String player, int matches, int goals) { System.out.printf("%-15s \t %5d \t\t %d \t\t %.1f \n",
player, matches, goals, ((float)goals/(float)matches));
}
public static void main(String[] str) { FormattedTable.line(); FormattedTable.printHeader(); FormattedTable.line(); FormattedTable.printRow("Demando", 100, 122); FormattedTable.printRow("Mushi", 80, 100); FormattedTable.printRow("Peale", 150, 180); FormattedTable.line();
}
}
220
Chapter 7 ■ String Processing
This program produces following output:
-----------------------------------------------------------------------------------
Player Matches Goals Goals per match
-----------------------------------------------------------------------------------
Demando |
100 |
122 |
1.2 |
Mushi |
80 |
100 |
1.3 |
Peale |
150 |
180 |
1.2 |
-----------------------------------------------------------------------------------
Let’s analyze the format string specified in the printRow() method. The first part of the format string is "%-15s". Here, the expression starts with %, which indicates the start of a format-specifier string. The next symbol is '-', which is used to make the string left aligned. The number "15" specifies the width of the string and finally data type specifier of "s" indicates the input data type as String. The next format specifier string is "%5d", which signifies it expects
an integer that will be displayed in the minimum 5 digits. The last format specifier string is "%.1f", which expects a floating point number that will be displayed with one precision digit. All format specifier strings are separated with one or more "\t"s (tab stops) to make space between the columns.
Points to Remember
Here are some points that might prove useful on your OCPJP exam:
•If you do not specify any string formatting specifier, the printf() method will not print anything from the given arguments!
•Flags such as "-", "^", or "0" make sense only when you specify width with the format specifier string.
•You can also print the % character in a format string; however, you need to use an escape sequence for it. In format specifier strings, % is an escape character—which means you need to use %% to print a single %.
•If you do not provide the intended input data type as expected by the format string, then you can get an IllegalFormatConversionException. For instance, if you provide a string instead of an expected integer in your printRow() method implementation, you will get following exception:
Exception in thread "main" java.util.IllegalFormatConversionException:
d != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion (Unknown Source)
•If you want to form a string and use it later rather than just printing it using the printf() method, you can use a static method in the String class—format(). We have reimplemented the printRow() method used in the last example using the format() method, as shown:
void printRow(String player, int matches, int goals){
String str = String.format("%-15s \t %5d \t\t %d \t\t %.1f \n", player, matches, goals, ((float)goals/(float)matches));
System.out.print(str);
}
221

Chapter 7 ■ String Processing
Question time!
1.Consider this program, which intends to print the word delimited by the characters ‘*’ in its both ends:
public class SearchString {
public static void main(String[] args) {
String quote = "An *onion* a day keeps everyone away!"; // match the word delimited by *'s
int startDelimit = quote.indexOf('*'); int endDelimit = quote.lastIndexOf("*");
System.out.println(quote.substring(startDelimit, endDelimit));
}
}
This program will print one of the following options:
A.*onion
B.*onion*
C.onion
D.*onio
E.nion*
Answer: A. *onion
(The substring (beginIndex, endIndex) consists of characters beginning from the character at beginIndex till the character at the endIndex – 1.)
2.Predict the outcome of the following program:
public class ParseString1 {
public static void main(String[] s) {
String quote = "Never lend books-nobody ever returns them!"; String [] words = quote.split(" ", 2);
// split strings based on the delimiter " " (space) for (String word : words) {
System.out.println(word);
}
}
}
A.It will result in a compile-time error.
B.It will result in a runtime exception.
C.It will print the following output when executed Never
lend
222
Chapter 7 ■ String Processing
D.It will print the following output when executed Never
lend books-nobody ever returns them!
Answer: D. It will print the following output when executed Never
Lend books-nobody ever returns them!
(The second parameter of the split() method specifies the total number of strings that the split() method needs to generate. In the last string (here, in the second string), the split() method puts the remaining part of the string.)
3.Listing 7-11 used the regex "\w+@\w+\.com" to match e-mail addresses in a string where the e-mail addresses end with a “.com” domain. Can you replace the regex with the following: "\w+@\w+.com"?
A.Yes, it will work perfectly and will only match valid e-mail addresses.
B.No, it will result in a compile-time error.
C.No, it will result in a runtime exception.
D.No, it will produce wrong matches and will match invalid e-mail addresses. Answer: D. No, it will produce wrong matches and will match invalid e-mail addresses.
(It will produce wrong matches. For instance, it will match to "abc@xyz!com" also, since "." works here as a single character wildcard, which could match any character.)
4.Which of the following regular expressions is the correct expression for matching a mobile number stored in following format: +YY-XXXXXXXXXX (YY is the country code, the rest of the number is a mobile number)?
A.“\+\d{2}-\d{10}”
B.“\b\+\d{2}-\d{10}\b”
C.“+\d{2}-\d{10}”
D.“\b+\d{2}-\d{10}\b”
Answer: A. “\+\d{2}-\d{10}”
(You need to provide a backslash as an escape character for “+”. Another important point is that you cannot use “\b” in starting and ending if the first or last character of the string is not a word character.)
5.You want to write a regex to match an e-mail address. The e-mail address should not start with a digit and should end with “.com”. Which one of the following regex will satisfy this requirement?
A.“\b\w+@\w+\.com\b”
B.“\b\D\w*@\w+\.com\b”
C.“\b\D\w+@\w+\.com\b”
D.None of the above
223