Command Palette

Search for a command to run...

Back to the lesson: Topic 4.7 — Packages and Imports
Core Java · Example 3 of 3

Settle a clash with a fully qualified name

java.sql.Date extends java.util.Date. For new code prefer java.time.LocalDate (Phase 10).

A package is a named group of related classes, stored in a matching folder, that gives every class a unique full name such as java.util.ArrayList. import lets you write the short name instead, and package-private access lets classes in one package share helpers without exposing them.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a program that uses java.util.List, java.util.Map and java.util.function.Function with explicit single-type imports: build a list of three words, map each to its length with a Function<String, Integer>, store the results in a TreeMap, and print it.

02

Use import static java.lang.Math.*; to print sqrt(144), abs(-7) and pow(2, 10).

03

Without any import statement, create a java.util.ArrayList<Integer>, add 3 and 4, and print its size and contents.

Explain it without notes

01

What problems do packages solve?

02

What does import actually do, and what does it cost at run time?

03

Explain the order in which javac resolves a simple class name, and how an ambiguity arises.

04

Why does running java Main inside the class's own folder fail for a class in a package?

Settle a clash with a fully qualified name
Sign in to run this example in your browser.

Expected output

java.util.Date
java.sql.Date
sql.Date is a util.Date? true