Results 1 to 2 of 2
  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.
    Don't forget to smile :-)

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

    Default Re: What is this Output !

    Please don't double post; I'm closing this thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2012, 12:39 PM
  2. Replies: 0
    Last Post: 09-30-2010, 09:16 PM
  3. Need help with output
    By cedric11 in forum New To Java
    Replies: 4
    Last Post: 11-30-2009, 02:09 PM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  5. different output
    By pro85 in forum Java Applets
    Replies: 6
    Last Post: 08-05-2008, 02:42 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
  •