mardi 2 juillet 2019

Incorrect result via refection method call for boolean and int

To my understanding following code should print

TRUE, TRUE

But it printing

FALSE, FALSE

But when I ran the following code via reflection , it is not working as intended.

I have also checked https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7

But this code is not following the above rule, Please help me understand this behavior.

public class Test {
    public static boolean m1() {
        return true;
    }

    public static int m2() {
        return 1;
    }

    public static void main(String[] args) throws Exception {
        final Object a = Test.class.getMethod("m1").invoke(null);
        final Object b = Test.class.getMethod("m2").invoke(null);
        final Object c = m1();

        System.out.println(a == Boolean.TRUE);
        System.out.println(Integer.valueOf(1) == b);
        System.out.println(c == Boolean.TRUE); // Giving TRUE correct result
    }
}





Aucun commentaire:

Enregistrer un commentaire