Java-string-equals

提供:Dev Guides
移動先:案内検索

Java-String equals()メソッド

説明

このメソッドは、この文字列を指定されたオブジェクトと比較します。 引数がnullではなく、このオブジェクトと同じ文字シーケンスを表すStringオブジェクトである場合にのみ、結果はtrueです。

構文

このメソッドの構文は次のとおりです-

public boolean equals(Object anObject)

パラメーター

ここにパラメータの詳細があります-

  • anObject -この文字列と比較するオブジェクト。

戻り値

  • このメソッドは、文字列が等しい場合にtrueを返します。それ以外の場合はfalse。

public class Test {

   public static void main(String args[]) {
      String Str1 = new String("This is really not immutable!!");
      String Str2 = Str1;
      String Str3 = new String("This is really not immutable!!");
      boolean retVal;

      retVal = Str1.equals( Str2 );
      System.out.println("Returned Value = " + retVal );

      retVal = Str1.equals( Str3 );
      System.out.println("Returned Value = " + retVal );
   }
}

これは、次の結果を生成します-

出力

Returned Value = true
Returned Value = true