Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By SurfMan

Thread: What is this Output !

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default What is this Output !

    Java Code:
    package passbyvaluepracticetest;
    
    
    public class PassReferenceTypeParamenterByValue {
    	public static void main (String []args){
    		PassReferenceTypeParamenterByValue avien= new PassReferenceTypeParamenterByValue();
    		
    		PointForPassReferenceTypeParametersByValue p = new PointForPassReferenceTypeParametersByValue(2, 3);
    		System.out.println("The reference variable p which is initially pointing to a Point.. object located at (2,3), inside the method moveP() refers to another newly created object of the same Point.. type located at (10,20).\nThe proof is taking the X and Y position of the Point.. object pointed by the reference variable returned by moveP(); i.e. X:" + avien.moveP(p) + "and Y:" + avien.moveP(p) + ".\n" );
    		
    		System.out.println("\nBut due to pass by value, outside moveP(), p still refers to the same object. Proof is X: " + p.xPosition + " and Y: " + p.yPosition + ".");
    	}
    	
    	public PointForPassReferenceTypeParametersByValue moveP(PointForPassReferenceTypeParametersByValue del_p){
    		del_p = new PointForPassReferenceTypeParametersByValue(10, 20);
    		return del_p;
    	}
    }
    
    public class PointForPassReferenceTypeParametersByValue {
    	int xPosition;
    	int yPosition;
    	PointForPassReferenceTypeParametersByValue(int x, int y){
    		xPosition=x;
    		yPosition=y;
    	}
    
    
    }
    OUTPUT: -

    The reference variable p which is initially pointing to a Point.. object located at (2,3), inside the method moveP() refers to another newly created object of the same Point.. type located at (10,20).
    The proof is taking the X and Y position of the Point.. object pointed by the reference variable returned by moveP(); i.e. X:passbyvaluepracticetest.PointForPassReferenceTyp eParametersByValue@2f78743band Y:passbyvaluepracticetest.PointForPassReferenceTyp eParametersByValue@d16e5d6.




    But due to pass by value, outside moveP(), p still refers to the same object. Proof is X: 2 and Y: 3.



    QUESTION: -
    WHAT IS THIS? X:passbyvaluepracticetest.PointForPassReferenceTyp eParametersByValue@2f78743band Y:passbyvaluepracticetest.PointForPassReferenceTyp eParametersByValue@d16e5d6.
    WHAT DO I DO TO GET A PROPER OUTPUT.
    Don't forget to smile :-)

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,078
    Rep Power
    4

    Default Re: What is this Output !

    The output does not match the code. You probably forgot to recompile something.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    720
    Rep Power
    2

    Default Re: What is this Output !

    That is the toString() method of your class PointForPassReferenceTypeParametersByValue. Since you don't have one, it will be the one defined in Object. You see this because you probably used it in some System.out.println() call like this: System.out.println("some long story: " + p); instead of System.out.println("some long story: " + p.xPosition);

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    720
    Rep Power
    2

    Default Re: What is this Output !

    Dammit gimbal, will you give me a chance to answer? I always come second! :)

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,078
    Rep Power
    4

    Default Re: What is this Output !

    But we give different information, so it doesn't matter ;) You were the first to provide YOUR explanation of what is going on, I just stated an observation.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: What is this Output !

    The output does not match the code. You probably forgot to recompile something.
    I just recompiled the whole thing, and it gives the same output.
    Don't forget to smile :-)

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,078
    Rep Power
    4

    Default Re: What is this Output !

    Then the code you're compiling is not the code you posted.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: What is this Output !

    You see this because you probably used it in some System.out.println() call like this: System.out.println("some long story: " + p); instead of System.out.println("some long story: " + p.xPosition);

    Here is the statement in code:

    Java Code:
    System.out.println("... My long story...  X:" + avien.moveP(p) + "and Y:" + avien.moveP(p) );
    So, no I didn't use avien instead of avien.move(p)
    Don't forget to smile :-)

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,078
    Rep Power
    4

    Default Re: What is this Output !

    So what does this avien.moveP() return? It is going to be an object and then you need to read and understand what SurfMan told you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: What is this Output !

    You see this because you probably used it in some System.out.println() call like this: System.out.println("some long story: " + p); instead of System.out.println("some long story: " + p.xPosition);
    Here is the statement in code:

    Java Code:
    1
    System.out.println("... My long story... X:" + avien.moveP(p) + "and Y:" + avien.moveP(p) );



    So, no I didn't use avien instead of avien.move(p)

    Sorry, you were right. It's fixed now. I had used avien.moveP()p instead of avien.moveP(p).xPosition. Thank you so very much.
    Don't forget to smile :-)

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    720
    Rep Power
    2

    Default Re: What is this Output !

    So gimbal was right, this is different from what you posted. Second, you call moveP() twice. This is probably not what you intend to happen. It looks like moveP() returns a new point, so you have to assign that to a new variable and work from there.
    gimbal2 likes this.

  12. #12
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,078
    Rep Power
    4

    Default Re: What is this Output !

    Probably too late for that wisdom to land home :( Oh well, you'll get the next one I'm sure.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. What is this Output !
    By Nazneen Ali in forum New To Java
    Replies: 1
    Last Post: 10-28-2013, 10:55 AM
  2. Replies: 2
    Last Post: 05-03-2012, 11:39 AM
  3. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  4. Can anyone tell me how this output?
    By vysh in forum Advanced Java
    Replies: 16
    Last Post: 05-10-2009, 08:32 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 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
  •