Simple java (bluej) questions
(I am Dutch so it can be hard to understand :))
1:
Are these two statements wrong or right? :
statement1: if (s1.equals (s2)) is true, is (s1 == s2) also true?
statement2: if (s1 == s2) is true, is (s1.equals (s2)) also true?
s1 and s2 are 2 objects with type String
2:
What are the rules for the expression behind a return statement.
3:
How can it be that when you call a method, there can be more then 1 method with the same name within one Class. Hoe can you see the difference between these two?
Re: Simple java (bluej) questions
== checks to see if both strings are the same object. .equals() compares the value.
Quote:
How can it be that when you call a method, there can be more then 1 method with the same name within one Class. Hoe can you see the difference between these two?
return type and parameter list can be different.
Re: Simple java (bluej) questions
Hey man, thanks for the quick reply:
so statement 1 is right and statement 2 is wrong?
greetz
Re: Simple java (bluej) questions
Yes.
As for question 2, the only rule is that the result is implicitly convertible to the return type of the containing method.
Regarding question 3, although the return type can be different in overloads, it is not part of the function signature and thus cannot be a distinguishing part of the overloading. In other words, you cannot have two methods that are identical except for their return type.
Re: Simple java (bluej) questions
Quote:
Originally Posted by eddiemcm
Hey man, thanks for the quick reply:
so statement 1 is right and statement 2 is wrong?
greetz
You have it backwards.
"statement1: if (s1.equals (s2)) is true, is (s1 == s2) also true?"
I can have two Strings that contain the String "Lunchboxtheman" but that are both references to different objects, so this statement is false.
"statement2: if (s1 == s2) is true, is (s1.equals (s2)) also true?"
Assuming you are talking about Strings here, this statement is true. (s1 == s2) checks to see if s1 and s2 point to the same object, hence they contain the same data.
EDIT: spelling
Re: Simple java (bluej) questions
I believe statement2 being wrong for any given object would be a violation of the contract for equals().