0% found this document useful (0 votes)
121 views12 pages

3 - Using Automatically Imported, Prewritten Constants and Methods

This document discusses automatically imported Java packages and classes, as well as how to import classes that are not automatically imported. It provides details on the Math class, including the PI constant, common methods like abs(), and how to call them. It also describes the three ways to import classes from packages that are not automatically imported: using the full package and class name, importing just the class, or importing the entire package using a wildcard.

Uploaded by

Chesley Carolino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views12 pages

3 - Using Automatically Imported, Prewritten Constants and Methods

This document discusses automatically imported Java packages and classes, as well as how to import classes that are not automatically imported. It provides details on the Math class, including the PI constant, common methods like abs(), and how to call them. It also describes the three ways to import classes from packages that are not automatically imported: using the full package and class name, importing just the class, or importing the entire package using a wildcard.

Uploaded by

Chesley Carolino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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