Hello everyone and sorry for the caps...just wanted to emphasize the "vs"!!
As with many people this is my first post here and i will immediately cut to the chase!
--When comparing integers in java, we use the boolean:
==
Correct?
--When comparing strings though, the right way to do it is by using the:
.equals
Can you explain me why exactly is this happening? - I have heard that it has to do with referencing in memory in the first case and checking of every single element in the second one! I am completely confused though...
My thinking is this:
1st case integer comparison:
int number1 = "5";
int number2 = "5";
if (number1==number2) // I suppose this returns TRUE...
2nd case string comparison:
String letter1 = "hello";
String letter2 = "hello";
if (letter1==letter2) // This is going to return false! - WHY?
It's exactly the same value! -- I am also aware that strings are objects and have differen memory slots..
If this is true then what variables really are? Don't they have different storage locations too??
Anyway, I'm completely lost here..Please help me sort this out!
Thanks in advance for your time!
Edited by: konos5 on Oct 31, 2008 10:40 AM
Edited by: konos5 on Oct 31, 2008 10:41 AM
Edited by: konos5 on Oct 31, 2008 10:42 AM