Java control flow exercises

Posted by Jack Hsu Fri, 18 Sep 2009 13:42:00 GMT

I came upon an interesting blog post about Java control flow. The author listed six examples (from LASER 2009 summer school on Software Testing) of pseudo Java code, and you have to determine what the value of the int b is, as well as whether an Exception is thrown or not. You can assume the program will execute with proper syntax, and proper throws Exception on the method signature.

 Here are my answers, and explanations. The actual answers will be posted in the author’s next blog entry.

1. What is the value of b at the end of this code:

foo () {
    int b = 1;
    b++;
}

My answer: b is 2

2. What is the value of b at the end of this code:

foo () {
    int b = 1;
    while (true) {
        b++;
        break;
    }
}

My answer: b is 2 because it gets incremented one first iteration of the while loop, then the loop breaks.

3. What is the value of b at the end of this code and tell us if this executes normally or with an exception:

foo () {
    int b = 1;
    try {
        throw new Exception();
    }
    finally {
        b++;
    }
}

My answer: The exception is thrown, and then b is incremented to 2. And since the exception in not caught, it is thrown from the method.

4. What is the value of b at the end of this code and tell us if this executes normally or with an exception:

foo () {
    int b = 1;
    while (true) {
        try {
            b++;
            throw new Exception();
        }
        finally {
            b++;
            break;
        }
    }
    b++;
}

My answer: b is incremented in the try block, then exception is thrown. In the finally block, b is incremented again, then a static return from break brings control flow to the end of the method where b is once again incremented, and no exception is thrown from the method. So, b is 4 at the end, and no exception is thrown… in fact, you don’t even need a throws Exception in the method signature because this method cannot throw Exception.

5. The previous sample was too long, what does this return:

int foo () {
    try {
        return 1;
    }
    finally {
        return 2;
    }
}

My answer: the return 2 is the last statement executed in the method, so it returns 2.

6. If you have answered properly to the above questions, then you are definitely quite an expert. To prove it, tell us the value of b at the end of the call as well as the return value:

int b;
int foo () {
    b = 0;
    try {
        b++;
        return b;
    }
    finally {
        b++;
    }
}

My answer: b is incremented in the try block, then returned; return value is 1 from the method. Then the finally block executes and increments b, so it is 2 at the end (but return value is still 1).

I think that made sense. Nes pas?

Unix history and bang commands

Posted by Jack Hsu Wed, 16 Sep 2009 16:07:00 GMT

The Unix bash shell provides many useful tools, one of them being then history command.

To see a list of previously executed commands, execute history into your shell. You can also supply an integer argument to limit the number of commands to show.

history 10

This can be piped into other commands, which can be useful for many purposes. For example, if I just finished a series of steps to get something working and wish to write a how-to entry for it, I could execute this:

history | cut -d ' ' -f 5- # cut out only the commands

The bash shell also provides the bang (!) command to execute the last command matching the provided command name.

$ echo foo
foo
$ !echo
echo foo
foo

Or you can use bang-bang (!!) to execute the last command

$ echo bar
bar
$ !!
echo bar
bar

This is useful when you get a permission denied error because you forgot to sudo.

$ Make me a sandwich.
What? Make it yourself.
$ sudo !!
Okay.

You can also print the matching command without executing it via the :p modifier. This will also copy that matching command to the end of your history.

$ echo faz
faz
$ !!:p
echo faz
$ !!
echo faz
faz

To substitute certain parts of a command, you can use the :s modified.

$ echo baz
baz
$ !!:s/baz/Hello, World/

You can combine both modifiers as well, to preview a command before executing… might be a good idea if the command may cause damage.

Retrieving only arguments from a command can be done using the :$ :^, :n, and :* modifiers. The $ and ^ will look familiar to anyone who knows regex, they match the last and first arguments respectively. The :n modifier will match argument at position n (first argument being :1), and :* retrieves all arguments.

$ mkdir foobar && cd foobar # make temp dir
$ touch file1 file2 file3 file23 file20 # dummy files
$ ls
file1 file2 file3 file23 file20
$ ls file2* # list files match pattern
file2   file20  file23
$ rm !!:* # remove previously matched files
rm file2*$ ls
file1   file3

You can also use the :0 modifier to match the command name, but I don’t really see the use in that.

One last thing about history. You can start an interactive search through history by press Ctrl+r (^R), which will interactively display commands matching what you are typing (in reverse history order).

Sub-pixel Art and Typeface

Posted by Jack Hsu Tue, 15 Sep 2009 20:07:00 GMT

I never knew favicons require such care and dedication to make them more appealing to our eyes.

This post on miha’s blog shows how YouTube’s favicon can be improved by using sub-pixel rendering. There are also improvements made to a few other favicons.

Then there’s this post by the same author that shows a work-in-progress subpixel typeface.

Sub-pixel rendering is a technique used to increase the apparent resolution of a LCD display. Pretty neat!

ReCSS Bookmarklet

Posted by Jack Hsu Fri, 11 Sep 2009 14:04:00 GMT

I’ve been using this ReCSS bookmarklet for a long time now… it’s one of the best tools I’ve found on the web. You drag-and-drop the bookmarket to your bookmarks toolbar, and when you need to force-reload the CSS on a page, just click on it and viola! No need to reload the entire page to see updated stylesheets.

Originally found on http://www.dojotoolkit.org/~david/recss.html.