Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để phân biệt toán tử chuỗi ==và phương thức equals ()

Trong bài này, chúng ta sẽ hiểu cách phân biệt toán tử ==và phương thức equals () trong Java. Toán tử ==(bằng) kiểm tra xem giá trị của hai toán hạng có bằng nhau hay không, nếu có thì điều kiện trở thành đúng.

Phương thức equals () so sánh chuỗi này với đối tượng được chỉ định. Kết quả là đúng nếu và chỉ khi đối số không rỗng và là đối tượng Chuỗi đại diện cho cùng một chuỗi ký tự với đối tượng này.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

The first string : abcde
The second string: 12345

Đầu ra mong muốn sẽ là -

Using == operator to compare the two strings: false
Using equals() to compare the two strings: false

Thuật toán

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

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "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);
   }
}

Đầu ra

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

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

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);
   }
}

Đầu ra

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