Java – why do I get a non convertible type error?

If I use this class:

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];
    }
}

It works well, I can specify Boolean, no problem Why can't I do the same thing when asking for a user's password?

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);
            JCheck@R_184_2419@ check@R_184_2419@ = new JCheck@R_184_2419@("Pair with this device");
            check@R_184_2419@.setHorizontalAlignment(SwingConstants.LEADING);
            panel.add(label);
            panel.add(input);
            panel.add(check@R_184_2419@);
            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)check@R_184_2419@.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

As far as I know, I do the same thing in both cases, but the first example compiles well and the second example doesn't

Solution

You are using different compiler options You must be These two pieces of code are compiled under Java 7 rules; Neither compile Java 6 rules For example, take your first piece of code (what you call compiled code):

c:\Users\Jon\Test>javac -source 1.7 BooleanTest.java

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

c:\Users\Jon\Test>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

Editor: I think the change is in section 5.5 of JLS (conversion)

Java 7 version includes:

JLS 3rd Edition (Java 5 and 6, basically) includes:

Please note that there is a lack of "unpacking conversion"

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>