Import Statement in AP Computer Science A
In AP Computer Science A, an import statement (like import java.util.ArrayList;) goes at the top of a Java file and tells the compiler where to find a class from another package, so you can use its short name. It shows up with ArrayList, File, and Scanner in Unit 4.
What is Import Statement?
An import statement is a line at the very top of a Java file, above the class header. It tells the compiler which package a class lives in. Java organizes its built-in classes into packages, which are basically labeled folders. ArrayList and Scanner live in java.util. File lives in java.io. Writing import java.util.ArrayList; lets you type ArrayList instead of the full name java.util.ArrayList every time.
Think of it like saving a contact in your phone. The person's full number still exists, but now you can just tap their name. An import doesn't copy any code into your program. It's a shortcut for naming. You can import one class (import java.util.Scanner;) or a whole package with a wildcard (import java.util.*;). Classes in java.lang, like String, Math, and Integer, are imported automatically, so you never write imports for those.
Why Import Statement matters in AP® Computer Science A
Import statements live in Unit 4: Data Collections. They show up in two topics. In 4.8 ArrayList Methods, you can't build an ArrayList<E> without access to java.util.ArrayList. That's the first step toward LO AP Comp Sci A 4.8.A (develop code using ArrayList objects and determine the result of calling their methods). In 4.6 Using Text Files, EK 4.6.A.2 says a file connects to a program through the File and Scanner classes. Those come from two different packages (java.io and java.util), so you need imports from both to meet LO AP Comp Sci A 4.6.A (develop code to read data from a text file). The import is the doorway to the classes. The LOs are about what you do once you're through it.
Keep studying AP® Computer Science A Unit 4
How Import Statement connects across the course
ArrayList and Generic Types (Unit 4)
import java.util.ArrayList; gives you access to the class. The generic type in ArrayList<String> then tells the compiler what goes inside. These are two separate jobs. The import answers "where is this class?" and the type parameter answers "what does it hold?" EK 4.8.A.3 says this lets the compiler catch errors that would otherwise only show up at run-time.
File and Scanner for Reading Text Files (Unit 4)
Reading a file pulls from two packages at once. File comes from java.io and Scanner comes from java.util. That's why file-reading code usually starts with more than one import. If you're missing one, the compiler can't find the class and the code won't compile.
String and Math from java.lang (Unit 1)
You've been using String and Math since early in the course without ever writing an import. That's because java.lang is imported automatically. So the rule is simple. Imports only become something you notice when you reach classes outside java.lang, like ArrayList, File, and Scanner.
Packages (Unit 4)
A package is the address, and the import is how you look up that address. java.util.ArrayList is the class's full name. The import lets you drop the java.util. part for the rest of the file.
Is Import Statement on the AP® Computer Science A exam?
No released FRQ has used the term "import statement" verbatim. FRQs usually have you write methods inside a class that's already set up, so the imports are assumed. The term is more likely to show up in multiple-choice vocabulary and setup questions. One example is "Which term describes a Java statement that makes classes from the java.io package available for use in a program?" The answer is the import statement, not the package itself. You should be able to:
- Pick out the correct import for
ArrayList,Scanner, orFile. - Explain that an import lets you use a short class name instead of the fully qualified one.
- Recognize that missing an import causes a compile-time error, not a run-time error.
- Keep the import (which makes
ArrayListavailable) separate from the generic type (which makesArrayList<Integer>type-safe). Questions like "What is the primary benefit of using ArrayList instead of ArrayList?" test the generic type, not the import.
Import Statement vs Package
A package is the named group a class belongs to, like java.util or java.io. An import statement is the line of code you write to use classes from that package by their short names. The package exists whether you import it or not. So if a question asks what statement makes java.io classes available, the answer is the import statement, not the package.
Key things to remember about Import Statement
An import statement goes at the top of a Java file, before the class header, and tells the compiler which package a class comes from.
Importing a class lets you write
ArrayListinstead of the fully qualified namejava.util.ArrayList. It does not copy any code into your program.ArrayListandScannerare injava.util, whileFileis injava.io, so reading a text file usually needs imports from both packages.Classes in
java.lang, such asString,Math, andInteger, are imported automatically and never need an import statement.Leaving out a needed import causes a compile-time error because the compiler cannot find the class.
The import makes
ArrayListusable, but the generic type inArrayList<String>is what lets the compiler check element types.
Frequently asked questions about Import Statement
What is an import statement in AP Computer Science A?
An import statement is a line like import java.util.ArrayList; at the top of a Java file. It lets you use a class from another package by its short name. In AP CSA it comes up mostly with ArrayList (Topic 4.8) and with File and Scanner (Topic 4.6).
Does an import statement copy the class's code into my program?
No. An import is just a naming shortcut that tells the compiler where to find the class. You could skip it and write the fully qualified name, like java.util.ArrayList<String> list = new java.util.ArrayList<String>();, and the program would work the same way.
Do I need to import String or Math in Java?
No. String, Math, and the wrapper classes like Integer live in java.lang, which Java imports automatically. You only need imports for classes outside java.lang, such as ArrayList, Scanner, and File.
What's the difference between an import statement and a package?
A package is the group a class belongs to, like java.io. An import statement is the code that makes that package's classes available by short name. On a vocab question asking which statement makes java.io classes available, the answer is the import statement.
What imports do I need to read a text file in Java?
Usually import java.io.File; and import java.util.Scanner;, because EK 4.6.A.2 says a file connects to a program through the File and Scanner classes. Many programs instead use wildcards like import java.io.*; and import java.util.*; to cover both at once.
Keep studying AP Computer Science A
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
AP Computer Science A hub
Review units, study guides, and course resources.
AP-style practice
Check this vocabulary in multiple-choice context.
FRQ practice
Apply key concepts in written AP responses.
Score calculator
Estimate the exam score you are working toward.
Cheatsheets
Review the highest-yield facts before practice.
Practice exam
Put the full course together before test day.