<< September 2008 | Home | November 2008 >>

Friday Java Quiz: Now For A Real Test

Less than an hour after I posted last weeks Friday Java Non-quiz, Steve walked in, wrote some code on the white board, and said: "This should be on your Java quiz."

Here's a simplified version of his class:

1    public class Foo { 
2        public static void main(String[] args) { 
3            Foo foo = new Foo(); 
4            foo.printIt(foo.getIt()); 
5        } 
6     
7        @SuppressWarnings("unchecked") 
8        private <T> T getIt() { 
9            return (T) "hello, world."; 
10       } 
11    
12       private void printIt(String s) { 
13           System.out.println(s); 
14       } 
15   }

The question remains the same:

Q: Compile, or no compile?

Strict rules apply: No actually running the compiler. Must back answer with JSL citation.

Tags :

This Applet Not Brought To You By...

... a buggy JavaScript file.

([Note] The following would have been a charming little blog entry with a small Java applet that warms people's hearts, had I not ran into some troubles that made the whole thing, eh, what shall I say, a little annoying.)

Along with Java 6 Update 10, released 14 days ago, comes a JavaScript file deployJava.js hosted at http://java.sun.com, that, among other things, helps in deploying Java applets.

Here's a little applet, deployed according to this advice:

(Source of a would-be applet follows)

<div style="margin-left:3em">
<script src="http://java.com/js/deployJava.js"></script>
<script>
    var attributes =
        {id:'squareApplet',
        codebase:'http://www.weiqigao.com/blog/files',
        code:'com.weiqigao.square.SquareApplet.class',
        archive:'square.jar',
        width: 100, height:100};
    var parameters = {color: 'red'};
    var version = '1.6' ;
    deployJava.runApplet(attributes, parameters, version);
</script></applet>
</div>

<script>
  function toBlue() {
    document.squareApplet.toBlue();
  }

  function toRed() {
    document.squareApplet.toRed();
  }
</script>

<p>And you can change its color to <a href=  "javascript:toBlue()"
>blue</a> and <a href= "javascript:toRed()" >back</a>.</p>

Here's the issues I had with this setup:

  • I don't have Java plug-in on my 64-bit Debian GNU/Linux machine. This causes the above script to auto-redirect to Java Downloads for Linux. But it forgot one thing: there isn't a Java plug-in for 64-bit Linux to download. In the mean time, I can't even go back to the blog entry to edit it.
  • So I switched to the Windows machine to finish the editing. For some reason, the tags output by the JavaScript is missing the closing applet tag, which, in Firefox or IE7 causes the Save button to not show up.
  • Luckily, Google Chrome was more tolerant, and I was able to resume my editing. I added the closing </applet> tag after the JavaScript code to make it work in Firefox 3 and IE7.
  • One thing I want to experiment with is the revived LiveConnect. But apparently it works only if the applet extends java.applet.Applet. I extended javax.swing.JApplet initially, thinking that JApplet is applet too. But none of the Java methods on it was callable. As soon as I changed to java.applet.Applet, Java method calls from JavaScript worked.
  • They worked in Firefox 3 and IE7, but not Google Chrome nor Safari. I guess such is web programming.
  • After all these fuss and trials and errors, once things worked as documented, it did feel fast and snappy. The start up delay is not longer than that of a Flash animation or even AJAX controls. Sun really did eliminate the Java/Applet start up disadvantage.
  • As I modified the applet and jarred it up and uploaded it to the server, I have to fight the JRE cache on the browser side to load the new version of the jar file. At one point, I removed the jar file from the server, restarted Tomcat, yet the browser still had the applet running, even after clicking on the reload button multiple times while the shift or control key was held down.

For the curious, here is the happy version of this blog. (Beware of Applet! You've been warned.)

Tags :

The C++0x Working Draft: Get It Now

Herb Sutter: ... Here’s where to find it: The September C++0x working draft document is essentially the same as the September 2008 CD.

This is “it”, feature-complete C++0x, including the major feature of “concepts”...

Here's a taste of what's in it:

Working Draft, Standard for Programming Language C++:

8.3.2 References

  1. A reference type that is declared using & is called an lvalue reference, and a reference type that is declared using && is called an rvalue reference...
Tags :

Friday Java Non-Quiz: Sharing An Obscure IDE Trick Day

If you couldn't tell from last weeks questions that I'm running out of legitimate Java quiz questions, I don't know how loudly I have to say it.

Let's face it, Java is a pretty low-surprise language. Tedious maybe, but never mysterious. The contrast with C++ is astounding. I have to debug some JNI code yesterday, and when I got to the C++ part, I do indeed have to stare at lines like

int a = 0;
foo(a);
if (a == 1) bar();

and go, "That's impossible!"

Anyway, I did learn a new IntelliJ IDEA trick recently from the IDEA Refcardz. Thanks to CodeToJoy for alerting me to their existence.

It's Ctrl-Shift-UpArrow and Ctrl-Shift-DownArrow. What it does is to move things up or down within scope:

  • If the cursor is on a statement, it moves the statement relative to adjacent statements. The movement stops when the statement hits an opening or a closing brace of a if statement, while statement or a method definition.
  • If the cursor is on the name of a method or a field, it moves the method or field up or down relative to other methods or fields within its defining class.

What other IDE tricks have you learned recently? Can you do what I just described in your favorite IDE, Java or other languages?

Something's Going On In The XML World

Something is going on in the XML world, where as far as I can feel and tell, things have been working "just fine" for the past 20(?) years.

That's why I can't believe what some people (in the W3C) were trying to do:

David Carlisle: ... This sets a really terrible precedence, and sadly puts XML into a similar state as HTML, where the specification will be widely ignored (as it will be inconsistent) and people will have no choice other than just to test against a collection of major implementations and do whatever they do.
James Clark: There is a real problem here, and it's partly my fault. If we had had a bit more foresight ten years ago, we would have made the 1st edition of XML 1.0 say what is now being proposed for the 5th Edition. I know that the XML Core WG are trying to do the right thing, but I really don't think this is a good idea.

Sound like another case of old timers saying: "No, no, leave it alone. You don't know what kind of a mess your are getting us into."

Tags :

Friday Java Quiz: Trivia Time

  • Q: How many methods does java.lang.Object have? How many of them are native?
  • Q: How many interfaces does the java.lang package contain? How many of them are marker interfaces?
  • Q: How many immediate subpackages are there under java? How many of them can be safely thrown away?
  • Q: Have you ever used the class java.util.Scanner in production code? (I know you blogged about it. That doesn't count.)

Rules: You won't need the compiler to answer these questions. For the first three questions, write down your guess before you check the API documentation.

Tags :

The Next Big Thing: An Opinionated Google Search

(Tangential thoughts triggered by techno.blog("Dion").)

In no particular order:

  • The JVM has become the playground of the programming language researchers. This is a good thing for the Java community, even for those of us who mainly uses the Java language on a day-to-day basis. It's nice to know that the language that I write CRUD applications is also capable of being so creatively used.
  • I like object-oriented programming. I also like functional programming. I'm not allergic to LISP's nested parenthesis. I've been looking at Clojure for the last year, and believe it has the potential to becoming the next big language.
  • Functional programming survived all these years because of one thing and one thing alone: It's mathematical—Its fundation can be presented as axioms, definitions and theorems. Object-orientation, on the other hand, feels ad hoc. It may map well to how some people view (the object/attribute/governing-rules view in the Aristotle tradition) the world. But psychological studies has shown that not all people (the community/relationship/harmony view in the Confucian tradition, for example) perceive the world that way.
  • Although I'm not certain about what the next big language is, I know what the next big thing is. It has to be something for the Internet. It has to be something that beats Google at searching results. Google is good at relatively simple and static things, like "From which direction does the Sun rise?", but terrible at dynamically changing things, like "Where is the Social Security Office at Creve Coeur, MO?" (They used to be on the first floor in the building OCI is in. They moved a couple of years ago. But we still see people coming to the building to inquire.) I know it's not going to be easy, but what if Google start to get some opinions and display a "We think this is good" or "We think this is wrong" marker alongside every search result. Sort of like fact checking for every web page.
  • JavaScript is the lousiest language ever invented. It is unfortunate that we are stuck with it for now (the web-era). It needs to grow. Some people want to make the browser a platform, fine. That platform needs to grow. In growth, it will see fragmentation ("which JavaScript framework library are you using today?"), consolidation and evolution. At the end (we are talking about five to ten years out) it will be just like one of the other platforms that we use: Windows, Mac, Linux, etc.
  • Ruby and Scala are both languages with weird syntaxes, just like Clojure is. But the mark of a truly useful language is that people will learn it in order to get things done. People seems to forget how hard object-orientation is when they first learned it. The same thing can be said of JavaScript.
  • Tomorrow's big language is today's obscure or nonexistent language that someone finds powerful enough, innovative enough and pleasant enough to do interesting things with. Have you solved a real problem lately? Have you enjoyed your programming work lately?

Java 6 Update 10 Released?

Java 6 Update 10 FCS seems to be here.

I'm sure the official announcement is somewhere.

[Update] (Wed Oct 22 08:10:50 CDT 2008) Here's the press release.

Tags :

Friday Java Quiz: It Can't Go Simpler Than This

Q: What will this program print? true? or false?

import java.io.*;

public class Foo {
  public static void main(String[] args) {
    Serializable bar = null;
    System.out.println(bar instanceof Serializable);
  }
}
Tags :

Git At Home

Here's a little experiment that I did last night after Kyle's Git talk—I put this blog under Git control.

I have been maintaining the content of this blog in Subversion since March 2005, 1292 days ago (and in CVS before that). When I moved this blog from my Fedora Core 6 box to my Debian box 64 days ago, I don't have enough disk space on the partition to hold the 1.9G Subversion workspace. So I simply used the 916M export from Subversion.

One of the questions I asked yesterday was the disk space usage of Git. And Kyle assured me that its much less than Subversion. So I gave Git a try:

[weiqi@gao] # feta install git-core
Blah Blah Blah
[weiqi@gao] # df -h /var
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/gao--2006-var
                      2.9G  2.0G  732M  74% /var
[weiqi@gao] # git --version
git version 1.4.4.4
[weiqi@gao] # git init-db
defaulting to local storage area
[weiqi@gao] # echo *.bak > .gitignore
[weiqi@gao] # git add .
Blah Blah Blah
Blah Blah Blah
...
[weiqi@gao] # df -h /var
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/gao--2006-var
                      2.9G  2.2G  550M  80% /var
[weiqi@gao] # git commit
Committing initial tree 9c31562f7a84c42ebab872d245a49b637b1e0395
[weiqi@gao] # df -h /var
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/gao--2006-var
                      2.9G  2.2G  546M  81% /var
[weiqi@gao] # du -sh .git
187M    .git

So everything fits in there nicely. What would have been a 1.9G Subversion working directory now fits into a 1.2G space. (This is a bit of an unfair comparison because I did not convert my Subversion repository to Git, so I don't have much history in Git, yet.) And yes, Git is blazingly fast.

I'll just have to push it to another box from time to time.

And here's how the repo looks in gitk, a Tk GUI that Kyle demoed yesterday:

Git initial import, shown in gitk

Git At The JUG

Today is JUG day.

The JUG did a Git talk.

Kyle Cordes gave the talk.

Kyle gave the talk without PowerPoint.

Kyle demoes Git on Windows.

A Gtk+ GUI Git tool did Kyle use on Windows.

The tool showed trees of his projects: branches and merges and stashes

Git is fast, said Kyle.

Git is the distributed version control tool to choose nowadays, said Kyle.

$ git init
Blah Blah Blah
$ git add .
Blah Blah Blah
$ git commit
Vi opened up for commit comments

GitHub is a host of Git stuff, free for open source, pay for non-open source.

No Eclipse plugin yet. Have to press F5.

Git can detect file movements within the workspace, based on similarity.

Git does not do expanded keywords.

Kyle brought a little thumb drive that contained the Linux kernel repository.

The actual repo is in three files, the largest is about 200MB.

The whole Git workspace (with the repo) is usually smaller than the Subversion workspace (with only the latest version).

We had a reasonable crowd. Many questions, many answers.

We had extraordinary giveaways.

Many people went home with prizes. Mine is Oracle VM and Oracle Linux! Mark won the new Ruby IDE from JetBrains.

Watch this space (and that space) for the presentation material (a one page PDF, which just shows you that you need to come.)

CodeToJoy didn't make it here today, but he really wanted to get people's opinions about OCItv.

Tags :

Monday C++ Quiz: We Are All Confused

There is a whole bunch of super smart C++ types in the office talking about a strange little C++ program, which I'm turning into an emergency quiz.

Q: Should the following C++ program compile? link? run without core dumps? And if so, what does it output?

namespace A {
  class F {
  public:
    static F f;
    F() {}
    void bar() { }
  };
};

namespace {
  A::F::F f;
}

void foo() {
   A::F &myF = A::F::f;
   myF.bar();
}

int main() {
  return 0;
}
Tags :

Mono 2.0 Is Released

Mono Project at Novell: Mono's 2.0 grand release has finally arrived.

See our detailed release notes covering both the 2.0 launch and the changes since Mono 1.9.

Or you can go directly and download Mono 2.0 for your platform.

The release notes has a lot more detailed information. The following struck me as nice, cool, or important:

  • C# 3.0 compiler implementation, with full support for LINQ
  • Mono.Posix: a library to access Linux and Unix specific functionality from your managed application. With both a low-level interface as well as higher level interfaces

I was hoping to find more than two cool features from the release notes, but I couldn't. Had I been an active user of Mono 1.2, or Visual C#, I probably would find more appealing new features, like Mono.Cecil, Mono.Cairo, Mono.SQLite, or even the System.Drawing 2.0 API.

But life as it is, you don't always have time to learn all the interesting new things.

Tags :

Friday Java Quiz: Compile Or No Compile?

Q: Compile, or no compile?

public class Foo {
  public static void main(String[] args) {
    Integer Object = new Integer(1024);
    Object Integer = Object;
    System.out.println(Integer);
  }
}
Tags :

Thursday Sanity Check: What Do You Hear

Listen carefully:

Female voice: 1.5 or 6

Q: Did your mind go: "Java 5 or Java 6"? "1.5 or 1.6"? "1.5 or 6.0"?

Q: 1.6 and 6 are really the same thing, right?

Tags :

Are You Feeling Lucky?

When was the last time you clicked on the "I'm feeling lucky" button on Google.com?

Tags :