文字列を区別するJavaプログラム==演算子とequals()メソッド
この記事では、Javaで==演算子とequals()メソッドを区別する方法を理解します。 ==(等しい)演算子は、2つのオペランドの値が等しいかどうかをチェックし、等しい場合は条件が真になります。
equals()メソッドは、この文字列を指定されたオブジェクトと比較します。引数がnullでなく、このオブジェクトと同じ文字シーケンスを表すStringオブジェクトである場合にのみ、結果はtrueになります。
以下は同じのデモンストレーションです-
入力がであると仮定します −
The first string : abcde The second string: 12345
必要な出力は −
Using == operator to compare the two strings: false Using equals() to compare the two strings: false
アルゴリズム
Step 1 – START Step 2 - Declare two strings namely input_string_1, input_string_2 and two boolean values namely result_1, result_2. Step 3 - Define the values. Step 4 - Compare the two strings using == operator and assign the result to result_1. Step 5 - Compare the two strings using equals() function and assign the result to result_2. Step 5 - Display the result Step 6 - Stop
例1
ここでは、「main」関数の下ですべての操作をバインドします。
public class compare { public static void main(String[] args) { String input_string_1 = new String("abcde"); System.out.println("The first string is defined as: " +input_string_1); String input_string_2 = new String("12345"); System.out.println("The second string is defined as: " +input_string_2); boolean result_1 = (input_string_1 == input_string_2); System.out.println("\nUsing == operator to compare the two strings: " + result_1); boolean result_2 = input_string_1.equals(input_string_2); System.out.println("Using equals() to compare the two strings: " + result_2); } }
出力
The first string is defined as: abcde The second string is defined as: 12345 Using == operator to compare the two strings: false Using equals() to compare the two strings: false
例2
ここでは、操作をオブジェクト指向プログラミングを示す関数にカプセル化します。
public class Demo { static void compare(String input_string_1, String input_string_2){ boolean result_1 = (input_string_1 == input_string_2); System.out.println("\nUsing == operator to compare the two strings: " + result_1); boolean result_2 = input_string_1.equals(input_string_2); System.out.println("Using equals() to compare the two strings: " + result_2); } public static void main(String[] args) { String input_string_1 = new String("abcde"); System.out.println("The first string is defined as: " +input_string_1); String input_string_2 = new String("12345"); System.out.println("The second string is defined as: " +input_string_2); compare(input_string_1, input_string_2); } }
出力
The first string is defined as: abcde The second string is defined as: 12345 Using == operator to compare the two strings: false Using equals() to compare the two strings: false
-
文字列内の母音をカウントするJavaプログラム
以下が私たちの文字列だとしましょう- String myStr = "Jamie"; 同じ変数の母音を計算するので、変数count=0に設定します。すべての文字をループして母音を数えます- for(char ch : myStr.toCharArray()) { ch = Character.toLowerCase(ch); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u
-
2つのリストをマージしてソートするPythonプログラム
2つのリストをマージして並べ替える必要がある場合は、「sort」メソッドを使用してリストを並べ替えるメソッドを定義できます。 以下は同じのデモンストレーションです- 例 def merge_list(list_1, list_2): merged_list = list_1 + list_2 merged_list.sort() return(merged_list) list_1 = [20, 18, 9, 51, 48, 31] list_2 = [28, 33, 3, 22, 15, 20] print(&