Results 1 to 3 of 3

Thread: pointer result

  1. #1
    willy83 is offline Member
    Join Date
    May 2015
    Posts
    1
    Rep Power
    0

    Default pointer result

    Hi guys

    I am new in Java and also object oriented programming

    Following is an example. I couldn't understand why last line of the print is different then the preceding one. Could you please explain why the latest line is x=0,y=0 but not x=5,y=10

    Regards

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package calling.methods;
    import java.awt.Rectangle;

    /**
    *
    * @author 502325400
    */
    public class pointerm {
    public static void main(String[] args)
    { go();
    }

    public static void go()
    { Rectangle r1 = new Rectangle(0,0,5,5);
    System.out.println("In method go. r1 " + r1 + "\n");
    // could have been
    //System.out.prinltn("r1" + r1.toString());
    r1.setSize(10, 15);
    System.out.println("In method go. r1 " + r1 + "\n");
    alterPointee(r1);
    System.out.println("In method go. r1 " + r1 + "\n");
    alterPointer(r1);
    System.out.println("In method go. r1 " + r1 + "\n");
    }

    public static void alterPointee(Rectangle r)
    { System.out.println("In method alterPointee. r " + r + "\n");
    r.setSize(20, 30);
    System.out.println("In method alterPointee. r " + r + "\n");
    }

    public static void alterPointer(Rectangle r)
    { System.out.println("In method alterPointer. r " + r + "\n");
    r = new Rectangle(5, 10, 30, 35);
    System.out.println("In method alterPointer. r " + r + "\n");
    }


    }


    -----------
    Results

    In method go. r1 java.awt.Rectangle[x=0,y=0,width=5,height=5]

    In method go. r1 java.awt.Rectangle[x=0,y=0,width=10,height=15]

    In method alterPointee. r java.awt.Rectangle[x=0,y=0,width=10,height=15]

    In method alterPointee. r java.awt.Rectangle[x=0,y=0,width=20,height=30]

    In method go. r1 java.awt.Rectangle[x=0,y=0,width=20,height=30]

    In method alterPointer. r java.awt.Rectangle[x=0,y=0,width=20,height=30]

    In method alterPointer. r java.awt.Rectangle[x=5,y=10,width=30,height=35]

    In method go. r1 java.awt.Rectangle[x=0,y=0,width=20,height=30]

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: pointer result

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

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: pointer result

    Woohoo, we were missing this question this week :)

    The short answer is:

    1. go() has a variable 'r1' which points to a Rectangle object where X,Y = 0,0
    2. alterPointee() has a variable 'r' which initially contains that same Rectangle
    3. alterPointee() creates a completely new Rectangle where X,Y = 5,10
    4. alterPointee() assigns that new rectangle with X,Y = 5,10 to it's own variable 'r'.

    At this moment in time, alterPointee()'s r = Rectangle(5,10) and go()'s r1 = Rectangle(0,0) . r1 did NOT automagically start pointing to another object just because you assigned a new Rectangle to 'r'.


    There are many other people with the exact same confusion, like these threads:

    http://www.java-forums.org/new-java/...s-objects.html

    http://www.java-forums.org/new-java/...ing-wrong.html

    If you click around enough you'll even see pictures to illustrate it.


    More reading:

    http://www.javaranch.com/campfire/StoryCups.jsp
    http://www.javaranch.com/campfire/StoryPassBy.jsp
    Last edited by gimbal2; 05-22-2015 at 01:44 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Getting a null pointer exception when loading my JSP result page
    By rc1138 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-13-2011, 02:23 PM
  2. After End Of Result set error?
    By mrhid6 in forum JDBC
    Replies: 14
    Last Post: 10-09-2011, 05:03 PM
  3. Keep getting result of 0.
    By dookie1293 in forum New To Java
    Replies: 7
    Last Post: 06-09-2011, 05:01 AM
  4. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 10:15 AM

Posting Permissions

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