Data Types and Exceptions in Java
Jim White
Java Developer
.
) to delimit name partsjava
or javax
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
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 |
java.math
provides classes for arithmeticBigInteger
for representing large integersint
or long
can handleBigDecimal
for representing very large or small floating point numberfloat
or double
import
+ package name at the topimport java.math.BigInteger;
public class HelloWorld {
BigInteger acct = new BigInteger("123");
}
*
to import all types in the packageimport java.math.*;
public class HelloWorld {
BigInteger acct = new BigInteger("123");
BigDecimal pi = new BigDecimal("3.14");
}
BigInteger
and BigDecimal
act as wrappers for big numbersString
or numericadd
, subtract
, multiply
, and divide
methodspow
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
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 |
java.lang
package is imported automatically.java.lang
it does not require an importjava.lang
includes System
, String
, the wrapper classes and Exception
.Data Types and Exceptions in Java