Monday, April 30, 2007

Java: Processes and Threads

The Java Tutorials > Essential Classes > Concurrency > Processes and Threads

Sanghera's book confused me so I had to review the differences between processes and threads!

A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.

...

Threads are sometimes called lightweight processes. Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.

Labels: , ,

Java: bounded wildcards

The Java Tutorials > Bonus > Generics > Wildcards

public void drawAll(List shapes) {
...
}

There is a small but very important difference here: we have replaced the type List with List. Now drawAll() will accept lists of any subclass of Shape, so we can now call it on a List if we want.

List is an example of a bounded wildcard. The ? stands for an unknown type, just like the wildcards we saw earlier. However, in this case, we know that this unknown type is in fact a subtype of Shape. (Note: It could be Shape itself, or some subclass; it need not literally extend Shape.) We say that Shape is the upper bound of the wildcard.

There is, as usual, a price to be paid for the flexibility of using wildcards. That price is that it is now illegal to write into shapes in the body of the method. For instance, this is not allowed:

public void addRectangle(List shapes) {
shapes.add(0, new Rectangle()); // Compile-time error!
}

You should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape-- an unknown subtype of Shape. Since we don't know what type it is, we don't know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn't safe to pass a Rectangle there.


On the other hand, if you specify the upper bound of the wildcard, it's okay to add items to the List shapes.

Labels: ,

Java: regular expressions

The Java Tutorials: Essential Classes > Regular Expressions > Quantifiers

Greedy Meaning
X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
X{n} X, exactly n times

Need to memorize these.

Labels: , ,

combining blogs

I was thinking of migrating this blog into my tokyo kuri blog, now that we have tags available. But given my last posts, it'd be a little embarrassing to force this content onto general readers. It's a topic of limited interest, after all, if you don't work with this stuff.

I will be copying my posts from my work sns over to here, though.

Labels:

Java: formatters

I'll need to revisit this topic as I only have superficial comprehension of Java Formatters like NumberFormat.

Currency objects are distinct from locales because a particular locale may use multiple currencies.

Labels: , , ,

Java: super keyword

http://java.sun.com/docs/books/tutorial/java/IandI/super.html

Invocation of a superclass constructor must be the first line in the subclass constructor.

The syntax for calling a superclass constructor is

super();
--or--
super(parameter list);

Labels:

Friday, April 27, 2007

update on Sanghera's SCJP Exam for J2SE 5

I've started rereading the book, and I definitely don't recommend it for preparing for the SCJP exam for J2SE 5.

There are so many errors! Significant errors, like what exception FileReader and FileWriter throw from their read/write methods - there's a table aggregating the I/O exceptions for various Stream classes and their read/write methods, and it's wrong. Kind of annoying. Keeps me sharp, I supposed, but the Sun book has to be better.

Also, the lack of formatting of code snippets is really annoying; it's hard to read.

I'll post a list of the errata later.

For now:
p.209
* "public FileReader(...) throws IOException"
should be "throws FileNotFoundException"

p.210
* "public FileWriter(...) throws IOException"
should be "throws FileNotFoundException"

p.214
* Table 8-6 FileReader, FileWriter exception thrown by Read/Write Methods should be IOException.

p.214
* It alternatively refers to "a string and two objects are written to the stream" and "the String and the Date Objects that were written to the file," when the first code snippet writes two Strings and one Object.

* bottom code snippet "(MyClassSerial)" should be "(MySerialClass)" on the 4th and 5th lines.

Labels: ,