在条件表达式中null.equals(str)和str.equals(null)类似形式的区别

package test1;

public class javatest1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str="123";
        String str1=null;
        //str-test
        if("123"==str)
        {
            System.out.println("the result of judge is true");
        }
        else
        {
            System.out.println("the result of judge is false");
        }
        if(str=="123")
        {
            System.out.println("the result of judge is true");
        }
        else
        {
            System.out.println("the result of judge is false");
        }
        
        //str1-test
        if("123".equals(str1))
        {
            System.out.println("the result1 of judge is true");
        }
        else
        {
            System.out.println("the result1 of judge is false");
        }
        //通过debug模式调试,在此处遇到了空指针错误
        if(str1.equals("123"))
        {
            System.out.println("the result1 of judge is true");
        }
        else
        {        
            System.out.println("the result1 of judge is false");
        }
    }

}


先看以上实例,运行结果:

显然在str1为空的时候,操作str1.equals("123")出现了空指针异常。所以就是为什么在很多编程实例中的写法都是把表达式中的常量放在前面,把变量放在后面的原因,就是为了避免出现这种空指针异常,特别是在web开发中,不同的页面之间的传值,如果不采用这种写法,就可能导致空指针异常,继而可能导致,页面直接崩溃的结果。基于这种情况,这种常量在前,变量在后的写法也就成为了一种编程的规范。

即应该写成:"123".equals(str1)

 


————————————————
版权声明:本文为优快云博主「hymKing」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/hymking/article/details/8849844

while (true) { String str = input.nextLine(); if (str.equals("end")) { break; } else { String[] nextLine = str.split(" "); if (nextLine.length == 3) { Course course = new Course(nextLine[0], nextLine[1], nextLine[2]); if (course.getCourseNature().equals("必修") && course.getAssessmentMethod().equals("考察")) { System.out.println(course.getCourseName() + " : course type & access mode mismatch"); continue; } if (RepetitiveCourses(course,courses)) continue; courses.add(course); } else if (nextLine.length == 5) { if (Integer.parseInt(nextLine[3]) > 100 || Integer.parseInt(nextLine[3]) < 0 || Integer.parseInt(nextLine[4]) > 100 || Integer.parseInt(nextLine[4]) < 0) { System.out.println("wrong format"); continue; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { Score score = new ExaminationResults(Integer.parseInt(nextLine[3]), Integer.parseInt(nextLine[4])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { courseSelections.add(new CourseSelection(course, student, score)); } } } } else if (nextLine.length == 4) { if (Integer.parseInt(nextLine[3]) > 100 || Integer.parseInt(nextLine[3]) < 0) { System.out.println("wrong format"); continue; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { Score score = new AssessmentResults(Integer.parseInt(nextLine[3])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) continue; courseSelections.add(courseSelection); } } } } } } 将以上代码改进一下
06-01
### 回答1: `Objects.isNull()` 是 `java.util.Objects` 类的一个静态方法,它的作用是判断一个对象是否为 `null`。 具体来说,当调用 `Objects.isNull(obj)` 时,如果 `obj` 为 `null`,则返回 `true`;如果 `obj` 不为 `null`,则返回 `false`。 这个方法非常实用,因为在 Java 中,对象引用为 `null` 时,对象的方法或属性是不能访问的,所以在使用对象之前,通常需要先判断对象是否为 `null`。 例如: ```java String str = null; if (Objects.isNull(str)) { System.out.println("str is null"); } else { System.out.println("str is not null"); } ``` 输出结果为: ``` str is null ``` 注意,`Objects.isNull()` 与 `obj == null` 的效果是相同的,但是 `Objects.isNull()` 更安全,因为它不会抛出空指针异常。 另外,`java.util.Objects` 还提供了一个相反的方法 `Objects.nonNull()`,用于判断一个对象是否不为 `null`。 ### 回答2: Objects.isNull()是Java 8中新增的静态方法,它的作用是判断给定的对象是否为null。在此方法中,如果对象为null,则返回true;反之则返回false。 Objects.isNull()的主要作用是简化代码增强可读性。在以前的版本中,我们通常使用if语句来判断一个对象是否为null。例如: if (obj == null) { // 对象为null的处理逻辑 } else { // 对象不为null的处理逻辑 } 使用Objects.isNull()方法,我们可以用更简洁的方式来实现相同的功能: if (Objects.isNull(obj)) { // 对象为null的处理逻辑 } else { // 对象不为null的处理逻辑 } 此外,Objects.isNull()方法还可以用于lambda表达式中的过滤条件。例如: List<String> list = Arrays.asList("A", null, "B"); List<String> filteredList = list.stream() .filter(Objects::isNull) .collect(Collectors.toList()); 在上述例子中,我们使用filter()方法过滤出了原始列表中为null的元素,并将其收集到一个新的列表中。 总结来说,Objects.isNull()方法的作用是判断给定对象是否为null,可以简化代码,提高可读性,同时也方便了在lambda表达式中的使用。 ### 回答3: Objects.isNull()是Java 8中添加的静态方法。它的作用是用于判断一个对象是否为null。 在Java编程中,我们经常需要判断一个对象是否为null,然后再进行相应的处理。通常我们使用if语句equals()方法来实现这个判断,如if (obj == null)。但是在某些情况下,写这样的判断语句会显得冗长。此时,可以使用Objects.isNull()方法来简化代码。 Objects.isNull()方法接受一个对象作为参数,如果这个对象为null,则返回true;否则返回false。这个方法实际上是对传入的参数进行空值判断,如果参数为null,则返回true,否则返回false。 使用Objects.isNull()方法可以提高代码的简洁性可读性。相对于传统的判断方式,使用Objects.isNull()方法可以减少代码的行数,使代码更加简洁明了。此外,这个方法还支持泛型,可以处理各种类型的对象。 例如,假设有一个对象obj,我们可以通过Objects.isNull(obj)来判断这个对象是否为null。如果返回值为true,则表示对象为null,我们可以进行相应的处理;反之,如果返回值为false,则表示对象不为null,我们也可以根据需要进行相应的处理。 总而言之,Objects.isNull()方法的作用是判断一个对象是否为null。它简化了空值判断的代码,提高了代码的可读性简洁性。在实际的Java编程中,我们可以根据需要使用这个方法来判断对象是否为null,并根据结果做出相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值