Import Statements

Python Java
A module is imported using the form import <module name>

The resources of that module are then referenced using the form

<module name>.<resource name>

Example:
import math

print(math.sqrt(2), math.pi)

Alternatively, an individual resource can be imported using the form
from <module name> import <resource name>
The resource is then referenced without the module name as a qualifier.

Examples:
from math import sqrt, pi

print(sqrt(2), pi)

from random import *

# Reference all resources from random

A package resource is imported using the formimport <package name>.<resource name>;

The resource is then referenced without the package name as a qualifier.

Example:

import javax.swing.JButton;

JButton b = new JButton(“Reset”);

Alternatively, all of the package’s resources can be imported using the form

import <package name>.*;

Occasionally, two resources will have the same name in different packages.  To use both resources, do not import them but just reference them using the package names as qualifiers.

Example:
java.util.List<String> names = new java.util.ArrayList<String>();

java.awt.List namesView = new java.awt.List();

© Ken Lambert. All rights reserved.