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.langString, 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.
  • import brings a type into short-name scope; fully qualified names always work.
  • java.lang is 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());
  }
}

FAQ: Java Packages

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java packages examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java packages in this Java Java lesson (Java Packages).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.