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?

More than 5 years have passed since last update.

Javaでラッパークラスは使うべきではない3つの理由

0
Posted at

今までなんとなくでやっていたのですが、変数の指定の際にラッパークラスのプリミティブ型の違いがやっとわかったのでメモとして残しておきます。

理由1: ラッパークラスを使うとプログラムのパフォーマンスが悪くなるらしい
理由2: ラッパークラスはnullを許容してしまうため、行動を読む人に「nullが入ってしまう可能性がある」と誤解させてしまう可能性がある
理由3: ラッパークラスはオブジェクトの1つであるため、値同士を比較演算子で以下のように比較しようとした場合、falseになってしまう。
スクリーンショット 2021-01-06 21.52.00.png

またラッパークラスはメリットとしてメソッドが使える点が挙げられるが、それらはスタティックメソッドであるため、変数宣言にラッパークラスを用いなくてもメソッドを使いたいときだけそれらを呼び出せば良いので、基本は変数宣言もプリミティブ型で問題ない。

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?