APIs in Java

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java provides a wide array of tools in your programs to perform many tasks, such as reading files or taking input from the keyboard. These tools are collectively called the Java APIs. This lesson will provide an overview and code examples of APIs in action.

API

API stands for Application Programming Interface. In Java-speak, the APIs are a collection of packages that programmers can import into their programs.

In this lesson we'll discuss the core Java APIs, since they come packaged with an installation of the developer toolkit. However, there are three major types of Java APIs that are available:

  • Core API: This comes when you download the developer kit (JDK), or a developer tool like NetBeans
  • Optional Java-delivered APIs: You may have to download these
  • Unofficial APIs: Available from external websites/sources (or write your own!)

Packages

You'll hear the term package to refer to an API. Think of each API as a bundle of goodies: Inside each package there are interfaces, classes, methods, fields, and constructors. There is a huge amount of functionality in these packages.

There's an API for That!

Let's see how this works with some real code. When you want to use a package/API, you need to use the import keyword and specify which part of the package you want to use. The most common API names start with java, e.g., java.awt.

The official Oracle/Java documentation details each API available. We'll cover a few examples here so you get the idea.

Let's say we want to read and write files from the network. There is an API for that. In this case it is the java.io API/package. If we want to import the ENTIRE API, we use the import keyword and an asterisk to indicate we want ALL functionality. Sometimes this will cause a drag on performance, but for smaller APIs/packages, it may make sense.


import java.io.*;


If, however, you only wanted to bring in a portion of the utility, you can import only that piece. The official Oracle/Java documentation contains the details for all functions of the delivered Java APIs/packages. For example, within the java.io package, there is a DataOutput interface that is used for converting data. If you were only going to perform this type of operation, it may save you a little overhead in processing to import only this interface:


import java.io.DataOutput;


We've mentioned the util package, and in particular the Scanner class; Scanner has methods for accepting keyboard input. If you need to take input from the keyboard, you'll need it! A common way to do this is to directly import the Scanner class:


import java.util.Scanner;


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support