Javaにおけるデータ型と例外処理
Jim White
Java Developer

.)java または javax で始まる既存パッケージの例
java.security
java.time
ユーザー定義パッケージ名の例
com.mycompany.myproject
com.mycompany.myproject.account
com.mycompany.myproject.controller
com.mycompany.myproject.ui
よく使う既存パッケージ
| Package | Contains/Provides |
|---|---|
java.lang |
基本言語サポートクラス |
java.io |
入出力処理 |
java.util.logging |
ロギング基盤 |
java.math |
高精度の整数・小数演算 |
java.net |
ネットワーク処理 |
java.util |
日付/時刻、Linked List・Dictionary などのデータ構造とサポート |
java.security |
セキュリティ基盤 |
java.math は算術用のクラスを提供BigInteger は大きな整数を表すint や long を超える整数に対応BigDecimal は非常に大きい/小さい浮動小数を表すfloat や double の丸め誤差に対処import + パッケージ名を記述import java.math.BigInteger;
public class HelloWorld {
BigInteger acct = new BigInteger("123");
}
* でパッケージ内の全型をインポートimport java.math.*;
public class HelloWorld {
BigInteger acct = new BigInteger("123");
BigDecimal pi = new BigDecimal("3.14");
}
BigInteger と BigDecimal は大きな数値のラッパーString または数値で生成add、subtract、multiply、divide を提供pow(べき乗)などのメソッドもあり// 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() | 整数の絶対値 |
| add(x) | 整数/小数に x を加算 |
| divide(x) | 整数/小数を x で除算 |
| multiply(x) | 整数/小数に x を乗算 |
| negate() | 整数/小数の符号反転 |
| pow(int x) | 整数/小数の x 乗 |
| subtract(x) | 整数/小数から x を減算 |
java.lang は自動でインポートされるjava.lang の要素は import 不要java.lang には System、String、ラッパークラス、Exception などが含まれるJavaにおけるデータ型と例外処理