lundi 4 février 2019

"Boolean.FALSE" not equal to "false"

So I was playing with "java.lang.reflect" stuff and tried to make something like this. And here is my problem (maybe a bug):

The code for my method to set the field to true:

private static void setFinalStatic(Field field, Object newValue) throws Exception
{
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, newValue);
}

The code where I print it:

setFinalStatic(Boolean.class.getField("FALSE"), true);
System.out.format("%s\n", false);         //prints true
System.out.println(false);                //prints false
System.out.format("%s\n", Boolean.FALSE); //prints true
System.out.println(Boolean.FALSE);        //prints true
System.out.println(Boolean.FALSE == false);        //prints false
System.out.format("%s\n", Boolean.FALSE == false); //prints true

When you use "System.out.format("%s", false)" it return "true", as expected
but when you use "System.out.println(false)" it prints "false"..
And when I tried this "System.out.println(Boolean.FALSE == false)" it printed "false".
Someone please explain me this.





Aucun commentaire:

Enregistrer un commentaire