Java Tutorial
Java Packages
Packages group related classes into a namespace. import brings a type into scope so you do not write the full path every time.
Why packages exist
Two classes named Utils can coexist if they live in different packages. The full name ispackage.ClassName. The first line of a source file (after comments) is thepackage declaration when the class is not in the default package.
Example — using a library type
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List items = new ArrayList<>();
items.add("tea");
System.out.println(items);
}
} import and fully qualified names
import java.util.Scanner; lets you write Scanner. Without it, use the full namejava.util.Scanner. Same type — different spelling.
Example
public class Main {
public static void main(String[] args) {
java.util.List nums = java.util.List.of(1, 2, 3);
System.out.println(nums.size());
}
} import java.util.*; imports every type in that package (not subpackages). Prefer specific imports in real projects so readers see your dependencies.
java.lang is always there
Types in java.lang — String, System, Math,Object — need no import. Everything else you use from the JDK usually does.
Example
public class Main {
public static void main(String[] args) {
String msg = "hello";
System.out.println(msg.toUpperCase());
System.out.println(Math.max(3, 9));
}
}Folders mirror package names
On disk, package com.study.demo maps to folders com/study/demo/. The public class name must match the file name. Tools and IDEs enforce this layout so javac can find types.
Example — shape of a package file
package com.study.demo;
public class Greeter {
public static String hi(String name) {
return "Hello, " + name;
}
}Single-file demos in this course often omit package and sit in the default package. That is fine for learning; real apps always use named packages.
Try It Yourself
Exercise: Import java.util.HashMap and java.util.Map, put one key/value pair in a map, and print it.
Show solution
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map ages = new HashMap<>();
ages.put("Ada", 36);
System.out.println(ages);
}
} Without the imports you would write java.util.Map and java.util.HashMap everywhere.
Key Takeaways
- Packages are namespaces that group related classes and avoid name clashes.
importbrings a type into short-name scope; fully qualified names always work.java.langis imported automatically; other packages are not.- Folder layout mirrors the package declaration in real projects.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Engineering
Use a library type
Packages group related classes. Fully qualified java.util.List names the type without an import line.
Example
public class Main {
public static void main(String[] args) {
System.out.println(java.util.List.of(1, 2, 3).size());
}
}