10 Useful Classes in the Java API - dummies

By Barry Burd

To help acquaint you with some of the best Java API classes for beginner programmers, here is a brief list. For more information on these classes, check Java’s online API documentation.

Applet

What Java discussion is complete without some mention of applets? An applet is a piece of code that runs inside a web browser window. For example, a small currency calculator running in a little rectangle on your web page can be a piece of code written in Java.

At one time, Java applets were really hot stuff, but nowadays, people are much more interested in using Java for business processing. Anyway, if applets are your thing, don’t be shy. Check the Applet page of Java’s API documentation.

ArrayList

This is good stuff, but in any programming language, arrays have their limitations. For example, take an array of size 100. If you suddenly need to store a 101st value, you’re plain out of luck. You can’t change an array’s size without rewriting some code. Inserting a value into an array is another problem.

To squeeze “Tim” alphabetically between “Thom” and “Tom”, you may have to make room by moving thousands of “Tyler”, “Uriah”, and “Victor” names.

But Java has an ArrayList class. An ArrayList is like an array, except that ArrayList objects grow and shrink as needed. You can also insert new values without pain using the ArrayList class’s add method. ArrayList objects are very useful because they do all kinds of nice things that arrays can’t do.

File

Talk about your useful Java classes! The File class does a bunch of useful things. Method canRead tells you whether you can read from a file or not. Method canWrite tells you if you can write to a file. Calling method setReadOnly ensures that you can’t accidentally write to a file.

Method deleteOnExit erases a file, but not until your program stops running. Method exists checks to see whether you have a particular file. Methods isHidden, lastModified, and length give you even more information about a file. You can even create a new directory by calling the mkdir method. Face it, this File class is powerful stuff!

Integer

The Integer class has lots of other features that come in handy when you work with int values. For example, Integer.MAX_VALUE stands for the number 2147483647. That’s the largest value that an int variable can store. The expression Integer.MIN_VALUE stands for the number –2147483648 (the smallest value that an int variable can store).

A call to Integer.toBinaryString takes an int and returns its base-2 (binary) representation. And what Integer.toBinaryString does for base 2, Integer.toHexString does for base 16 (hexadecimal).

Math

Do you have any numbers to crunch? Do you use your computer to do exotic calculations? If so, try Java’s Math class. (It’s a piece of code, not a place to sit down and listen to lectures about algebra.) The Math class deals with π, e, logarithms, trig functions, square roots, and all those other mathematical things that give most people the creeps.

NumberFormat

With the NumberFormat.getCurrencyInstance method, you can turn 20.338500000000003 into $20.34. If the United States isn’t your home, or if your company sells products worldwide, you can enhance your currency instance with a Java Locale. For example, with euro = NumberFormat.getCurrencyInstance(Locale.FRANCE), a call to euro.format(3) returns 3,00 instead of $3.00.

The NumberFormat class also has methods for displaying things that aren’t currency amounts. For example, you can display a number with or without commas, with or without leading zeros, and with as many digits beyond the decimal point as you care to include.

Scanner

Java’s Scanner class can do loads of great things. Like the NumberFormat class, the Scanner can handle numbers from various locales. For example, to input 3,5 and have it mean “three and half,” you can type myScanner.useLocale(Locale.FRANCE). You can also tell a Scanner to skip certain input strings or use numeric bases other than 10. All in all, the Scanner class is very versatile.

String

The String class has many useful methods. For example, with the length method, you find the number of characters in a string. With replaceAll, you can easily change the phrase “my fault” to “your fault” wherever “my fault” appears inside a string. And with compareTo, you can sort strings alphabetically.

StringTokenizer

Sometimes, you may need to chop strings into pieces. For example, you may have a fullName variable that stores a “Barry A. Burd” string. From this fullName value, you need to create firstName, middleInitial, and lastName values. You have one big string (“Barry A. Burd”), and you need three little strings — “Barry”, “A.”, and “Burd”.

Fortunately, the StringTokenizer class does this kind of grunt work. Using this class, you can separate “Barry A. Burd” or “Barry,A.,Burd” or even “Barry<tab>A.<tab>Burd” into pieces. You can also treat each separator as valuable data, or you can ignore each separator as though it were trash. To do lots of interesting processing using strings, check out Java’s StringTokenizer class.

System

You’re probably familiar with System.in and System.out. But what about System.getProperty? The getProperty method reveals all kinds of information about your computer.

Some of the information you can find includes your operating system’s name, your processor’s architecture, your Java Virtual Machine version, your classpath, your username, and whether your system uses a backslash or a forward slash to separate folder names from one another. Sure, you may already know all this stuff. But does your Java code need to discover it on the fly?