Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean value is inverted when casting #70

Open
foaw opened this issue Sep 10, 2021 · 0 comments
Open

Boolean value is inverted when casting #70

foaw opened this issue Sep 10, 2021 · 0 comments

Comments

@foaw
Copy link

foaw commented Sep 10, 2021

In Casts#castWithPrimitives(...) there seems to be a typo. When converting Number values with boolean.class as the type, it returns true when the given value is 0, and false when the value is 1.

public static Object castWithPrimitives(Class<?> type, Object value) {
// these values can't be casted from java.lang.Integer
Object numValue = toNumber(value); // handle Boolean and Character
if (numValue instanceof Number) {
Number n = (Number) numValue;
if (type == short.class) {
return n.shortValue();
} else if (type == boolean.class) {
return n.intValue() == 0;

As of Java 16, Java Virtual Machine Specification says:

The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.

Below I am attaching a list of all calls to this method:

Not much to fix, but I could send a PR if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant