Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] String "" in Jetty 8 vs Jetty 9

On Fri, Jan 4, 2019 at 2:27 AM kapil gupta <kapilgupta82@xxxxxxxxx> wrote:
This is for request.getParameter

On Fri, Jan 4, 2019 at 12:30 PM kapil gupta <kapilgupta82@xxxxxxxxx> wrote:
We were using Jetty 8 and now trying to upgrade on Jetty 9. The below piece of code has different results in both versions of Jetty.

if (a != "") 

Where a is string variable. 

I understand the code is wrong, we should always use a.equals for comparison. But we have it at so many places, but not sure why Jetty 8 and Jetty 9 have different results for it.


If you think the question above is valid, then the code is so much more wrong than you are recognizing. :-)

The "" generates a constant string that is at least in principle allocated in the *calling* class. This means it is part of your class. Within your class, it is *possible* that the compiler will do de-duplication of the constant strings, and emit just one constant string representing "" for your whole class, which means that it is *possible* that "" will have the same identity anywhere in your class. I emphasize *possible*, as there is no language level guarantee for this. This is an invalid assumption that should never make it past code review.

Any "" that gets generated from other classes, would either start from a constant string defined in another class (with a different identity!), or start from a char[0] which will always have a new identity. There is almost zero way your code could work in Jetty 8.

I say "almost zero", because there is a way... some Java runtimes may de-duplicate constant strings across classes, or during runtime garbage collection. This may also be triggered manually by using String.intern()  ( https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern() ). In such cases, it's possible that your code might work, sometimes.

"Might work sometimes" dependent upon a runtime feature that was probably not understood at the time it was accidentally selected, is a very bad way to write code.

You need to fix your code.

--
Mark Mielke <mark.mielke@xxxxxxxxx>


Back to the top