recent posts related to java, jdbc, spring, etc.

Converting a Scala class file to decompiled Java source code

As a Scala newbie, I'm curious about how the process of converting a Scala class back to Java source code works. What I really want to see is how my Scala source code is converted to Java source code. Besides plain old curiosity, I think that understanding more about how Scala works can also be very important to my understanding of Scala (such as the apply() method, and so on).

JOptionPane showMessageDialog examples (part 1)

JOptionPane showMessageDialog examples: I've been working with the Java JOptionPane showMessageDialog a lot lately, so I thought I'd create a page here with a number of JOptionPane showMessageDialog examples, sort of a JOptionPane reference page.

I'll walk you through some JOptionPaneexamples here, starting simple, and increasing the level of difficulty as we go on.

A simple Java Generics example class

To take a break from a project I was working on yesterday I gave my brain a little exercise to see if I could remember how to create a Java class that used the Java 5 Generics magic. Since I've used Scala a lot recently, I decided to create a Java version of the Scala Tuple class.

The Tuple class simply stores two values, which are often key/value pairs. Beginning with the end in mind, here's how you typically use a Tuple:

Java 'import static' example - importing System.out.println and System.err.println

I was just reminding myself how to write a generics class in Java, and for some reason while I was doing that I wanted to use the Java 'import static' capability so instead of typing this:


I could just use this:


The only thing you have to do to make this happen is to use this import static statement at the top of your class:

How to search multiple jar files for a string or pattern

Here's a shell script that I use that search Java jar files for any type of pattern. You can use it to search for the name of a class, the name of a package, or any other string/pattern that will show up if you manually ran jar tvf on each jar file. The advantage of this script -- if you're a Unix, Linux, or Cygwin user -- is that this script will search through all jar files in the current directory.

How to square a number in Java

Java FAQ: How do I square a number in Java?

You can square a number in Java in at least two different ways:

  1. Multiply the number by itself.
  2. Call the Math.pow function.

Here's how to square a number by multiplying it by itself:

i = 2
int square = i * i

In that case, if you print the value of square, it will be 4.

Here's how you call the Math.pow method to square a number:

Syndicate content