0
0

More than 1 year has passed since last update.

== とequalsの使い分け

Posted at

== とequalsの使い分け

[同一性と同値性]
・同一性
//同じインスタンスを参照していること
・同値性
//インスタンスは異なるが、同じ値を持っているこ

[==]
同一性を示す時に使う
int型などのプリミティブ型の時には、値が等しいことを示すことができる
配列や文字列などの参照型、同じオブジェクトを参照しているときにtrue
→2つの文字列が同じ値であっても、参照している文字列のオブジェクトが異なる場合はfalseになる

[equals]
同値性を示すときに使う
なにかオブジェクトが等しいかどうかを示すときに使う
Ex.

String sample1 = sc.next();
String sample2 = sc.next();

if(sample1.equals(sample2)){
処理
}

0
0
1

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
0