本文目录导读:

我来详细介绍Java比较运算的使用案例。
基本比较运算符
public class ComparisonOperators {
public static void main(String[] args) {
int a = 10;
int b = 20;
// 基本比较操作
System.out.println("a == b: " + (a == b)); // false
System.out.println("a != b: " + (a != b)); // true
System.out.println("a > b: " + (a > b)); // false
System.out.println("a < b: " + (a < b)); // true
System.out.println("a >= b: " + (a >= b)); // false
System.out.println("a <= b: " + (a <= b)); // true
}
}
字符串比较
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: " + (str1 == str2)); // true (字符串常量池)
System.out.println("str1 == str3: " + (str1 == str3)); // false (不同对象)
// 使用 equals() 比较内容
System.out.println("str1.equals(str2): " + str1.equals(str2)); // true
System.out.println("str1.equals(str3): " + str1.equals(str3)); // true
// 忽略大小写比较
System.out.println("Hello equalsIgnoreCase hello: " +
"Hello".equalsIgnoreCase("hello")); // true
}
}
对象比较
class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 重写 equals 方法
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Person person = (Person) obj;
return age == person.age && name.equals(person.name);
}
// 实现 Comparable 接口
@Override
public int compareTo(Person other) {
return this.age - other.age; // 按年龄比较
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
public class ObjectComparison {
public static void main(String[] args) {
Person p1 = new Person("Alice", 25);
Person p2 = new Person("Bob", 30);
Person p3 = new Person("Alice", 25);
// 对象引用比较
System.out.println("p1 == p2: " + (p1 == p2)); // false
System.out.println("p1 == p3: " + (p1 == p3)); // false (不同对象)
// 内容比较
System.out.println("p1.equals(p2): " + p1.equals(p2)); // false
System.out.println("p1.equals(p3): " + p1.equals(p3)); // true
// 使用 Comparable 接口比较
System.out.println("p1.compareTo(p2): " + p1.compareTo(p2)); // -5 (25-30)
System.out.println("p2.compareTo(p1): " + p2.compareTo(p1)); // 5 (30-25)
System.out.println("p1.compareTo(p3): " + p1.compareTo(p3)); // 0 (25-25)
}
}
使用 Comparator 进行自定义排序
import java.util.*;
class Student {
String name;
int score;
Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return name + ":" + score;
}
}
public class ComparatorExample {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 85));
students.add(new Student("Bob", 92));
students.add(new Student("Charlie", 78));
students.add(new Student("David", 85));
// 按分数降序排序(相同分数按名字排序)
Comparator<Student> comparator = Comparator
.comparingInt((Student s) -> s.score)
.reversed()
.thenComparing(s -> s.name);
students.sort(comparator);
System.out.println("排序后:");
students.forEach(System.out::println);
// 输出: Bob:92, Alice:85, David:85, Charlie:78
}
}
实际应用案例
public class PracticalComparison {
// 成绩评级系统
public static String gradeStudent(int score) {
if (score >= 90) {
return "优秀";
} else if (score >= 80) {
return "良好";
} else if (score >= 70) {
return "中等";
} else if (score >= 60) {
return "及格";
} else {
return "不及格";
}
}
// 验证密码强度
public static boolean isStrongPassword(String password) {
return password.length() >= 8 &&
password.matches(".*[A-Z].*") &&
password.matches(".*[a-z].*") &&
password.matches(".*\\d.*");
}
// 判断闰年
public static boolean isLeapYear(int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
public static void main(String[] args) {
// 成绩评级
System.out.println("85分的评级: " + gradeStudent(85)); // 良好
System.out.println("59分的评级: " + gradeStudent(59)); // 不及格
// 密码验证
String password = "Password123";
System.out.println(password + " 密码强度: " +
(isStrongPassword(password) ? "强" : "弱"));
// 闰年判断
int year = 2024;
System.out.println(year + "是闰年吗? " +
(isLeapYear(year) ? "是" : "否"));
// 三元运算符案例
int age = 18;
String status = (age >= 18) ? "成年人" : "未成年人";
System.out.println("年龄" + age + "岁: " + status);
}
}
数组和集合比较
import java.util.Arrays;
public class ArrayComparison {
public static void main(String[] args) {
// 数组比较
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = arr1;
System.out.println("arr1 == arr2: " + (arr1 == arr2)); // false (不同对象)
System.out.println("arr1 == arr3: " + (arr1 == arr3)); // true (同一对象)
// 使用 Arrays.equals 比较数组内容
System.out.println("Arrays.equals(arr1, arr2): " +
Arrays.equals(arr1, arr2)); // true
// 多维数组比较
int[][] matrix1 = {{1, 2}, {3, 4}};
int[][] matrix2 = {{1, 2}, {3, 4}};
System.out.println("Arrays.deepEquals: " +
Arrays.deepEquals(matrix1, matrix2)); // true
}
}
常见陷阱和注意事项
public class ComparisonPitfalls {
public static void main(String[] args) {
// 陷阱1: 浮点数比较
double d1 = 0.1 + 0.2;
double d2 = 0.3;
System.out.println("0.1 + 0.2 == 0.3: " + (d1 == d2)); // false
// 正确方式
double epsilon = 0.0001;
System.out.println("Math.abs(d1 - d2) < epsilon: " +
(Math.abs(d1 - d2) < epsilon)); // true
// 陷阱2: Integer 缓存
Integer n1 = 127;
Integer n2 = 127;
System.out.println("n1 == n2 (127): " + (n1 == n2)); // true (缓存)
Integer n3 = 128;
Integer n4 = 128;
System.out.println("n3 == n4 (128): " + (n3 == n4)); // false (超出缓存)
// 正确方式:使用 equals()
System.out.println("n3.equals(n4): " + n3.equals(n4)); // true
}
}
- 基本类型:使用 、、
>、<、>=、<= - 字符串比较比较用
equals(),忽略大小写用equalsIgnoreCase() - 对象比较:重写
equals()方法,实现Comparable或使用Comparator - 浮点数比较:使用误差范围,不要直接用
- 包装类型:使用
equals()而不是
这些比较运算在实际开发中非常常用,掌握它们能帮助你编写更健壮的代码。
版权声明:足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播