Results 1 to 10 of 10
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default misunderstanding the dot operator

    I have a missing link in understanding the dot operator
    (though I do use the operator in most cases correctly).

    the method:
    int compare (int a, int b) {
    return a - b;
    }

    I do understand fully.

    But imagine I would like to implemented something similar concerning the
    compareTo method (so it would give me a similar result):

    int a.compareTo (int b) {
    //??????????
    }

    How would I implement this method? I mean to say I can get hold of "b" easily,
    because "b" enters the method through the parameter definition (int b),
    similar to the above example, right!
    But how do I get hold of "a"?

    Something similar happens with "this.equals(that)". I understand that the dot says:
    envoke the method equals(that) on "this", but how would be the inner working of that method:
    obj equals(obj o).

    Sure I am using these methods already for some time but I do not understand the inner mechanics of them?

    I hope I made myself clear…. there might be some other misunderstanding or better misinterpreting of the above matter?

    Any help welcome please

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: misunderstanding the dot operator

    When doing Comparable, you're not calling it on an int but rather on the object of a reference class that implements the interface. The "a" portion is the object itself, the "this" that is calling the method. So for instance, if you have:

    Java Code:
    public class Foo implements Comparable<Foo> {
      private int value = 0;
    
      public Foo(int value) {
        this.value = value;
      }
    
      // compareTo would look like:
      public int compareTo(Foo other) {
        if (value > other.value) {
           return 1;
        } else if (value < other.value) {
           return -1;
        } else {
           return 0;
        }
      }
    }
    and could be called like:

    Java Code:
    Foo fooA = new Foo(10);
    Foo fooB = new Foo(100);
    
    System.out.println("fooA.compareTo(fooB): " + fooA.compareTo(fooB));

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: misunderstanding the dot operator

    Thanks fubarable, the light is starting to shine.
    A method is either part of a class, which means it is fully implemented, or it is part of an interface, which means it is not implemented at all.
    Interesting enough is that both compareTo and compare are "interface-methods" of the interfaces Comparable and Comparator , and therefor designed differently, right? The "int" preceding compare and compareTo is the return value, notting less or more.

    Concerning the dot operators one usually finds:
    (1) when you are connecting an object reference variable to a method. The Extending Existing Classes lesson introduces the dot operator for this use, and
    (2) when you are connecting a class name to one of its static fields.

    So the a.compareTo(b) would fall into the first, right? Or would there be a third point?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: misunderstanding the dot operator

    Quote Originally Posted by willemjav View Post
    Thanks fubarable, the light is starting to shine.
    A method is either part of a class, which means it is fully implemented, or it is part of an interface, which means it is not implemented at all.
    Interesting enough is that both compareTo and compare are "interface-methods" of the interfaces Comparable and Comparator , and therefor designed differently, right? The "int" preceding compare and compareTo is the return value, notting less or more.

    Concerning the dot operators one usually finds:
    (1) when you are connecting an object reference variable to a method. The Extending Existing Classes lesson introduces the dot operator for this use, and
    (2) when you are connecting a class name to one of its static fields.

    So the a.compareTo(b) would fall into the first, right? Or would there be a third point?
    Yep, that is correct; semantically l.r needs l to be an object reference (*) and r needs to be a member of class l. A member can either be a method or a variable.

    kind regards,

    Jos

    (*) a class itself is also an object of type/class Class (see its API documentation).
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: misunderstanding the dot operator

    What still puzzles me is the "other.value" format at
    if (value > other.value)
    why not simply:
    if (value > other) ?
    You see I, do not understand what "other.value"
    presents really.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: misunderstanding the dot operator

    Ask yourself what other is.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: misunderstanding the dot operator

    other is a parameter of the type Foo, right?
    or in others words a variable that passes into the method?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: misunderstanding the dot operator

    Why not place the following as the first statements of your compareTo method?

    Java Code:
    System.out.println("Object References: " + this + "  " + other);
    System.out.println("Instance fields: " + this.value + "  " + other.value);
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: misunderstanding the dot operator

    aha I see what you mean:
    Object References: willemlearningjava.Foo@199a0c7c willemlearningjava.Foo@50a9ae05
    Instance fields: 1000 100
    fooA.compareTo(fooB): 1

    ok first this is ref to FooA
    other is ref to FooB
    (the weird stuff is the address?)


    Java Code:
    package willemlearningjava;
    
    
    public class Foo implements Comparable<Foo> {
      private int value = 0;
    
      public Foo(int value) {
        this.value = value;
    
      }
    
      public static void main(String[] args)  {
            Foo fooA = new Foo(1000);
            Foo fooB = new Foo(100);
            System.out.println("fooA.compareTo(fooB): " + fooA.compareTo(fooB));
      }
    
      // compareTo would look like:
      public int compareTo(Foo other) {
          System.out.println("Object References: " + this + "  " + other);
          System.out.println("Instance fields: " + this.value + "  " + other.value);
    
        if (value > other.value) {
           return 1;
        } else if (value < other.value) {
           return -1;
        } else {
           return 0;
        }
      }
    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: misunderstanding the dot operator

    Quote Originally Posted by willemjav View Post
    (the weird stuff is the address?)
    Yes, but via the hashCode method. So if you overrode hashCode but didn't override toString(), it would use the new hashCode method as part of the default toString(). Of course, that is irrelevant to the issue at hand. But references are similar (at least to me) to pointers in C in terms of referring to something, be it an object in Java, or a structure member in C.

    Regards,
    Jim
    Last edited by jim829; 07-06-2013 at 09:54 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. probably a principle misunderstanding of OOP?
    By willemjav in forum AWT / Swing
    Replies: 8
    Last Post: 04-05-2013, 12:57 AM
  2. {} after new operator, what does it mean?
    By willemjav in forum New To Java
    Replies: 3
    Last Post: 01-29-2013, 09:40 AM
  3. Use of this operator
    By anjibman in forum New To Java
    Replies: 2
    Last Post: 09-15-2012, 11:22 PM
  4. Replies: 5
    Last Post: 01-21-2012, 11:02 PM
  5. Misunderstanding of Break
    By Steve Whalen in forum New To Java
    Replies: 6
    Last Post: 10-22-2008, 06:31 PM

Posting Permissions

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