Results 1 to 4 of 4
  1. #1
    naved is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default . confuse that how actually reference is passed to a method ?

    i know this code is example of pass by value , so
    how to make the same code as pass by reference ?
    also explain how and what happens when you alter the same code as i new bie to java and confuse that how actually reference is passed to a method ?

    Java Code:
    class sample
    {
    	 int  value = 60;
    	void changeVal(int val)
    	{
    		val=19;
    		System.out.println("inside method" +val);
    	}
    	
    
    	public static void main(String[] args)
    	{
    		int val = 10;
    		sample s ;
    		s = new sample(50);
    		
    		
    		//System.out.println(" in main method " + s.value);
    		//System.out.println(" in main method " + s1.value);
    		s.changeVal(val);
    		System.out.println(" in main method " + val);
    		
    		
    	}
    }

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    You can't pass primitive values by reference in Java. You'll have to use objects instead.

    Java Code:
    public class A {
    
    	public static void main(String[] args) {
    		IntWrapper i = new IntWrapper();
    		i.val = 1;
    		addFive(i);
    		System.out.println(i.val);
    	}
    	public static void addFive(IntWrapper i) {
    		i.val += 5;
    	}
    }
    
    class IntWrapper {
    	int val;
    }
    Note that this won't work with the built-in Integer class because of auto-unboxing. If you were to use Integer instead of IntWrapper here, the expression i += 5 would create a new Integer with value equal to i+5, and then assign a reference to the new Integer to the local variable i. The original Integer still exists at its original location with an unchanged value.
    Last edited by Iron Lion; 07-16-2011 at 06:28 PM.

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

    Default

    You can't pass primitive values by reference in Java.
    You can't pass any values by reference in Java. All passing is by value. Primitives are passed by value. References are passed by value.

    There's a very good anecdotal explanation here:
    JavaRanch Campfire - Cup Size: a Story About Variables
    JavaRanch Campfire - Pass By Value, Please

    Make sure to read both articles, in the correct order.

    db

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

Similar Threads

  1. hiding passed parameters using window.location.href = 'params' method
    By kulangotski in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-01-2011, 12:49 PM
  2. Replies: 3
    Last Post: 06-14-2011, 09:35 PM
  3. Method, returning reference to an object
    By Saletra in forum New To Java
    Replies: 3
    Last Post: 08-23-2010, 08:22 PM
  4. entities are passed by value or passed by reference
    By syntrax in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 07:13 AM
  5. Passing Class Reference to method
    By nekt in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2009, 05:08 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
  •