0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Javaのラッパークラスをわかりやすく解説!

Posted at

Javaには、「基本データ型(プリミティブ型)」という数字や文字を簡単に扱うデータの種類があります。でも、これだけではちょっと足りないことも。そんなときに使うのが 「ラッパークラス」 です!

この記事では、ラッパークラスが何なのか、どうやって使うのかを、わかりやすく説明します!


ラッパークラスって何?

Javaでは、数字や文字などの「シンプルなデータ(プリミティブ型)」があります。

プリミティブ型 ラッパークラス(包むやつ)
int Integer
double Double
boolean Boolean

プリミティブ型は、軽くて速いけど、「他の特別な機能」を使うことができません。そこで、ラッパークラスを使うと、これを「オブジェクト(機能を持つデータ)」として扱うことができます。


ラッパークラスを使うと何がうれしいの?

1. コレクション(リストやマップ)で使える!

例えば、たくさんの数字をリストに入れたいとします。でも、リストは「オブジェクト」しか入れられないので、そのままではプリミティブ型は使えません。

こんなとき、ラッパークラスを使えば解決!

import java.util.ArrayList;

public class Example {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(10); // 数字をラッパークラスに変換してリストに追加
        numbers.add(20);

        System.out.println("リストの中身: " + numbers);
    }
}

出力結果:

リストの中身: [10, 20]

2. null が使えるようになる!

プリミティブ型は「0」や「false」みたいな「決まった初期値」しか使えません。でも、ラッパークラスなら「データがない」という状態を表すnullを使えます。

public class Example {
    public static void main(String[] args) {
        Integer age = null; // 「年齢が不明」の状態を表現
        System.out.println("年齢: " + age);
    }
}

出力結果:

年齢: null

3. 数字や文字を変換できる!

ラッパークラスには、「文字列を数字に変える」「数字を文字列にする」みたいな便利な機能があります。

public class Example {
    public static void main(String[] args) {
        String input = "123"; // 文字列の"123"
        int number = Integer.parseInt(input); // 文字列を数字に変換
        System.out.println("数字: " + number);

        int value = 456;
        String text = Integer.toString(value); // 数字を文字列に変換
        System.out.println("文字列: " + text);
    }
}

出力結果:

数字: 123
文字列: 456

ラッパークラスの仕組み

Javaでは、プリミティブ型ラッパークラスを「自動的に変換」してくれます。これを 「オートボクシング(包む)」「アンボクシング(ほどく)」 と言います。

public class Example {
    public static void main(String[] args) {
        int primitive = 10; // 普通の数字
        Integer wrapped = primitive; // 自動で包む(オートボクシング)
        int unwrapped = wrapped; // 自動でほどく(アンボクシング)

        System.out.println("ラッパークラス: " + wrapped);
        System.out.println("プリミティブ型: " + unwrapped);
    }
}

出力結果:

ラッパークラス: 10
プリミティブ型: 10

ラッパークラスを使うときの注意点

1. メモリを多く使う

ラッパークラスは、プリミティブ型よりも「重たい」ので、大量のデータを扱うとメモリをたくさん使います。

2. null に注意!

ラッパークラスでは、nullが使える分、nullをそのまま使うとエラーになることがあります。

public class Example {
    public static void main(String[] args) {
        Integer value = null;
        // int unwrapped = value; // エラーが出る(NullPointerException)
    }
}

現場でのラッパークラスの使われ方

よく使われる場面

  • リストやマップで数字を管理するとき
  • データベースやAPIから受け取るデータがnullになるとき
  • 文字列を数字に変換するとき

まとめ

ラッパークラスができること

  1. プリミティブ型を「オブジェクト」に変える。
  2. コレクションやnullの扱いを可能にする。
  3. 便利な変換機能が使える。

注意すること

  • メモリを多く使うため、大量のデータ処理には向いていない。
  • nullが原因でエラーが出る場合がある。

ラッパークラスは、ちょっとした数字の操作から大規模なデータ処理まで、Javaプログラミングでよく使われるツールです。シンプルな使い方を覚えて、便利に活用してみましょう!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?