Chapter 22

Ten Useful Classes in the Java API

IN THIS CHAPTER

check Finding out more about classes

check Discovering other helpful classes

I’m proud of myself. I’ve written around 400 pages about Java using fewer than 30 classes from the Java API. The standard API has thousands of classes, so I think I’m doing very well.

Anyway, to help acquaint you with some of my favorite Java API classes, this chapter contains a brief list. Some of the classes in this list appear in examples throughout this book. Others are so darn useful that I can’t finish the book without including them.

For more information on the classes in this chapter, check Java’s online API documentation at http://docs.oracle.com/javase/8/docs/api.

ArrayList

Chapter 16 introduces arrays. 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 things that aren’t included in this book’s examples. Method canRead tells you whether you can read from a file. Method canWrite tells you whether 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

Chapter 18 describes the Integer class and its parseInt method. 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. (Refer to Chapter 7.) 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).

JFrame

Chapter 20 has a JFrame example. A JFrame can be the starting point for an app’s appearance on the screen. A JFrame is like a window, so you can put buttons, text fields, and other useful widgets on a JFrame. In Chapter 20, I put an image on a JFrame.

A JFrame has one of several different layouts. For example, a border layout divides the JFrame into five regions: the NORTH, SOUTH, EAST, WEST, and CENTER regions. An item in the CENTER region is usually the largest. It’s the centerpiece of the JFrame. Items in the other border layout regions live on the four edges of the JFrame.

With a grid layout, you put items into table cells, and with a flow layout, you place items one after another in a row.

JOptionPane

For a quick and easy graphical user interface, use a JOptionPane. Here’s some code:

String word = JOptionPane.showInputDialog("Enter a word");
JOptionPane.showMessageDialog(null, word);
String string = JOptionPane.showInputDialog("Enter an int value");
int number = Integer.parseInt(string);
number++;
JOptionPane.showMessageDialog(null, "One more is " + number);

When you run this code, you see the dialog boxes in Figures 22-1 to 22-4.

image

FIGURE 22-1: Showing an input dialog.

image

FIGURE 22-2: Showing a message dialog.

image

FIGURE 22-3: Showing another input dialog.

image

FIGURE 22-4: Showing another message dialog.

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 uc, e, logarithms, trig functions, square roots, and all those other mathematical things that give most people the creeps.

NumberFormat

Chapter 18 has a section about the NumberFormat.getCurrencyInstance method. With this 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 more than what it does in this book’s examples. Like the NumberFormat class, the Scanner can handle numbers from various locales. For example, to input 3,5 and have it mean “three-and-a-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 quite versatile.

String

Chapter 18 examines Java’s String class. The chapter describes (in gory detail) a method named equals. The String class has many other 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.

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 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?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.188.238