Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

USING AUTOMATICALLY

IMPORTED, PREWRITTEN
CONSTANTS AND METHODS
FEROL JOHN NOHAY
JAVA PACKAGES
• Java Packages - group of similar types of classes, interfaces and sub-
packages.
• Note: some of the java packages are imported automatically and some
are need to be imported by the programmers to use the classes under
the java packages (example: java.util.Scanner).
• java.lang package – automatically imported into every program that
you write. It contains the fundamental classes that provide the basis of
the Java programming language
MATH CLASS
• java.lang.Math class – contains constants and methods used to
perform common mathematical functions.
• PI – commonly used Math class constant.
MATH CLASS
• In Math class, the declaration for PI is as follows:
public final static double PI = 3.14159265358979323846;
• Public - so any program can access it directly
• Final - so it cannot be changed
• Static - so only one copy exists and you can access it without declaring
a Math object
• Double - so it holds a floating-point value
• Syntax to use the constant PI: Math.PI
MATH CLASS
• Syntax: Math.(method); example: Math.abs(-9);
• Common methods in Math class:
MATH CLASS
MATH CLASS
• Result of the code (in study guide):

(2)

(1)
MATH CLASS
• Continuation of Result:
(3)
IMPORTING CLASSES THAT ARE NOT IMPORTED
AUTOMATICALLY
• Some of the java classes are not imported automatically unlike
java.lang package.
• These are the three methods to import packages:
• Use the entire path with the class name.
• Import the class
• Import the package that contains the class that will be used.
IMPORTING CLASSES THAT ARE NOT IMPORTED
AUTOMATICALLY
• Example: to import java.time package, one of the classes is LocalDate
• Use the entire path with the class name:
• java.time.LocalDate myBday; //myBday is an identifier.

• Import the class


• import java.time.LocalDate; //located at the very top of source code

• Import the package that contains the class that will be used.
• import java.time.*; //located at the very top of the source code
IMPORTING CLASSES THAT ARE NOT IMPORTED
AUTOMATICALLY
• Wildcard symbol (*) – indicated that it can be replaced by any set of
characters.
• In a Java import statement, you use a wildcard symbol to represent
all the classes in a package.

IMPORTANT NOTE:
• We cannot import all the java classes with import java.*;
• To know more about the list of packages, classes and methods of Java:
(https://docs.oracle.com/javase/8/docs/api/)

You might also like