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

Thread: References versus values

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question References versus values

    Comming from C here. I'm trying to figure out when I have a copy of an object and when I have a reference to an object. I tried out this function, and the original values are left un-modified. How would i modify this function

    class SomeUtilClass {
      static public void swap(int a, int b) {
        int c = a;
        a = b;
        b = c;
      }
    }

    What I'm looking for is a java equivalent to this C function:
    void swap(int* a, int* b){
      int c = *a; 
      *a = *b;
      *b = c;
    }

    Of course, I'm really intending to use it for non-primitive types too.


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

    Default Re: References versus values

    You always get copies. With object references (pointers) the reference can be used to update the object it points to. You can not change the value of the original reference or primitive.

    If you pass an array, its contents can be modified. An array is often handled the same as an object.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: References versus values

    This thread discussed the topic and provided several good outside links to additional material that I think will help you understand Java better in this area.

    Bottom line: There is no equivalent to pointers in Java. Even so, there are, of course, ways to write a swap method. Come back when you've absorbed what Java is (or isn't) and want to discuss further.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: References versus values

    So if there are no references and everything is a copy, then I'm quite confused regarding how some of my other applications are working.

    Working with GUIs I often do this:
    JButton button = new JButton("OK");
    button.addActionListener(this);

    When I hit "OK", the "addActionListener(ActionEvent e)" method of my parent JFrame is called and the member variables are manipulated. Am I actually just constantly making new copies and manipulating those? I thought that I was manipulating the original object.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: References versus values

    When I hit "OK", the "addActionListener(ActionEvent e)" method of my parent JFrame is called
    Technically, the addActionListener() method was called as the GUI was being built, before it was displayed or made visible. Selecting the "OK" button runs the actionPerformed() method of the attached ActionListener class, which in the case you provided is the current instance of 'this'. Is 'this' a reference? Of course, a symbolic one, and one we can't use that to derive a pointer as one can in the C languages.
    Am I actually just constantly making new copies and manipulating those?
    That's probably an over generalization or simplification, so I'll say "No," but show some examples of what you have in mind. And in this sentence and the next, I'm not sure what you mean by 'manipulated/ing." Try to use a more precise term.

Similar Threads

  1. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  2. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  3. Replies: 4
    Last Post: March 21st, 2013, 02:21 PM
  4. Web service references
    By Eggbert in forum Java IDEs
    Replies: 3
    Last Post: October 25th, 2011, 06:46 AM
  5. what is benefit of JWMS architect versus MVC architect ?
    By david.opensource in forum Java Theory & Questions
    Replies: 0
    Last Post: July 4th, 2010, 03:08 PM