Pages

Aug 13, 2012

String Handling



Using String, StringBuffer, and StringBuilder (Objective 3.1)
  1. String objects are immutable, and String reference variables are not.
  2. If you create a new String without assigning it, it will be lost to your program.
  3. If you redirect a String reference to a new String, the old String can be lost.
  4. String methods use zero-based indexes, except for the second argument of substring().
  5. The String class is final¡ÂȘits methods can't be overridden.
  6. When the JVM finds a String literal, it is added to the String literal pool.
  7. Strings have a method: length(); arrays have an attribute named length.
  8. The StringBuffer's API is the same as the new StringBuilder's API, except that StringBuilder's methods are not synchronized for thread safety.
  9. StringBuilder methods should run faster than StringBuffer methods.
  10. All of the following bullets apply to both StringBuffer and StringBuilder:
    1. They are mutable - they can change without creating a new object.
    2. StringBuffer methods act on the invoking object, and objects can change without an explicit assignment in the statement.
    3. StringBuffer equals() is not overridden; it doesn't compare values.
  11. Remember that chained methods are evaluated from left to right.
  12. String methods to remember: charAt(), concat(), equalsIgnoreCase(), length(), replace(), substring(), toLowerCase(), toString(), toUpperCase(), and trim().
  13. StringBuffer methods to remember: append(), delete(), insert(), reverse(), and toString().


File I/O (Objective 3.2)
  1. The classes you need to understand in java.io are File, FileReader, BufferedReader, FileWriter, BufferedWriter, PrintWriter, and Console.
  2. A new File object doesn't mean there's a new file on your hard drive.
  3. File objects can represent either a file or a directory.
  4. The File class lets you manage (add, rename, and delete) files and directories.
  5. The methods createNewFile() and mkdir() add entries to your file system.
  6. FileWriter and FileReader are low-level I/O classesYou can use them to write and read files, but they should usually be wrapped.
  7. Classes in java.io are designed to be "chained" or "wrapped." (This is a common use of the decorator design pattern.)
  8. It's very common to "wrap" a BufferedReader around a FileReader or a BufferedWriter around a FileWriter, to get access to higher-level (more convenient) methods.
  9. PrintWriters can be used to wrap other Writers, but as of Java 5 they can be built directly from Files or Strings.
  10. Java 5 PrintWriters have new append(), format(), and printf() methods.
  11. Console objects can read non-echoed input and are instantiated using System.console().


Serialization (Objective 3.3)
  1. The classes you need to understand are all in thejava.io package; they include: ObjectOutputStream and ObjectInputStream primarily, and FileOutputStream and FileInputStream because you will use them to create the low-level streams that the ObjectXxxStream classes will use.
  2. A class must implement Serializable before its objects can be serialized.
  3. The ObjectOutputStream.writeObject() method serializes objects, and the ObjectInputStream.readObject() method deserializes objects.
  4. If you mark an instance variable transient, it will not be serialized even thought the rest of the object's state will be.
  5. You can supplement a class's automatic serialization process by implementing the writeObject() and readObject() methodsIf you do this, embedding calls to defaultWriteObject() and defaultReadObject(),respectively, will handle the part of serialization that happens normally.
  6. If a superclass implements Serializable, then its subclasses do automatically.
  7. If a superclass doesn't implement Serializable, then when a subclass object is deserialized, the superclass constructor will be invoked, along with its superconstructor(s).
  8. DataInputStream and DataOutputStream aren't actually on the exam, inspite of what the Sun objectives say.


Dates, Numbers, and Currency (Objective 3.4)
  1. The classes you need to understand are java.util.Date, java.util.Calendar, java.text.DateFormat, java.text.NumberFormat, and java.util.Locale.
  2. Most of the Date class's methods have been deprecated.
  3. A Date is stored as a long, the number of milliseconds since January 1, 1970.
  4. Date objects are go-betweens the Calendar and Locale classes.
  5. The Calendar provides a powerful set of methods to manipulate dates, performing tasks such as getting days of the week, or adding some number of months or years (or other increments) to a date.
  6. Create Calendar instances using static factory methods (getInstance()).
  7. The Calendar methods you should understand are add(), which allows you to add or subtract various pieces (minutes, days, years, and so on)  of dates, and roll(), which works like add() but doesn't increment a date's bigger pieces(For example: adding 10 months to an October date changes the month to August, but doesn't increment the Calendar's year value.)
  8. DateFormat instances are created using static factory methods (getInstance() and getDateInstance()).
  9. There are several format "styles" available in the DateFormat class.
  10. DateFormat styles can be applied against various Locales to create a wide array of outputs for any given date.
  11. The DateFormat.format() method is used to create Strings containing properly formatted dates.
  12. The Locale class is used in conjunction with DateFormat and NumberFormat.
  13. Both DateFormat and NumberFormat objects can be constructed with a specific, immutable Locale.
  14. For the exam you should understand creating Locales using language, or a combination of language and country.

Parsing, Tokenizing, and Formatting (Objective 3.5)
  1. regex is short for regular expressions, which are the patterns used to search for data within large data sources.
  2. regex is a sub-language that exists in Java and other languages (such as Perl).
  3. regex lets you to create search patterns using literal characters or metacharactersMetacharacters allow you to search for slightly more abstract data like "digits" or "whitespace".
  4. Study the \d, \s, \w, and metacharacters
  5. regex provides for quantifiers which allow you to specify concepts like: "look for one or more digits in a row."
  6. Study the ?, *, and + greedy quantifiers.
  7. Remember that metacharacters and Strings don't mix well unless you remember to "escape" them properlyFor instance String s = "\\d";
  8. The Pattern and Matcher classes have Java's most powerful regex capabilities.
  9. You should understand the Pattern compile() method and the Matcher matches(), pattern(), find(), start(), and group() methods.
  10. You WON'T need to understand Matcher's replacement-oriented methods.
  11. You can use java.util.Scanner to do simple regex searches, but it is primarily intended for tokenizing.
  12. Tokenizing is the process of splitting delimited data into small pieces.
  13. In tokenizing, the data you want is called tokens, and the strings that separate the tokens are called delimiters.
  14. Tokenizing can be done with the Scanner class, or with String.split().
  15. Delimiters are single characters like commas, or complex regex expressions.
  16. The Scanner class allows you to tokenize data from within a loop, which allows you to stop whenever you want to.
  17. The Scanner class allows you to tokenize Strings or streams or files.
  18. The String.split() method tokenizes the entire source data all at once, so large amounts of data can be quite slow to process.
  19. New to Java 5 are two methods used to format data for output. These methods are format() and printf()These methods are found in the PrintStream class, an instance of which is the out in System.out.
  20. The format() and printf() methods have identical functionality.
  21. Formatting data with printf() (or format()) is accomplished using formatting strings that are associated with primitive or string arguments.
  22. The format() method allows you to mix literals in with your format strings.
  23. The format string values you should know are
    1. Flags: -, +, 0, "," , and (
    2. Conversions: b, c, d, f, and s
  24. If your conversion character doesn't match your argument type, an exception will be thrown.

regards,
TechDexters Support Team
Hyderabad.


for Feedback and Support simply drop a mail in
techdexters{at}gmail.com
Remember to replace the {at} with @ in the email addresses

No comments:

Post a Comment