Learning Java/Important Java classes

Introduction
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  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, you can use the API. And guess what? If you write text to the command line with, you're already using the API.

Benefits of Using the Java API
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
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  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
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  methods and other programming languages.)

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

It Provides a Standard
If your personal calendar program uses your  class to mark points in time, but your friend's todo list program uses his   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, no translator would be necessary.

Organization of the API
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  package, for example, is the Java standard package containing classes relating to file I/O. contains various utility classes;  provides utility classes for concurrent (multi-threaded) programming.

The Statement
To use classes in the API, use the  statement. At the beginning of your source code file, even before your class definition (the  line), add a line with the   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  class in package   is. To import that class, use the statement

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  and  ), 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:

Note that importing a "superpackage" does not also import its "subpackages"; that is, to import all classes from the packages,  ,  ,  ,  , and  , you must specifically import each of them:

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

is Special
The package  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  package is imported into every Java file whether you put in   or not.

Using the API Documentation
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.

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

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

True to its name,  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.