Results 1 to 6 of 6

Thread: Simple java (bluej) questions

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    2

    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?

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Simple java (bluej) questions

    == checks to see if both strings are the same object. .equals() compares the value.


    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    2

    Re: Simple java (bluej) questions

    Hey man, thanks for the quick reply:

    so statement 1 is right and statement 2 is wrong?

    greetz

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    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
    Last edited by lunchboxtheman; Jan 26th, 2007 at 06:43 PM.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Simple java (bluej) questions

    I believe statement2 being wrong for any given object would be a violation of the contract for equals().
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width