Codegym University コースの一部としてのメンターによる講義の抜粋。フルコースにお申し込みください。


「 Java での変数の比較について少し説明したいと思います。 」

「最も単純な比較演算子、より小さい (<) とより大きい (>) はすでにご存知でしょう。」

「はい。」

「等しい (==) や等しくない (!=) などの演算子もあります。また、以下 (<=) や以上 (>=) もあります。」

「今度は面白くなってきました。」

「Java には =< または => 演算子がないことに注意してください。」

" = 記号は代入演算に使用されます。 そのため、等しいかどうかをテストするために 2 つの等号 (==) が使用されます。変数が等しくないことを確認するには、!=演算子を使用します。

"そうか。"

「Java で == 演算子を使用して 2 つの変数を比較する場合、変数の内容を比較していることになります。」

「したがって、プリミティブ変数の場合、その値が比較されます。」

参照変数の場合、参照が比較されます同一であるが別個のオブジェクトがあるとします。それらへの参照が異なるため、比較するとそれらが等しくないことがわかります。つまり、比較結果はfalseになります。参照の比較はtrueになります。」両方の参照が同じオブジェクトを指している場合のみ。

「オブジェクトの内部内容を比較するには、特別なequalsメソッドを使用します。このメソッド(およびObjectクラスのすべてのメソッド)は、宣言していなくてもコンパイラによってクラスに追加されます。いくつかの例を示します: 」

コード 説明
1
int a = 5;
int b = 5;
System.out.println(a == b);
プリミティブ型を比較します。
trueと画面に表示されます。
2
Cat cat1 = new Cat("Oscar");
Cat cat2 = cat1;
System.out.println(cat1 == cat2);
参照を比較します。
trueと画面に表示されます。どちらの変数も同じオブジェクトへの
参照を保存します。
3
String s = new String("Mom");
String s2 = s;
System.out.println(s == s2);
参照を比較します。
trueと画面に表示されます。どちらの変数も同じオブジェクトへの
参照を保存します。
4
Cat cat1 = new Cat("Oscar");
Cat cat2 = new Cat("Oscar");
System.out.println(cat1 == cat2);
参照を比較します。
falseと画面に表示されます。
2 つの変数は同一の Cat オブジェクトを参照しますが、同じものではありません。
5
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s == s2);
参照を比較します。
falseと画面に表示されます。
2 つの変数は同一の String オブジェクトを参照しますが、同じものではありません。
6
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s.equals(s2));
オブジェクトを比較します。
trueと画面に表示されます。
2 つの変数は同一の String オブジェクトを参照します

「ああ、忘れるところでした!ここでいくつかの演習をご紹介します。」

4
タスク
Java Syntax,  レベル 4レッスン 6
ロック未解除
Minimum of two numbers
All search and sort algorithms are based on comparisons. You'll be able to handle these very soon, if you so desire. In the meantime, we suggest starting with something small: write a program to find the minimum of two numbers. Find it and then display it. And if the numbers are the same, display either of them.
4
タスク
Java Syntax,  レベル 4レッスン 6
ロック未解除
Maximum of four numbers
Finding the maximum is an n-ary operation (an operation on n numbers) that returns the largest of several numbers. Never mind. We have no need for such definitions at the secret CodeGym center. We're here to learn how to write code. In this task, you need to use the keyboard to enter four numbers. Then determine the largest of them and display it on the screen.
8
タスク
Java Syntax,  レベル 4レッスン 6
ロック未解除
Sorting three numbers
Planet Linear Chaos is populated by isomorphs. They are believed to have invented sorting algorithms. Everything in their heads is extremely well-ordered. They only issue planetary visas to people who know at least 7 sorting algorithms. Let's take our first step toward Linear Chaos: Read three numbers from the keyboard, put them in descending order, and then display them on the screen.
4
タスク
Java Syntax,  レベル 4レッスン 6
ロック未解除
Jen or Jen?
Jen, Company X's admin, learned how to pilot a space ship and flew away to another planet. People in Company X are good and sincere. It's just that they're scatterbrained and they mix up names. So they decided that the new administrator would also be called Jen. Let's help Company X find their Jen: write a program that checks the identity of two entered names.