java

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

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:

System.out.println("foo");

I could just use this:

out.println("foo");

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

A Java static initializer block example

I just ran across a nice example of a Java static initializer block example in an Android book, and thought it might be helpful to share that example here.

First, here's the static initialization code:

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:

Java exec - execute a system command pipeline in Java

In earlier articles I've described how to execute system commands from Java applications. A long time ago I wrote my first article on this topic (How to execute system commands from Java), and more recently I wrote an updated version of that article titled "Executing system commands from Java using the ProcessBuilder and Process classes".

Java StringTokenizer - strings, words, and punctuation marks

I was just reading the book, Hadoop in Action, and came across a nice, simple way to use the Java StringTokenizer class to break a sentence (String) into words, taking into account many standard punctuation marks. Before looking at their solution, first take a look at the code they used to break a String into words using whitespace (a blank):

Mac OS X JAVA_HOME location

Since I can never remember this, JAVA_HOME on a Mac OS X system is located here:

/Library/Java/Home

I believe this is correct for Mac OS X 10.5, 10.6, and 10.7 systems.

(This is most likely the shortest blog post I've ever written. :)

 

A Scala cheat sheet in PDF format

I've been working with Scala quite a bit lately, and in an effort to get it all to stick in my brain, I've created a Scala cheat sheet in PDF format, which you can download below.

This PDF is very different from my earlier Scala cheat sheet in HTML format, as I tried to create something that works much better in a print format. (I first tried to get it all in one page, but short of using a one-point font, that wasn't going to happen.)

A printf format reference page (cheat sheet)

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A cool thing about the printf formatting syntax is that the specifiers you can use are very similar, if not identical, between several different languages, including C, C++, Java, Perl, Ruby, and others, so your knowledge is reusable, which is a good thing.

Java String formatting with the String format method

Java String formatting FAQ: How can I format Java String output?

For a long time I appended Java Strings together (using the "+" operator) to combine them into the output format I wanted, but I always knew that approach was pretty ugly. Fortunately at some point I learned how to use the String format method to format my text, and as you'll see, the resulting Java code is much cleaner this way.

Syndicate content