Learning Java/Important Java classes

From Wikiversity
Jump to navigation Jump to search

Introduction[edit | edit source]

It's a fact. As a general rule, programmers are lazy. At least, it seems that way, considering how often we talk thinking about the future and reusing code. Why do we care so much?

Suppose for a moment that you're making a calculator program. You need to be able calculate exponents, square roots, and trig functions. Sure, it's possible to write your own function to find a square root, but do you know the algorithm?

What about a calendar? You'd need some sort of Date class that holds a specific instant in time, and maybe another to format that date in a way that humans understand.

Wouldn't it be nice if all these things were done for you? If there was some great library of standard classes you could just import into your programs and use as if you wrote them yourself? Dream no more. The answer is... the Java API.

What does API stand for? "Application Programming Interface." What does that mean? It doesn't matter. What does matter is that there are lots and lots and lots of classes in there that you can use. Oh, and it ships with your compiler. That's right -- if you can write text to the command line with System.out, you can use the API. And guess what? If you write text to the command line with System.out, you're already using the API.

Benefits of Using the Java API[edit | edit source]

You might be wondering why you should bother using code that someone else wrote. Isn't it all about writing the code yourself?

Put simply, no. Here's why.

It Saves You Time[edit | edit source]

You could go online, find an algorithm for calculating a square root, and translate the math into Java code. Or you could use the API's Math class to do it for you. Second idea here is that you can trust Java API. Can you trust your first attempt to make something more complicated than a simple dynamic array memory allocation? There is a lot of debugging and optimizing done. It is a hard work which none of us want to repeat.

It Saves You Effort[edit | edit source]

Suppose you want to read data out of a file. Do you know how to do that? (I'll give you a hint -- it involves native methods and other programming languages.)

...I didn't think so. Using the API's java.io package is much simpler.

It Provides a Standard[edit | edit source]

If your personal calendar program uses your Date class to mark points in time, but your friend's todo list program uses his Time class, you would have to write some sort of translator class to let the programs talk to each other. That's no fun. If you both use java.util.Date, no translator would be necessary.

Organization of the API[edit | edit source]

The Java API is the collective term for all the classes that you can use. It's broken down into packages, groups of related classes.

Package names are lowercase by convention and separated by periods. The java.io package, for example, is the Java standard package containing classes relating to file I/O. java.util contains various utility classes; java.util.concurrent provides utility classes for concurrent (multi-threaded) programming.

The import Statement[edit | edit source]

To use classes in the API, use the import statement. At the beginning of your source code file, even before your class definition (the public class MyClass { line), add a line with the import keyword, the fully-qualified classname of the class you want to use, and a semicolon.

A class's fully-qualified classname is simply the name of the class prefixed with the name of its package and a period. For example, the fully-qualified classname of the Date class in package java.util is java.util.Date. To import that class, use the statement

import java.util.Date;

You can import as many classes as you want, with one exception: you can't create a naming conflict. That is, you can't import two classes with the same (non-fully-qualified) classname (like java.awt.List and java.util.List), or a class whose (non-fully-qualified) classname is the same as the name of one of your classes.

To import all the classes from a package, replace the classname with the asterisk wildcard:

import java.util.*;

Note that importing a "superpackage" does not also import its "subpackages"; that is, to import all classes from the packages java.util, java.util.concurrent, java.util.concurrent.atomic, java.util.concurrent.locks, java.util.jar, and java.util.logging, you must specifically import each of them:

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.jar.*;
import java.util.logging.*;

If you only use the first line, only the classes directly in that package will be imported (like java.util.Date), but no classes in any "subpackages" (like java.util.concurrent.locks.LockSupport).

java.lang is Special[edit | edit source]

The package java.lang is special. It contains fundamental classes that tie directly into the Java language. It contains some of the most commonly-used classes. For this reason, the java.lang package is imported into every Java file whether you put in import java.lang.*; or not.

Using the API Documentation[edit | edit source]

It's all very well for me to tell you the most commonly used classes, what they do, and how to use them. But there are thousands of classes in the API, and there's no way I could tell you about all of them. For this reason, the same people that made the API also created the API documentation, found here.

Important Classes[edit | edit source]

java.lang.String[edit | edit source]

A string is a generic programming term for a variable that holds text. In Java, the java.lang.String class does the trick.

For more information, see the java.lang.String page.

java.lang.Math[edit | edit source]

True to its name, java.lang.Math contains functions for doing math. It uses static methods, which is a topic too advanced for this lesson. For now, just trust that the special syntax we give you works.

For more information, and the special syntax for using static methods, see the java.lang.Math page.





Project: Java
Previous: Java Error Handling — Learning Java/Important Java classes — Next: Java Programming Tips