加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 为什么我得到一个不可转换的类型错误?

发布时间:2020-12-14 05:52:58 所属栏目:Java 来源:网络整理
导读:如果我使用这个类: public class BooleanTest { public static void main(String args[]) { final Object[] objarray = new Object[2]; try { objarray[0] = "Hello World!"; objarray[1] = false; } catch (NullPointerException e) { } boolean bool = (b
如果我使用这个类:
public class BooleanTest {
    public static void main(String args[]) {
        final Object[] objarray = new Object[2];
        try {
            objarray[0] = "Hello World!";
            objarray[1] = false;
        } catch (NullPointerException e) {
        }
        boolean bool = (boolean) objarray[1];
    }
}

它工作正常,我可以指定布尔没有问题.在询问用户密码时,为什么我不能做同样的事情?

final Object result[] = new Object[2];
try {
    java.awt.EventQueue.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(3,0));
            JLabel label = new JLabel();

            label.setHorizontalAlignment(SwingConstants.LEADING);
            JTextField input = new JTextField();

            input.setHorizontalAlignment(SwingConstants.CENTER);
            JCheckBox checkbox = new JCheckBox("Pair with this device");
            checkbox.setHorizontalAlignment(SwingConstants.LEADING);
            panel.add(label);
            panel.add(input);
            panel.add(checkbox);
            if (wrong) {
                label.setText("Wrong password. Please enter the password from the other device:");
            } else {
                label.setText("Please enter the password from the other device:");
            }
            int response = JOptionPane.showConfirmDialog(SendGUI.this,panel,"Enter password",JOptionPane.OK_CANCEL_OPTION);
            if (response == JOptionPane.OK_OPTION) {
                result[0] = input.getText();
                result[1] = (boolean)checkbox.isSelected();
            } else {
                result[0] = null;
                result[1] = false;
            }
        }
    });
} catch (InterruptedException e) {
} catch (InvocationTargetException e) {
}
boolean pair = (boolean)result[1]; //inconvertible type,expected boolean found Object

据我所知,我在两种情况下都做同样的事情,但第一个例子编译得很好而第二个例子没有.

解决方法

您正在使用不同的编译器选项.你一定是.这两段代码都是在Java 7规则下编译的;既不编译Java 6规则.例如,拿你的第一段代码(你说的编译代码):
c:UsersJonTest>javac -source 1.7 BooleanTest.java

(No console output,i.e. no errors)

c:UsersJonTest>javac -source 1.6 BooleanTest.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
BooleanTest.java:10: error: inconvertible types
        boolean bool = (boolean) objarray[1];
                                         ^
  required: boolean
  found:    Object
1 error
1 warning

编辑:我认为更改是在JLS(转换转换)的第5.5节中.

Java 7 version包括:

Casting contexts allow the use of one of:

  • a narrowing reference conversion (§5.1.6) optionally followed by either an unboxing conversion (§5.1.8) or an unchecked conversion (§5.1.9)

JLS 3rd edition(Java 5和6,基本上)包括:

Casting contexts allow the use of one of:

  • a narrowing reference conversion (§5.1.6) optionally followed by an unchecked conversion

请注意那里缺少“拆箱转换”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读