5 Replies - 4173 Views - Last Post: 02 January 2011 - 01:34 AM Rate Topic: -----

#1 mittu.thefire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-December 10

call by reference

Posted 01 January 2011 - 09:13 AM

I did programs in C and C++ programs by using pointers (call by reference)..
but same programs i want to do using JAVA ...
please any one can help me about "how to implement call by reference in Java"
Is This A Good Question/Topic? 0
  • +

Replies To: call by reference

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Re: call by reference

Posted 01 January 2011 - 09:56 AM

You can't. Everything is passed by value in Java. For Objects, the value is their the location in memory. So if you assign one Object to another, they will share the same location in memory. This produces the effect of passing by reference.
Was This Post Helpful? 1
  • +
  • -

#3 mittu.thefire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-December 10

Re: call by reference

Posted 01 January 2011 - 10:02 AM

there is no difference between 'call by value' and 'call by reference'... Is it??
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: call by reference

Posted 01 January 2011 - 10:03 AM

I would add on to that explanation that the object reference is passed by value, thus changes to the object's contents persist, while a reassignment of that object reference does not.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2860
  • View blog
  • Posts: 10,964
  • Joined: 15-July 08

Re: call by reference

Posted 01 January 2011 - 04:58 PM

To further show KYAs point, I wrote a program where the value of an object is reassigned in the method bodies:
public class Tester {
	public static void main(String[] args) {
		Integer bi = new Integer("1");
		System.out.println(bi);
		reassignMe(bi);
		System.out.println(bi);
		changeMe(bi);
		System.out.println(bi);
		
	}

	private static void changeMe(Integer bi) {
		bi += 12;		
	}

	private static void reassignMe(Integer bi) {
		bi = Integer.valueOf(42);
	}
}



However, all three times, the Integer stays 1. However, if you passed an object that could change it's internal state, that change persists.
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: call by reference

Posted 02 January 2011 - 01:34 AM

following on from the examples already give the following uses the Java Point class which represents an (x,y) coordinate in 2D space
http://download.orac.../awt/Point.html

it demonstrates how the data members of a Point object (passed by value) can be modified by a method
  public static void main (String args[]) {
    Point one = new Point(1, 1);
    System.out.println("initial point " + one.toString());  
    assign1(one);
    System.out.println("after assign1 point " +one.toString()); 
    assign2(one);
    System.out.println("after assign2 point " +one.toString());  
    }

  // parameter point is assigned a reference to a new object
  //   i.e. the Point object in calling program is not changed
  static void assign1(Point point)
    {
     point = new Point(4, 5);
     System.out.println("in assign1 " + point.toString());   
    }

  // the x and y members of object referenced by parameter point are changed
  //   i.e. the (x,y) of the Point object in calling program is changed
  static void assign2(Point point)
    {
     point.x = 4; point.y = 5;
     System.out.println("in assign2 " + point.toString());   
    }


a run of the program gave
initial point java.awt.Point[x=1,y=1]
in assign1 java.awt.Point[x=4,y=5]
after assign1 point java.awt.Point[x=1,y=1]
in assign2 java.awt.Point[x=4,y=5]
after assign2 point java.awt.Point[x=4,y=5]


This post has been edited by horace: 02 January 2011 - 01:35 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1