jeudi 8 septembre 2022

Why does String creation using `newInstance()` method behave different when using `var` compared to using explicit type `String`?

I am learning about reflection in Java. By accident, I discovered the following, for me unexpected behavior.

Both test as written below succeed.

    class NewInstanceUsingReflection {

        @Test
        void testClassNewInstance() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
            final var input = "A string";
            final var theClass = input.getClass();
            final var constructor = theClass.getConstructor();
            final String newString = constructor.newInstance();

            assertEquals("", newString);
        }

        @Test
        void testClassNewInstanceWithVarOnly() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
            final var input = "A string";
            final var theClass = input.getClass();
            final var constructor = theClass.getConstructor();
            final var newString = constructor.newInstance();

            assertEquals("A string", newString);
        }

    }

The only difference apart from the assertion is that the newString variable type is explicit in the first test and declared as var in the second test.

I'm using java 17 and the junit5 test framework.

Why is the value of newString an empty string in the first test and the input string value in the second test?

Does it have something todo with the string-pool?

Or is something else going on?


Thanks in advance for the explanation!





Aucun commentaire:

Enregistrer un commentaire