Great book written by Kathy Sierra and Bert Bates.
I wanted to share few important quotations found from the six chapter.
1) Once you have assigned a String a value, that value can never change.
2) When we make a new instance of the class File, we're not yet making an actual file, we're just creating a filename.
Ex:
// Does not create a new file but only creating a filename.
File file = new File("fileWrite1.txt");
To create a file we need to create it in below way
newFile = file.createNewFile();
3) Basic serialization happens with just two methods: one to serialize objects and write them to a stream, and a second to read the stream and deserialize objects.
ObjectOutputStream.writeObject() // serialize and write
ObjectInputStream.readObject() // read and deserialize
Ex:
import java.io.*;
class Cat implements Serializable {
String name;
Cat(String name) {
this.name=name;
}
public String getName() {
return "Returning name" + name;
}
}
public class SerializationDemo {
public static void main(String[] args) {
Cat c = new Cat("Mimom"); // 2
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(c); // 3
os.close();
} catch (Exception e) { e.printStackTrace (); }
try {
FileInputStream fis = new FileInputStream("testSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (Cat) ois.readObject(); // 4
System.out.println(c.getName());
ois.close();
} catch (Exception e) { e.printStackTrace(); }
}
}
4) Serializable is a marker interface; it has no methods to implement.
5) When you serialize an object, Java serialization takes care of saving that object's entire "object graph." That means a deep copy of everything the saved object needs to be restored.
6) transient modifier will skip serialization of the respective object.
7) When we invoke defaultWriteobject() from within writeObject() we're telling the JVM to do the normal serialization process for this object. When implementing writeObject(), we will typically request the normal serialization process, and do some custom writing and reading too.
8) When it's time to deserialize, defaultReadobject() handles the normal deserialization we'd get if we didn't implement a readobject() method.
9) When an instance of a Serializable class is deserialized, the constructor does not run, and instance variables arc NOT given their initially assigned values! If the constructor were invoked, and/or instance variables were assigned the values given in their declarations, the object we're trying to restore would revert back to its original state, rather than coming back reflecting the changes in its state that happened sometime after it was created.
10) Object references marked transient will always be reset to null, regardless of whether they were initialized at the time of declaration in the class.
11) When superclass is NOT serializable, then any instance variables we INHERIT from that superclass will be reset to the values they were given during the original construction of the object. This is because the non-serializable class constructor WILL run!
Ex:
import java.io.*;
class SuperNotSerial {
public static void main(String [] args) {
Dog d = new Dog(35, "Fido");
System.out.println("before: " + d.name + " "
+ d.weight);
try {
FileOutputStream fs = new FileOutputStream("testSer.ser"};
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(d);
os.close ();
} catch (Exception e) { e.printStackTrace(); }
try {
FileInputStream fis = new FileInputstream("testSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Dog) ois.readObject();
ois.close();
} catch (Exception e) { e.printStackTrace(); }
System.out.println("after: " + d.name + " "
+ d.weight);
}
}
class Dog extends Animal implements Serializable {
String name;
Dog(int w, String n) {
weight = w; // inherited
name = n; // not inherited
}
}
class Animal { // not serializable !
int weight = 42;
}
which produces the output:
before: Fido 35
after: Fido 42
12) If you serialize a collection or an array, every element must be serializable! A single non-serializable element will cause serialization to fail.
13) Static variables are NEVER saved as part of the object's state…because they do not belong to the object!
14) As simple as serialization code is to write, versioning problems can occur in the real world. If you save a Dog object using one version of the class, but attempt to deserialize it using a newer, different version of the class, deserialization might fail
15) The parse() method in DateFormat class takes a String formatted in the style of the DateFormat instance being used, and converts the String into a Date object.
16) Regular expressions (regex for short) are a kind of language within a language, designed to help programmers with these searching tasks.
Simple Searches
For our first example, we'd like to search through the following source String
abaaaba
for all occurrences (or matches) of the expression
ab
We can see that we have two occurrences of the expression ab: one starting at position 0 and the second starting at position 4.
import java.util.regex.*;
class RegexSmall {
public static void main(String[] args) {
Pattern p = Pattern.compile("ab"); // the expression
Matcher m = p.matcher("abaaaba"); // the source
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + " ");
}
}
}
This produces
0 4
17) Regex provides a rich set of metacharacters for java.util.regex.Pattern.
| \d | A digit |
| \s | A whitespace character |
| \w | A word character (letters, digits, or "_" (underscore)) |
We can also specify sets of characters to search for using square brackets and ranges of characters to search for using square brackets and a dash:
[abc] Searches only for a's, b's or c's
In addition, we can search across several ranges at once. The following expression is looking for occurrences of the letters a - f or A - F, it's NOT looking for an fA combination:
[a-fA-F] Searches for the first six letters of the alphabet, both cases.
18) ? (zero or one) quantifier : Let's say that our job this time is to search a text file and find anything that might be a local, 7-digit phone number. We're going to say, arbitrarily, that if we find either seven digits in a row, or three digits followed by a dash or a space followed by 4 digits, that we have a candidate. Here are examples of "valid" phone numbers:
1234567
123 4567
123–4567
The key to creating this expression is to see that we need "zero or one instance of either a space or a dash" in the middle of our digits:
\d\d\d([-\s])?\d\d\d\d
19) The Predefined Dot
When you see this character in a regex expression, it means "any character can serve here." For instance, the following source and pattern
source: "ac abc a c"
pattern: a.c
will produce the output
3 abc
7 a c
The "." was able to match both the "b" and the " " in the source data.
20) The greedy quantifier does in fact read the entire source data, and then it works backwards (from the right) until it finds the rightmost match. At that point, it includes everything from earlier in the source data up to and including the data that is part of the rightmost match.
? is greedy, ?? is reluctant, for zero or once
* is greedy, *? is reluctant, for zero or more
+ is greedy, +? is reluctant, for one or more
Example:
What happens when we have the following source and pattern?
source: yyxxxyxx
pattern: .*xx
Result
0 yyxxxyxx
The way to think about this is to consider the name greedy. The regex engine looks (greedily) at the entire source data before it could determine that there was an xx at the end.
If we change the pattern to
source: yyxxxyxx
pattern: .*?xx
we're now using the reluctant qualifier *?, and we get the following:
0 yyxx
4 xyxx
3 comments:
Prasanth,
I've a question in general to blogging. How do you post code snippet which has varying fonts?
Thank you.
Kandula,
Please use this site
http://www.palfrader.org/code2html/
http://www.lv-designer-handbags.net/ Louis Vuitton Designer
Post a Comment