• ベストアンサー

equals()について

supersonicの回答

回答No.3

同じオブジェクトを参照しているかどうか評価するのは「==」です。 equals()メソッドは、意味的に同じかどうかを評価するために使用します。どう実装されているかは、そのクラスによります。 しかし、Javaの世界の全てのクラスはjava.lang.Objectクラスのequals()メソッドを継承して持っていますが、equals()メソッドをオーバーライドしていない場合は、同じオブジェクトを参照しているとtrueを返します。つまり、この場合は実質的に「==」と同じ機能になります。 この辺がこんがらがっているのではないでしょうか?

azicyan
質問者

補足

ありがとうございます。 はい。 こんがらがっていました(^_^;) #2の方の補足にも書いたのですが、 この認識で正しいでしょうか?

関連するQ&A

  • equals()について

    プログラミングの勉強をしています。 endと入力するとプログラムを終了すると言ったプログラムです。 以下のプログラムなのですが、 import java.io.*; class Ex58{ public static void main(String args[])throws IOException{ String s=""; String t = "end"; int number = 0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while(!s.equals(t)){ System.out.print(">"); s = br.readLine(); System.out.println("入力された文字列は" + s + "です。"); System.out.println((number+1) + "番目の文字は" + s.charAt(number) + "です。"); System.out.println((number+2) + "番目の文字は" + s.charAt(number+1) + "です。"); System.out.println("文字列の長さは" + s.length() + "です。"); } } } このプログラムの中で System.out.println((number+1) + "番目の文字は" + s.charAt(number) + "です。"); System.out.println((number+2) + "番目の文字は" + s.charAt(number+1) + "です。"); System.out.println("文字列の長さは" + s.length() + "です。"); を書き加えたところendが入力されても処理が終了しない プログラムになってしまいました。 どなたか助言をよろしくおねがいします。

    • ベストアンサー
    • Java
  • java

    次のコード中の括弧内で下に示す11通りの各コードを実行した場合の実行画面を正確に答えよ。 class A{ public void func1(){System.out.println("A1");} public void func2(){System.out.println("A2");} } class B extends A{ public void func1(){System.out.println("B");} } class C{ public int x=0, y=1; } class D extends C{ public int x=2; public void func1(int x){System.out.println(x);} public void func2(int x){System.out.println(this.x);} public void func3(int x){System.out.println(super.x);} public void func4(int x){System.out.println(this.y);} public void func5(int x){System.out.println(super.y);} } class E{ public void func1(int n){ try{ System.out.println("E1"); int[] ary=new int[n]; System.out.println("E2"); }catch(NegativeArraySizeException e){ System.out.println("E3"); }finally{ System.out.println("E4"); } } } (1) A a=new A(); a.func1(); (2) A a=new B(); a.func1(); (3) B b=new B(); b.func1(); (4) B b=new B(); b.func2(); (5) D d=new D(); d.func1(3); (6) D d=new D(); d.func2(3); (7) D d=new D(); d.func3(3); (8) D d=new D(); d.func4(3); (9) D d=new D(); d.func5(3); (10) E e=new E(); e.func1(5); (11) E e=new E(); e.func1(-2); (1) A1 (2)B (3)B (4) A2 (5) 3 (6) 2 (7) 0 (8) 1 (9) 1 (10) E1E2E4(11) E1E3E4 と答えになるんですがなぜこうなるのかわかりません。教えてください

    • ベストアンサー
    • Java
  • equals()メソッドの継承について

    ある本に ーーーーーーーーーーーーーーーーーーーーー class Car { protected int num; protected double gas; public Car() { num = 0; gas = 0.0; System.out.println("車を作成しました。"); } } class Sample8 { public static void main(String[] args) { Car car1 = new Car(); Car car2 = new Car(); Car car3; car3 = car1; boolean bl1 = car1.equals(car2); boolean bl2 = car1.equals(car3); System.out.println("car1とcar2が同じか調べたところ" + bl1 + "でした。"); System.out.println("car1とcar3が同じか調べたところ" + bl2 + "でした。"); } } JavaのクラスはすべてObjectクラスのメンバを継承していますので、equals()メソッドを記述しなくても、このメソッドを呼び出すことが出来るわけです。 ----------------------------------------- JavaのクラスはすべてObjectクラスのメンバを継承していますので、equals()メソッドを記述しなくても、このメソッドを呼び出すことが出来る    についてですが、 逆から言えば他の言語の場合は本来クラスの宣言の辺りにでもequals()メソッドを記述する必要があるという意味なんでしょうか?

    • ベストアンサー
    • Java
  • equalsメソッドの実装

    いくつかの本をインスタンスで生成します。この本を4つ生成するときに、3つ目に用意したインスタンスと4つ目に用意したインスタンスの中身が等しい=trueという処理をおこないたいのですが、調べてみた結果、自分でequalsを実装しなくてはならないらしく、その実装ができずにこまっています。equalsメソッドがどんな形になるか、教えていただけないでしょうか? class Book{ String name;//書名 String author;//著者 String publisher;//出版 /*コンストラクタ*/ Book( String name, String author, String publisher, int number){ this.name = name;//書名 this.author = author;//著者 this.publisher = publisher;//出版社 } } class BookShelf{ public static void main(String[] args){ /*インスタンスを作成*/ Book aBook_A = new Book("書名1","Aさん","A出版"); Book aBook_B = new Book("書名2","Bさん","B出版"); Book aBook_C = new Book("書名3","Cさん","C出版"); Book aBook_D = new Book("書名3","Cさん","C出版"); System.out.println(aBook_C.equals(aBook_d));//falseが返される。 } }

  • オブジェクトの参照およびハッシュコードについて

     下記のプログラムは任意のクラスMoofおよびStringクラスについてそれらのオブジェクトの参照値とハッシュコードをプリントし、さらにStringオブジェクトの参照値同士の比較をしています。 次の質問に対して分かりやすくご教示ください。 (1)Moofオブジェクトについては参照値そのものの値を出力しているようですが、Stringオブジェクトの場合はオブジェクトの値(abc)が出力されているのは何故ですか (2)参照値の構成は(クラス名+@+16進ハッシュコード)であり、オブジェクト(インスタンス)のあるアドレスを指し示すと聞きました。Stringオブジェクトは2つあってその値はabcで等しいのでハッシュコードも同じになっている訳ですが、もしハッシュコードがアドレスを示すとすると2つのオブジェクトに対してアドレスは1つとなりますが… (3)上の(2)に関連して参照値four、fiveに関するオブジェクトのハッシュコードはともに0x17862で等しいのですが、両者を==演算子で比較するとfalseとなります。 これはどうしてですか。  どなたか詳しい方、よろしくお願いします。 public class EqualsCheckX { public static void main(String[] args) { Moof one = new Moof(8); Moof two = new Moof(8); Moof three = one; int a = one.hashCode(); int b = two.hashCode(); int c = three.hashCode(); String four = new String("abc"); String five = new String("abc"); String six = four; int d = four.hashCode(); int e = five.hashCode(); int f = six.hashCode(); System.out.println("Moof " + " one : " + one + " two : " + two + " three : " + three); System.out.println("String" + " four : " + four + " five : " + five + " six : " + six); System.out.println(); System.out.println("hashCode" + " one : " + Integer.toHexString(a) + " two : " + Integer.toHexString(b) + " three : " + Integer.toHexString(c)); System.out.println("hashCode" + " four : " + Integer.toHexString(d) + " five : " + Integer.toHexString(e) + " six : " + Integer.toHexString(f)); System.out.println(); // String if (four == five) { System.out.println("String : four and five are equal"); } else { System.out.println("String : four and five are not equal"); } if (four == six) { System.out.println("String : four and six are equal"); } else { System.out.println("String : four and six are not equal"); } } } class Moof { private int moofValue; Moof(int val) { moofValue = val; } } C:\MyJava>java EqualsCheckX Moof one : Moof@1ac04e8 two : Moof@765291 three : Moof@1ac04e8 String four : abc five : abc six : abc hashCode one : 1ac04e8 two : 765291 three : 1ac04e8 hashCode four : 17862 five : 17862 six : 17862 String : four and five are not equal String : four and six are equal

  • 環境でeval()が利用できません

    3×3の簡易エクセルをjavaで作っているところです。 各セルにキーボードから入力された数値を表示するところまではできました。しかし、Data: と表示するところで、A1+A2などを入力すると、 エラーが出ます。 Row:A Line:1 Data:2 Row:A Line:2 Data:4 と入力しているとき Row:A Line:3 Data:A1+A2 と入力された際、 2 4 6 と表示させたいです。どう改良すればいいでしょうか? for(roop=0;roop<=8;roop++) { System.out.print("Row:"); try { Row_before = br.readLine(); } catch(IOException e) { System.err.println(e); } if(Row_before.equals("A")||Row_before.equals("B")||Row_before.equals("C")) { Row[roop] = Row_before;Row_before=""; } else {Row_before="";} System.out.print("Line:"); try { Line_before = br.readLine(); } catch(IOException e) { System.err.println(e); } if(Line_before.equals("1")||Line_before.equals("2")||Line_before.equals("3")) {Line[roop] = Line_before;Line_before="";} else {Line_before="";} System.out.println("Data:"); //A1に数値を入力 if(Row[roop].equals("A")&&Line[roop].equals("1")) { try { A1_string = br.readLine(); } catch(IOException e) { System.err.println(e); } A1 = Integer.parseInt(A1_string); System.out.println(""+A1_string); } //以下同様の動き if(Row[roop].equals("A")&&Line[roop].equals("2")) {try{A2_string = br.readLine();}catch(IOException e){System.err.println(e);}A2 = Integer.parseInt(A2_string);} if(Row[roop].equals("A")&&Line[roop].equals("3")) {try{A3_string = br.readLine();}catch(IOException e){System.err.println(e);}A3 = Integer.parseInt(A3_string);} if(Row[roop].equals("B")&&Line[roop].equals("1")) {try{B1_string = br.readLine();}catch(IOException e){System.err.println(e);}B1 = Integer.parseInt(B1_string);} if(Row[roop].equals("B")&&Line[roop].equals("2")) {try{B2_string = br.readLine();}catch(IOException e){System.err.println(e);}B2 = Integer.parseInt(B2_string);} if(Row[roop].equals("B")&&Line[roop].equals("3")) {try{B3_string = br.readLine();}catch(IOException e){System.err.println(e);}B3 = Integer.parseInt(B3_string);} if(Row[roop].equals("C")&&Line[roop].equals("1")) {try{C1_string = br.readLine();}catch(IOException e){System.err.println(e);}C1 = Integer.parseInt(C1_string);} if(Row[roop].equals("C")&&Line[roop].equals("2")) {try{C2_string = br.readLine();}catch(IOException e){System.err.println(e);}C2 = Integer.parseInt(C2_string);} if(Row[roop].equals("C")&&Line[roop].equals("3")) {try{C3_string = br.readLine();}catch(IOException e){System.err.println(e);}C3 = Integer.parseInt(C3_string);} System.out.println(""); }

  • throw文について

    上と下のソースプログラムがよく似ているのにもかかわらず上記のプログラムでコンパイルエラー発生する理由がわかりません。 解決方法はpublic static void badMethod()throws IOException{に書き換えるということが分かっていますがイマイチ理屈が分からない次第であります。 たぶん、上記のプログラムはimport文があるからだと思うのですが回答のほどよろしくお願い致します。 (コンパイルエラー) import java.io.IOException; class TryCatch{ public static void main(String args[]){ try{ badMethod(); System.out.println("A"); } catch(IOException ex){ System.out.println("B"); } catch(Exception e){ System.out.println("C"); } System.out.println("E"); } public static void badMethod(){ throw new IOException(); } } (コンパイル正常) public class X{ public static void main(String args[]){ try{ badMethod(); System.out.println("A"); } catch(Exception ex){ System.out.println("B"); } finally{ System.out.println("C"); } System.out.println("D"); } public static void badMethod(){ throw new RuntimeException(); } }

  • Java言語のプログラムをC言語にする場合

    次の2つのJava言語のプログラムをC言語にしたいのですが、C言語でプログラムを書いたことがありません。C言語にする場合はどう書けばいいのでしょうか? import java.io.*; public class Sort { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("整数値を入力してください"); String line = reader.readLine(); int a = Integer.parseInt(line); String line2 = reader.readLine(); int b = Integer.parseInt(line2); String line3 = reader.readLine(); int c = Integer.parseInt(line3); String line4 = reader.readLine(); int d = Integer.parseInt(line4); int[] data = {a, b, c, d}; for (int i = 0; i< data.length - 1 ; i++) { for (int j = i + 1; j< data.length; j++) { if(data[i] > data[j]) { int e = data[i]; data[i] = data[j]; data[j] = e; } } } System.out.println("昇順に並べ替えると、"); for (int i = 0; i< data.length; i++) { System.out.print(data[i] + " "); } System.out.println("です。"); } catch (IOException e){ System.out.println(e); } catch (NumberFormatException e) { System.out.println("数式の形式が正しくありません。"); } } } import java.io.*; public class Yakusu { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("2つの整数値を入力してください"); System.out.print("整数A : "); String line = reader.readLine(); int a = Integer.parseInt(line); System.out.print("整数B : "); String line2 = reader.readLine(); int b = Integer.parseInt(line2); if(a%b == 0){ System.out.println("BはAの約数です"); } else { System.out.println("BはAの約数ではありません"); } } catch (IOException e){ System.out.println(e); } catch (NumberFormatException e) { System.out.println("数式の形式が正しくありません。"); } } }

  • Javaの基礎のプログラム

    結城浩さんの本に載っていたプログラムです。 ですが、説明がわかりにくくていまいち処理がわからなかったので 質問させていただきました。 どうやら、X軸とY軸がありY軸は下に行けばいくほど数字が大きくなる。 X軸は右に行けばいくほど大きくなる。座標上にある、二つの長方形の重なりあう部分の座標を求めるプログラムのようです。(イメージとしては、下に添付してある画像のような感じになるようです。) 質問としては一つです。 プログラム中のこの部分なのですが、おそらく「長方形の座標を示している」行であると思います。 a = new Rectangle(0, 0, 20, 10); b = new Rectangle(5, 5, 20, 10); c = new Rectangle(20, 10, 20, 10); d = new Rectangle(-10, -20, 100, 200); e = new Rectangle(21, 11, 20, 10); その後の処理としては、aとb→c→d→eというようにそれぞれ比較していっているのもなんとなくわかります。 ですが、しっかり解説されていなかったので、この4つの数字がどのように座標を示しているのかよくわかりませんでした。 よろしくお願いします。 class Rectangle { final int INITIAL_WIDTH = 10; final int INITIAL_HEIGHT = 20; int width; int height; int x; int y; Rectangle() { width = INITIAL_WIDTH; height = INITIAL_HEIGHT; x = 0; y = 0; } Rectangle(int width, int height) { this.width = width; this.height = height; this.x = 0; this.y = 0; } Rectangle(int x, int y, int width, int height) { this.width = width; this.height = height; this.x = x; this.y = y; } void setLocation(int x, int y) { this.x = x; this.y = y; } void setSize(int width, int height) { this.width = width; this.height = height; } public String toString() { return "[" + x + ", " + y + ", " + width + ", " + height + "]"; } Rectangle intersect(Rectangle r) { int sx = Math.max(this.x, r.x); int sy = Math.max(this.y, r.y); int ex = Math.min(this.x + this.width, r.x + r.width); int ey = Math.min(this.y + this.height, r.y + r.height); int newwidth = ex - sx; int newheight = ey - sy; if (newwidth > 0 && newheight > 0) { return new Rectangle(sx, sy, newwidth, newheight); } else { return null; } } public static void main(String[] args) { Rectangle a, b, c, d, e; a = new Rectangle(0, 0, 20, 10); b = new Rectangle(5, 5, 20, 10); c = new Rectangle(20, 10, 20, 10); d = new Rectangle(-10, -20, 100, 200); e = new Rectangle(21, 11, 20, 10); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); System.out.println("a と a の重なり = " + a.intersect(a)); System.out.println("a と b の重なり = " + a.intersect(b)); System.out.println("a と c の重なり = " + a.intersect(c)); System.out.println("a と d の重なり = " + a.intersect(d)); System.out.println("a と e の重なり = " + a.intersect(e)); } }

    • ベストアンサー
    • Java
  • Javaの参照型変数の比較についての質問です。

    SUN教科書Javaアソシエイツ P209 問5-8より 出力結果を問う問題です。 class A{String s = ”ABC”;} class Sample{ public static void main(String[] args){ A a1 = new A(); A a2 = new A(); if(a1 == a2){ System.out.println("a1 == a2"); } if(a1.s == a2.s){ System.out.println("a1.s == a2.s"); } if(a1.equals(a2)){ System.println("a1 equals a2"); } if((a1.s).equals(a2.s)){ System.out.println("a1.s equals a2.s"); } } } 【解答】 a1.s == a2.s a1.s equals a2.s 「a1.s」と「a2.s」の記述、つまり表記が意味している内容が良く分かりません。 a1とa2が別のオブジェクトであることは理解出来ています。 ですから、(a1 == a2)と(a1.equals(a2))がfalseとなるのも分かります。 ですが、「a1.s」の「.s」とは何なのでしょうか。 解説には「a1.sとa2.sはAクラスのオブジェクトが保持する同一のStringオブジェクトを 参照しています。」とあります。Aクラスを生成した時にその中にString型クラスsが 作れているということでしょうか。 アドバイスをよろしくお願い致します。

    • ベストアンサー
    • Java