কম্পিউটার

স্ট্রিং ==অপারেটর এবং সমান() পদ্ধতিতে পার্থক্য করার জন্য জাভা প্রোগ্রাম


এই নিবন্ধে, আমরা জানব কিভাবে জাভাতে ==অপারেটর এবং সমান() পদ্ধতির মধ্যে পার্থক্য করা যায়। ==(সমান) অপারেটর চেক করে যে দুটি অপারেন্ডের মান সমান কি না, যদি হ্যাঁ হয় তবে শর্ত সত্য হয়।

equals() পদ্ধতি এই স্ট্রিংটিকে নির্দিষ্ট বস্তুর সাথে তুলনা করে। ফলাফলটি সত্য যদি এবং শুধুমাত্র যদি যুক্তিটি শূন্য না হয় এবং এটি একটি স্ট্রিং অবজেক্ট যা এই অবজেক্টের মতো অক্ষরগুলির একই ক্রম উপস্থাপন করে৷

নীচে একই -

এর একটি প্রদর্শন রয়েছে৷

ধরুন আমাদের ইনপুট হল

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

এখানে, আমরা 'প্রধান' ফাংশনের অধীনে সমস্ত ক্রিয়াকলাপ একসাথে আবদ্ধ করি।

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

  1. কিভাবে আমরা দুটি স্ট্রিং মিশ্রিত করব এবং জাভাতে আরেকটি তৈরি করব?

  2. জাভাতে দুটি স্ট্রিং তুলনা করুন

  3. জাভা প্রোগ্রাম একটি স্ট্রিং মধ্যে স্বর গণনা

  4. পাইথন প্রোগ্রাম দুটি তালিকা একত্রিত করতে এবং এটি সাজানোর জন্য