1
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初心者】==演算子とequalsメソッド

Last updated at Posted at 2019-01-23

■ == 演算子(オペレーター):比較するものはオペランド(値や変数)
■ objectクラスのequalsメソッド:比較するものはオブジェクト

● equalsメソッドはオーバーライドして使うことを前提としている

【Stringクラスのequalsメソッド】

String型のequalsメソッドでは、String型の文字列をchar型の配列に代入して1文字づつ確認する。
つまり、String型のインスタンスは文字列として判断される。

(1) String a = new String("sample");
(2) String b = "sample";
(3) System.out.print(a==b);
(4) System.out.print(a.equals(b));

【解説】
(1)String型のインスタンス
(2)String型のリテラル
(3)実行結果:false 理由:参照が異なる為
(4)実行結果:ture 理由:文字列を比較している為

1
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
1
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?