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 fine and I can assign that boolean
no problem. Why can I not do the same thing when asking my user for a 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);
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
As far as I can see I'm doing the same thing in both cases but the first example compiles fine while the second example does not.
You're using different compiler options. You must be. Both pieces of code compile under Java 7 rules; neither compiles under Java 6 rules. For example, taking your first piece of code (the one that you say compiles for you):
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
EDIT: I believe the change is in section 5.5 of the JLS (Casting conversions).
The Java 7 version includes:
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)
The JLS 3rd edition (Java 5 and 6, basically) includes:
Casting contexts allow the use of one of:
- ...
- a narrowing reference conversion (§5.1.6) optionally followed by an unchecked conversion
Note the lack of "an unboxing conversion" there.
Change:
result[1] = (boolean)checkbox.isSelected();
To:
result[1] = Boolean.valueOf(checkbox.isSelected());
The problem you have is related with Autoboxing in Java 1.6
You put a primitive type into Object array. Java can not mix primitive with Object, therefore it wrap that primitive boolean into Boolean.
So what you are doing can not be represented as:
boolean result = (boolean) Boolean.TRUE;
The solutions are:
Boolean.TRUE.equals(result[1])
; 用这个
Boolean pair = (Boolean)result[1];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.