Using packages

Data Types and Exceptions in Java

Jim White

Java Developer

What are Java packages?

  • Packages organize Java code
    • A package acts like a file folder or directory
    • Combining related Java code (classes, interfaces, enums, etc.)
  • Like file folders
    • Each package has a name

Java packages are like file folders and filing cabinet for organizing code

1 Image from https://commons.wikimedia.org/wiki/File:File_Cabinet.jpg
Data Types and Exceptions in Java

Types of packages

  • Built-in packages
    • Part of Java
    • Name begins with "java" or "javax"
  • User defined packages
    • Packages we define
    • Packages we get from 3rd parties
Data Types and Exceptions in Java

Package names

  • Package names follow a convention
    • Written in all lowercase
    • Use periods (.) to delimit name parts
  • Built-in packages
    • Start with java or javax
    • Rest of the name suggests purpose
  • User defined package names
    • Start with the reverse of the organization's domain
    • Rest of the name suggests functionality
  • Example built-in packages

    java.security
    java.time
    
  • Example user-defined package names

    com.mycompany.myproject
    com.mycompany.myproject.account
    com.mycompany.myproject.controller
    com.mycompany.myproject.ui
    
Data Types and Exceptions in Java

Built-in packages

Some commonly used built-in packages

Package Contains/Provides
java.lang Base language support classes
java.io Input / output operations
java.util.logging Logging framework
java.math Precision integer and decimal arithmetic
java.net Networking operations
java.util Date / time and data structures like Linked List, Dictionary and support
java.security Security framework
Data Types and Exceptions in Java

java.math

  • java.math provides classes for arithmetic
    • Used in cryptographic, scientific, and currency/money applications
  • BigInteger for representing large integers
    • Integers larger than int or long can handle
    • To work with integers of almost unlimited number of digits.
  • BigDecimal for representing very large or small floating point number
    • Deals with rounding errors that can occur with float or double
Data Types and Exceptions in Java

Using packages

  • Use import + package name at the top
import java.math.BigInteger;
public class HelloWorld {
  BigInteger acct = new BigInteger("123");
}
  • Packages can contain many types.
    • Use * to import all types in the package
import java.math.*;
public class HelloWorld {
  BigInteger acct = new BigInteger("123");
  BigDecimal pi = new BigDecimal("3.14");
}
Data Types and Exceptions in Java

BigInteger & BigDecimal from java.math

  • BigInteger and BigDecimal act as wrappers for big numbers
  • Construct them using a String or numeric
  • Come with add, subtract, multiply, and divide methods
  • Have additional methods like pow for power
// Imports go at the top of the class
import java.math.BigInteger;
import java.math.BigDecimal;

// Create BigInteger or BigDecimal with String
BigInteger big = new BigInteger("1000");
BigInteger ten = new BigInteger("10");
BigDecimal pi = new BigDecimal("3.14");
// Using a primitive to create BigDecimal
BigDecimal one = new BigDecimal(1.0);
BigInteger x = big.add(ten); // = 1010
BigDecimal y = pi.add(one); // = 4.14
BigInteger bigSqr = big.pow(2); // = 1000000
BigDecimal piCubed = pi.pow(3); // = 30.959144
Data Types and Exceptions in Java

BigInteger and BigDecimal methods

Method Description
abs() Absolute value of the integer
add(x) Add x to the integer or decimal
divide(x) Divide the integer or decimal by x
multiply(x) Multiply the integer or decimal by x
negate() Negate the integer or decimal
pow(int x) The integer or decimal to the power of x
subtract(x) Subtract x from the integer or decimal
1 See https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html and https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
Data Types and Exceptions in Java

Import not required

  • Importantly - java.lang package is imported automatically.
    • When using anything from java.lang it does not require an import
  • java.lang includes System, String, the wrapper classes and Exception.
    • Explains why we can learn and use the base language elements without seeing an import.
Data Types and Exceptions in Java

Let's practice!

Data Types and Exceptions in Java

Preparing Video For Download...