В Java строки (String) сравниваются не через ==, а с equals(), иначе возможны ошибки.
public class StringComparison {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1 == s2); // ❌ false (сравнение ссылок)
System.out.println(s1.equals(s2)); // ✅ true (сравнение значений)
}
}if (s1.equals(s2)) {
System.out.println("Строки равны ");
}Please open Telegram to view this post
VIEW IN TELEGRAM
👍19🤯3❤2
Ошибка
public class StringComparison {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println(str1 == str2); // ✅ true (оба строки из пула строк)
System.out.println(str1 == str3); // ❌ false (разные объекты в памяти)
}
}Что произошло
System.out.println(str1.equals(str2)); // true
System.out.println(str1.equals(str3)); // true
Please open Telegram to view this post
VIEW IN TELEGRAM
🔥10👍5❤4