0

Original code:

if (!externals.containsKey(query.getName())) {

I need to check also if query.getName() != "manualPrice" so my code becomes:

if ((!externals.containsKey(query.getName())) && (query.getName() != "manualPrice")) {

this isn't giving me the desired result. I'm not getting errors but the page isnt loading.

However annoyingly enough this code does work (it just doesnt feel as neat):

if (!externals.containsKey(query.getName())) {
  if (query.getName() != "manualPrice") {

is there something really obvious I'm missing out. Im not normally at Java developer but do know C# which is pretty similar from what I've seen. Im hoping its just something silly I've missed.

cheers

UPDATE *****

ok so people have pointed out that rather than == i should be using .equals

which I have done. my new code is:

if ((!externals.containsKey(query.getName())) && (!query.getName().equals("manualPrice"))) {

which still gives the same error as it did with the ==

I'm begrudgingly using the nested if method for now but its just got me baffled as to what could possibly be wrong with it. It compiles fine!

Box
  • 87
  • 8
  • 1
    Use `query.getName().equals("manualPrice")` – Lydia Ralph Jul 20 '15 at 15:34
  • 2
    @LydiaRalph I was thinking that too but it doens't explain why the last nested if works, whereas the on-liner doens't .. ? – user2808054 Jul 20 '15 at 15:35
  • Because of De-Morgan's laws. – Maroun Jul 20 '15 at 15:36
  • 1
    @MarounMaroun er ... no. Sorry. That's a cryptic comment and at first glance, and second glance, seems to be incorrect. – user2808054 Jul 20 '15 at 15:41
  • I'm very cryptic now. – Maroun Jul 20 '15 at 15:42
  • 1
    In both cases, try doing `System.out.printf("%d, %d\n", System.identityHashCode("manualPrice"), System.identityHashCode(query.getName()));`. I bet it's just a coincidence it works in one case and not the other. – David Ehrmann Jul 20 '15 at 15:43
  • @MarounMaroun lol.. yes! I'd like to understand the answer to this question too, but so far as I can see adding .equals instead of != won't explain why the second nested arrangement of ifs works when the one-liner doesn't. The logic is the same, you're just replacing the && with a nested if. – user2808054 Jul 20 '15 at 15:44
  • Agreed with @David. You might just be lucky in your second cases and both String happen to end up in the StringPool and have the same pointer: http://stackoverflow.com/questions/3801343/what-is-string-pool-in-java. But really, here you are relying on some compiler optimization. In any cases you should never use == to compare String. – HLP Jul 20 '15 at 15:46
  • @user280805 you're probably reaching the code slightly differently, or some optimization is getting triggered. What if you made `"manualPrice"` a `private static final` variable? – David Ehrmann Jul 20 '15 at 15:47
  • 1
    hey guys. cheers for the correction on the .equals function. I've changed my code to use it if ((!externals.containsKey(query.getName())) && (!query.getName().equals("manualPrice"))) { but im still getting the same error – Box Jul 20 '15 at 15:48
  • 3
    Please post a complete and reproducible example. – Sotirios Delimanolis Jul 20 '15 at 15:49

0 Answers0