1. Using packages
Let's turn our attention to how Java organizes code using packages.
2. What are Java packages?
Applications consist of many code components, such as classes. Packages help organize these classes, making them easier to find and use efficiently. Similar to file folders on a computer, packages group related classes together. Each package has a name that identifies its collection of classes.
3. Types of packages
There are two types of packages. Packages provided by the language are built-in. These packages begin with the name "java" or "javax".
Code we create or code we get from others are in user-defined packages.
Defining our own packages is outside the scope of this course, we use them just like using built-in packages.
4. Package names
Package names follow a convention. Names are in lowercase and use periods to split up parts of the name. Built-in packages start with "java." or "javax." and the rest of the name suggests what the package contains. For example, 'java.security' contains Java's security framework. `java.time` contains dates and time handling code.
Although we won’t be using user-defined packages, their names typically start with an organization’s reversed domain name, followed by the project name and functionality, separated by periods, as shown here.
5. Built-in packages
This table includes some commonly used built-in packages. Note the package names all begin with `java.`.
6. java.math
A commonly used package is `java.math`. It provides classes for arithmetic on large integers and decimals. Classes in this package are often used in mathematically intensive crypto, financial and scientific applications.
`BigInteger` allows working with integers of unlimited digits, unlike long, which is restricted to values between +/- 2^64.
`BigDecimal` represents floating-point numbers with high precision, avoiding the rounding errors associated with `double` and `float`. We'll see an example of the inaccuracy in an upcoming exercise.
7. Using packages
To use classes from a package, add `import` with the package and class name at the top of our class. Here, we import the class `BigInteger` from the `java.math` package.
Use `*` with the package name to import all the types from that package. Both `BigInteger` and `BigDecimal` are in `java.math`.
8. BigInteger & BigDecimal from java.math
When creating an instance of `BigInteger` or `BigDecimal`, use a string representation of the number to ensure accuracy. We can also create `BigDecimal` using a primitive `int`, `long` or `double`. Both classes come with standard add, subtract, multiply and divide methods as well as some other operations such as a power operation.
9. BigInteger and BigDecimal methods
Here are some sample methods on `BigInteger` and `BigDecimal`.
10. Import not required
The `java.lang` package is automatically imported in all Java code. It contains fundamental classes like `System`, `String`, wrapper classes, and `Exception`. Since these types are always available, we don't need an explicit `import` statement, which is why we haven't seen import in any code so far.
11. Let's practice!
Let's take a look at imports and `java.math`.