Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Isn't this hashCode method self-referential/recursive?

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Isn't this hashCode method self-referential/recursive?

    I'm learning about overriding equals() and hashCode().
    In one of the articles there is this Car class and in it the hashCode() method is being overridden:
    @Override
    public int hashCode() {
       int result = model == null ? 0 : model.hashCode();
       result = result + manufactureYear;
       result = result + dollarPrice;
       return result;
    }

    My question is: what happens when the model.hashCode() is called?
    It seems to me that it calls the same hashCode() to check if model.hashCode() is equal - so it's recursive.
    But apparently it works. So what happens there.
    Last edited by NimbleSpeaker; January 26th, 2022 at 11:34 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Isn't this hashCode method self-referential/recursive?

    Where is the declaration for the class that is referred to by the variable model?
    What class is the posted code in?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Isn't this hashCode method self-referential/recursive?

    Here's what's in the article:
    The Car class:
    public class LuxuryAuto {
     
       private String model;
       private int manufactureYear;
       private int dollarPrice;
     
       public LuxuryAuto(String model, int manufactureYear, int dollarPrice) {
           this.model = model;
           this.manufactureYear = manufactureYear;
           this.dollarPrice = dollarPrice;
       }
     
       // ...getters, setters, etc.
    }

    Then in the main class:
           LuxuryAuto ferrariGTO = new LuxuryAuto("Ferrari 250 GTO", 1963, 70000000);
           LuxuryAuto ferrariSpider = new LuxuryAuto("Ferrari 335 S Spider Scaglietti", 1963, 70000000);
     
           System.out.println("What are their hash codes?");
           System.out.println(ferrariGTO.hashCode());
           System.out.println(ferrariSpider.hashCode());

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Isn't this hashCode method self-referential/recursive?

    Ok so model is a String. It has its own hashCode method.

    What class is your hashCode method in? It obviously is not in a class that extends String.
    So how is there any conflict? Where is the recursion?
    What is your confusion?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    NimbleSpeaker (January 26th, 2022)

  6. #5
    Junior Member
    Join Date
    Jan 2022
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Isn't this hashCode method self-referential/recursive?

    Oh, God. Now I get, it clicked!
    So, it calls the String class's hashCode().
    But if I don't override the method in the LuxuryAuto class and call the method:
    System.out.println(ferrariGTO.hashCode());

    Will it call Object class's hashCode() method?

    Thanks, man!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Isn't this hashCode method self-referential/recursive?

    it call Object class's hashCode() method
    Yes
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    NimbleSpeaker (January 26th, 2022)

Similar Threads

  1. Replies: 4
    Last Post: December 9th, 2013, 10:40 AM
  2. How to create this recursive method.
    By exodus2041 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 10:26 AM
  3. Help in understanding this recursive method
    By jameschristopher06 in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 20th, 2012, 01:23 PM
  4. help with recursive method
    By mflb94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 06:30 PM
  5. Problem with recursive method. Can you help?
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 7th, 2011, 05:44 PM

Tags for this Thread