迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

修复 Java 中的错误操作数类型错误

作者:迹忆客 最近更新:2023/11/13 浏览次数:

本文介绍了 Java 中二元运算符的 bad operand types 错误。

二元运算符是需要两个操作数的运算符。算术运算符和关系运算符等运算符称为二元运算符。

运算符在编程中起着至关重要的作用,有时,由于使用不当,二元运算符会给出错误的操作数类型错误。当两种类型的操作数不兼容时,bad operand types 错误是编译时错误。

例如,我们将字符串与整数进行比较时会出现此错误。本文将介绍此错误如何发生以及如何解决的不同示例。

有时,运算符的优先顺序也可能导致运算符类型不兼容并导致控制台出错。


Java 中&运算符的错误操作数类型

让我们首先了解 Java 代码中可能出现 bad operator error 的最常见情况。看下面的示例代码:

public class MyClass {
  public static void main(String args[]) {
    int x = 43;
    if (x & 21 == 1) {
      System.out.println("if block executing");
    } else {
      System.out.println("else block executing");
    }
  }
}

输出:

MyClass.java:4: error: bad operand types for binary operator '&'
      if( x & 21 == 1){
            ^
  first type:  int
  second type: boolean
1 error

发生此错误是因为 ==(等于)运算符的优先级高于& 运算符的优先级。这导致了 21 == 1 评估,它给了我们一个布尔值。

现在,请注意 & 有一个整数操作数和一个布尔值。由于两个运算符的类型不同,&运算符不能工作,所以我们得到一个错误。

我们将使用括号表示需要首先评估 x & 21 以解决此错误。看下面修改后的代码:

public class MyClass {
  public static void main(String args[]) {
    int x = 43;
    if ((x & 21) == 1) {
      System.out.println("if block executing");
    } else {
      System.out.println("else block executing");
    }
  }
}

输出:

if block executing

Java 中&&运算符的错误操作数类型

同样,如果你正在使用逻辑&&(和)运算符,那么在某些情况下你可能会遇到 bad operand types 错误,例如以下示例代码:

public class MyClass {
  public static void main(String args[]) {
    int x = 43;
    if ((x > 10) && (x * 5)) {
      System.out.println("if block executing");
    } else {
      System.out.println("else block executing");
    }
  }
}

输出:

MyClass.java:4: error: bad operand types for binary operator '&&'
      if((x > 10) && (x*5)){
                  ^
  first type:  boolean
  second type: int
1 error

发生此错误是因为 && 操作数需要两个布尔操作数。

这里,表达式 x * 5 给出一个整数值。因此,这里的&& 运算符有一个整数操作数,并给我们带来 bad operand types 错误。

为了解决这个错误,我们将修改这个代码,使得 x * 5==21 返回一个布尔值。看下面修改后的代码:

public class MyClass {
  public static void main(String args[]) {
    int x = 43;
    if ((x > 10) && (x * 5 == 21)) {
      System.out.println("if block executing");
    } else {
      System.out.println("else block executing");
    }
  }
}

输出:

else block executing

Java 中 == 运算符的错误操作数类型

使用 == 等于运算符时,你可能会遇到相同的错误。如果传递的两个操作数都是不同的类型,它会给出一个错误的操作符错误。

看下面的例子:

public class MyClass {
  public static void main(String args[]) {
    int x = 43;
    String y = "43";
    if (x == y) {
      System.out.println("if block executing");
    } else {
      System.out.println("else block executing");
    }
  }
}

输出:

MyClass.java:5: error: bad operand types for binary operator '=='
      if(x == y){
           ^
  first type:  int
  second type: String
1 error

发生此错误是因为 == 等于运算符的操作数属于不同类型。一个是字符串,另一个是整数。

要解决此错误,我们必须转换其中一个以获得相同的数据类型。如果我们将整数转换为字符串,则比较将按词汇顺序进行。

因此,我们将字符串转换为 int。看下面修改后的代码:

public class MyClass {
  public static void main(String args[]) {
    int x = 43;
    String y = "43";
    if (x == Integer.parseInt(y)) {
      System.out.println("if block executing");
    } else {
      System.out.println("else block executing");
    }
  }
}

输出:

if block executing

Java 中 <= 运算符的错误操作数类型

与前面的案例示例一样,当两个操作数的类型不同时,<=(小于等于)运算符也可能给出 bad operator types 错误。看下面的例子:

public class MyClass {
  public static void main(String args[]) {
    int x = 43;
    String y = "43";
    if (x <= y) {
      System.out.println("if block executing");
    } else {
      System.out.println("else block executing");
    }
  }
}

输出:

MyClass.java:5: error: bad operand types for binary operator '<='
      if(x <= y){
           ^
  first type:  int
  second type: String
1 error

要解决此错误,我们必须转换其中一个以获得相同的数据类型。看下面修改后的代码:

public class MyClass {
  public static void main(String args[]) {
    int x = 43;
    String y = "43";
    if (x <= Integer.parseInt(y)) {
      System.out.println("if block executing");
    } else {
      System.out.println("else block executing");
    }
  }
}

输出:

if block executing

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

在 Pandas 的列中展平层次索引

发布时间:2024/04/24 浏览次数:1782 分类:Python

在这篇文章中,我们将使用不同的函数来使用 Pandas DataFrame 列来展平层次索引。我们将使用的方法是重置索引和 as_index() 函数。

Pandas 中的 Groupby 索引列

发布时间:2024/04/23 浏览次数:89 分类:Python

本教程将介绍如何使用 Python Pandas Groupby 对数据进行分类,然后将函数应用于类别。通过示例使用 groupby() 函数按 Pandas 中的多个索引列进行分组。

Pandas 中的散点矩阵

发布时间:2024/04/23 浏览次数:125 分类:Python

本教程演示了如何使用 scatter_matrix 函数在 Pandas 中创建散点图。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便