2016-12-13 5 views
0

&のオンラインビデオのいくつかを検索したが、適切ではないことがわかったときに、のJavaで&のプログラムを作成しました説明。だから、みんなが私にそれを手伝ってくれる。クラスコンストラクターが作成され、単純な文字列を比較したときの等価()

ありがとうございました。

package Practice; 

public class StringManipulation11 { 

    StringManipulation11(String s) { 
    } 

    public static void main(String[] args) { 

     String s = "Good"; 
     String s1 = "Good"; 
     String s2 = "Morning"; 

     String t = new String("Good"); 
     String t1 = new String("Good"); 
     String t2 = new String("Morning"); 

     StringManipulation11 sm = new StringManipulation11("Good"); 
     StringManipulation11 sm1 = new StringManipulation11("Good"); 

     System.out.println(s.equals(s1));// true because check content 
     System.out.println(s.equals(s2));// false content not match 

     System.out.println(t.equals(t1));// true because check content 
     System.out.println(s.equals(t));// true because check content 

     System.out.println(sm.equals(sm1));// false, but not getting the reason 
              // why it is false 

     /* 
     * In this case also the content is same but not getting the proper 
     * conclusion why it is false & it is false then why i am getting true 
     * in "System.out.println(t.equals(t1))" in this condtion. 
     */ 

     System.out.println(s.equals(sm)); 

    } 
} 
+0

普通の言葉でそれを置くために – Shaun

+0

ただ、クラスの仲間 – Ash

+0

だん内容を確認する必要があります。 あなたはそれが2つのオブジェクトを比較するとき、「等しい」ことを意味するものと述べたことはありません。 – Shaun

答えて

0

StringManipulation11これは定義されますが、デフォルトの方法は

public boolean equals(Object obj) { 
     return (this == obj); 
    } 

あるequals methodをオーバーライドしなかった場合は、String

public boolean equals(Object var1) { 
     if(this == var1) { 
      return true; 
     } else { 
      if(var1 instanceof String) { 
       String var2 = (String)var1; 
       int var3 = this.value.length; 
       if(var3 == var2.value.length) { 
        char[] var4 = this.value; 
        char[] var5 = var2.value; 

        for(int var6 = 0; var3-- != 0; ++var6) { 
         if(var4[var6] != var5[var6]) { 
          return false; 
         } 
        } 

        return true; 
       } 
      } 

      return false; 
     } 
    } 
0

にequalsメソッドを比較することができ、オブジェクトを拡張していますStringクラス(Java 8)のequalsメソッドの説明:

public boolean equals(Object anObject) 
Compares this string to the specified object. The result is true if and only 
if the argument is not null and is a String object that represents the same 
sequence of characters as this object. 

つまり、両方の文字列が同じ文字列を表す場合に限り、このメソッドはtrueを返します。

あなたの場合、StringManipulation11という新しいクラスを定義しています。このクラスは、デフォルトでObjectクラスのequalsメソッドを使用しています。あなたの例では

The equals method for class Object implements the most discriminating 
possible equivalence relation on objects; that is, for any non-null 
reference values x and y, this method returns true if and only if x and y 
refer to the same object (x == y has the value true). 

あなたは2つの異なるオブジェクトsmsm1を定義している:そして、これはその定義があります。これらの変数はDIFFERENTオブジェクトを参照するため、equalsメソッドがfalseを返すのはこのためです。この場合

4

、また、内容は同じですが、私がこの状態でSystem.out.println(t.equals(t1))に真取得していますなぜそれがその後、偽の&偽である理由を、適切な結論を得ていません。

クラスString文字ことにより、2つのオブジェクトの文字を比較equals(及びhashcode)の実装を有します。あなたのクラスはこれらのメソッドの実装を持っていないので、参照を比較するObjectから継承した実装を使用します。つまり、trueの場合、インスタンスはと同じである必要があります。

これはあなたの頭の中で重要な区別です。同じは、これら2つの参照がまったく同じインスタンスを指していることを意味します。 と等しい場合は、同じコンテンツでも同等のコンテンツでもありますが、コンテンツの比較方法はユーザーが定義します。thisを参照してください。

+0

をバディ、StringManipulationはまあ、それは理由 –

0

あなたがあなた自身の書かれたクラスのequals()メソッドと平等を確認したい場合は、Objectから継承されたequal()のオブジェクトclass.Theのデフォルトの実装のequals()メソッドをオーバーライドする必要があり、2つの変数が同じに指している場合にのみtrueを返しあなたのケースでは、smとsm1オブジェクトは同じ情報を含んでいますが、それらはメモリ内の異なるオブジェクトです。その理由はfalseを返します。

+0

文字列t =新しい文字列( "Good"); 文字列t1 =新しい文字列( "Good"); これはまた、それがなぜ真実を与えているのかという異なるオブジェクトです。 – Ash

+0

独自のオブジェクトを作成し、そのオブジェクトの等価性をチェックしたい場合は、クラス内のequalsメソッドをオーバーライド(実装)し、Stringクラスで、Java言語がString equals Stringクラスの実装された/オーバーライドされたequalsメソッドのため、これをtrueにしています – Zia

関連する問題