Packages organize classes into namespaces (com.example.app) and map to folder paths. import brings types into scope—only one public class per file, package declaration at top.
Structure
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
Static import
import static java.lang.Math.max;
Use sparingly—readable code beats wildcard imports in large files.
Important interview questions and answers
- Q: Package vs import?
A: Package declares this class's namespace; import references other packages' types. - Q: java.lang imported automatically?
A: Yes—String, System, Math need no import.
Self-check
- How does package name relate to directory structure?
- Why avoid
import java.util.*in production code?