How many String objects would be created when concatenating multiple Strings?

Any answer to your question will depend on the JVM implementation and the Java version currently being used. I think it’s an unreasonable question to ask in an interview. Java 8 On my machine, with Java 1.8.0_201, your snippet results in this bytecode L0 LINENUMBER 13 L0 LDC “First” ASTORE 1 L1 LINENUMBER 14 L1 … Read more

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

Java17, same problem. The explanation is clearly: bug. decompiling it, the relevant section: 20: anewarray #2 // class java/lang/Object 23: invokevirtual #35 // Method java/lang/reflect/Constructor.newInstance:([Ljava/lang/Object;)Ljava/lang/Object; 26: checkcast #41 // class java/lang/String 29: astore 4 31: ldc #23 // String A string 33: ldc #23 // String A string 35: invokevirtual #43 // Method java/lang/String.equals:(Ljava/lang/Object;)Z astore … Read more

How does this Java code snippet work? (String pool and reflection) [duplicate]

What happened to Mario ?? You changed it, basically. Yes, with reflection you can violate the immutability of strings… and due to string interning, that means any use of “Mario” (other than in a larger string constant expression, which would have been resolved at compile-time) will end up as “Luigi” in the rest of the … Read more

error code: 521