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 4 of 4

Thread: Trying to understand how these two types of code work?

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

    Default Trying to understand how these two types of code work?

    Well a little background I am starting to get back into coding, so the language I am using is Java. I cam across this website called Hacker Rank, which give different problems to solve. One of them is dealing with Lists, which I have never encountered until now. So I started looking at different places to try and how to solve the problem. I ran into this piece of code:
      // Overriding compare()method of Comparator 
                            // for descending order of cgpa
                public int compare(Student s1, Student s2) {
                    if (s1.cgpa < s2.cgpa)
                        return 1;
                    else if (s1.cgpa > s2.cgpa)
                        return -1;
                                    return 0;
                    }
    I am trying to in the else if statement two integers are sent. I am trying to understand why send -1 and then 0, instead just 0 by itself. I just figured that by sending the lower integers is to make sure that it is sorted appropriately (correct me if I am wrong on my thinking).

    The second issue I am having is the comparing using subtracting. I am trying to understand how that work?
    Last edited by archangl1; February 8th, 2022 at 12:13 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to understand how these two types of code work?

    Look at the API doc for the compare method of the Comparator interface. It explains what values the method should return.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Trying to understand how these two types of code work?

    Quote Originally Posted by Norm View Post
    Look at the API doc for the compare method of the Comparator interface. It explains what values the method should return.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Ok no prob. Thanks.

  4. #4
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Trying to understand how these two types of code work?

    This is a case of the code not being formatted very well. The last line 'return 0;' is not inside the if/else blocks.
    Because the if and the else are not using {}, they are single line statements.
    If the first condition is true 1 is returned.
    If the second condition is true -1 is returned.
    Otherwise 0 is returned.
    Here is what it looks like if it is aligned more clearly.
    // Overriding compare()method of Comparator 
    // for descending order of cgpa
    public int compare(Student s1, Student s2) {
        if (s1.cgpa < s2.cgpa) return 1;
        else if (s1.cgpa > s2.cgpa) return -1;
        return 0;
    }
    or with {} to better show it:
    public int compare(Student s1, Student s2) {
        if (s1.cgpa < s2.cgpa) {
            return 1;
        }else if (s1.cgpa > s2.cgpa) {
            return -1;
        }
        return 0;    // this is returned when the items are equal
    }

Similar Threads

  1. ArrayList.contains method does not work on user-defined data types
    By semaphore in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 1st, 2014, 06:50 AM
  2. I dont understand this code ?
    By ammehmeti in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 22nd, 2014, 07:57 AM
  3. Replies: 2
    Last Post: March 6th, 2014, 05:35 AM
  4. Replies: 10
    Last Post: December 14th, 2013, 12:12 PM
  5. Help me understand this code?
    By Farkuldi in forum Java Theory & Questions
    Replies: 11
    Last Post: November 12th, 2013, 03:37 PM