• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with a If statement  RSS feed

 
Cid Rodrigues
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have started studying Java and i am loving it but i have a very peculiar problem an if statement is not behaving as it should be whatever the condition i introduce it always enters the code block...i have check the condition of the if statement and it is what i was expecting but it stills enters...i will post the code in the hopes that a more knowledgeable person gets this headache because this is a strange behaviour for an if statement.

Thank you in advance for your help and time.





 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cid:

You have a number of 'if' code blocks. Which specific one are you having the problem with, what is the condition you are introducing, and what is your expected result?

John.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why you having a ; after ) in this if statement while you already open the braces { to define the scope of this if statement.

and use the .equals method in case of matching string instead of == like this.
 
Cid Rodrigues
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Saifuddin wrote:why you having a ; after ) in this if statement while you already open the braces { to define the scope of this if statement.

and use the .equals method in case of matching string instead of == like this.


Thank you very much Muhammed you have clearly a better vision than me...must be going blind!

Edit: Java is not my first programming language and i though strange that the writer would use the .equals() when == can be used. I am used to use == operator i though the writer would have a explanation the use of the equals method. do you guys have any idea for that?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should only use == for primitives, otherwise it isn't doing what you think it is.

When you say something like

MyObject variableName = new MyObject();

the variableName holds the ADDRESS (more or less) of where the data actually is. Say you are a homebuilder. You don't carry around the homes you own in your back pocket, but you may have a piece of paper with their addresses in there, which lets you find the actual house.

When you say == on objects, you are asking if the paper has the same address written down on both. When you use the equals() method, you are saying "Are the homes at both these addresses logically equal?"

And just to make things more confusing, String literals work slightly differently than everything else, so you can get away with == with those...sometimes...
 
Cid Rodrigues
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:you should only use == for primitives, otherwise it isn't doing what you think it is.

When you say something like

MyObject variableName = new MyObject();

the variableName holds the ADDRESS (more or less) of where the data actually is. Say you are a homebuilder. You don't carry around the homes you own in your back pocket, but you may have a piece of paper with their addresses in there, which lets you find the actual house.

When you say == on objects, you are asking if the paper has the same address written down on both. When you use the equals() method, you are saying "Are the homes at both these addresses logically equal?"

And just to make things more confusing, String literals work slightly differently than everything else, so you can get away with == with those...sometimes...


Thanks for the info man!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!