• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange output

 
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use NetBeans as editor.

I have the code:



and




Here in my exemple args[0] is "junkfood"

At run the code I got the message :

run:
junkfood
This is bio food
BUILD SUCCESSFUL (total time: 0 seconds)

so Is incorrect.

So if I use checkFood("junkfood");
response is :

run:
junkfood
This is junk food
BUILD SUCCESSFUL (total time: 0 seconds)

This is correct.

I don't understand where is the mistake.


If I run cu parameter args[0] response is wrong.

Can be a issue with my editor?


Thank you.





 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/674031/difference-equals
 
Greenhorn
Posts: 20
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 16 of your code passes to the checkFood method a String object (because junkfood from the command line is a String object), then line 25 compares this object with a literal from the String Pool. The problem is that this comparison is made through the == operator, which actually doesn't check if two String objects have the same content, but if two String references point to the same object in memory. And in this case they obviously don't.
You said that by writing checkFood("junkfood") it works correctly. That's because this time the comparison happens on two references that point to the same literal from the String Pool. But if you attempt to write checkFood(new String("junkfood")), you will encounter the same problem, for the above reasons.

Remember: always compare two strings with the equal method!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrea Simoncini wrote:Remember: always compare two strings with the equal method!


That will always result in a compiler error
 
Dana Ucaed
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Andrea Simoncini wrote:Remember: always compare two strings with the equal method!


That will always result in a compiler error



I forget that I must use equals.



 
Andrea Simoncini
Greenhorn
Posts: 20
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't get it Roel... What do you mean?
 
Andrea Simoncini
Greenhorn
Posts: 20
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, that was just a typo, I spend a lot of time to check if my English is correct and clear and I don't check Java words...
I obviously meant equals.
 
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic