GeekInterview.com
Series: Subject: Topic:
Question: 112 of 573

Does Java support pass by reference. If yes/no- why?

Asked by: sha_kr2001 | Member Since Dec-2007 | Asked on: Dec 21st, 2007

View all questions by sha_kr2001   View all answers by sha_kr2001

Showing Answers 1 - 14 of 14 Answers

Java does not support pass by reference. Java actually pass-by-value for all variables running within a single VM. pass-by-value means pass-by-variable-value and that means pass-by-copy-of the variable.

It makes no difference if you are passing primitive or reference variable, you always passing a copy of the bits in the variable.

For example: if u are passing the variable with the value 3, you are passing thecopy of variable representing 3. If you are passing an object reference variable, you are passing the copy of the bits representing the reference to an object.

Yes  2 Users have rated as useful.
  
Login to rate this answer.

Yes , java do support pass by reference. when  ever we pass an object to the method it is call by reference only. The changes made for the state of the object inside the method call which is in other object will be reflected in the original object passed in ...

so this nothing but the pass by reference only.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
cricketfan04

Answered On : Feb 7th, 2008

View all answers by cricketfan04

Java is purely pass-by-value only. The confusion is because when an object is passed, the objects value which in this case is the memory allocation is passed. Since the object received in the method has the same memory reference, this seems like it is passed by reference.

  
Login to rate this answer.
sampra

Answered On : Feb 11th, 2008

View all answers by sampra

Java is purely pass-by-value only. The confusion is because when an object is passed, the objects value which in this case is the memory allocation is passed. Since the object received in the method has the same memory reference, this seems like it is passed by reference...........

  
Login to rate this answer.
ramesh124

Answered On : Jun 8th, 2008

View all answers by ramesh124

Java Supports pass by reference.
An object means, the variable which contains the starting location of its class.

When we pass the Object, it is internally getting the address of starting point.

  
Login to rate this answer.
vegetto

Answered On : Jun 9th, 2008

View all answers by vegetto

hi there,
                 the java technology supports a kind of pass by reference but this process applies only to the objects not the primitive data types ,i.e if you are familiar with c++ then you must be knowing that whatever changes you make in the the reference that are reflected back in call by reference, in the same way this happens in java but only in case of the objects.try a  programme in which you instantiate a class and try to modify the primitive data type by calling it from the main method and  kind of same process apply it to the case of objects ,you will find that only in the case of the objects the change occurs.for any further query please ask byeee

Yes  2 Users have rated as useful.
  
Login to rate this answer.
Naveenjavasun

Answered On : Jul 8th, 2008

View all answers by Naveenjavasun

Java always makes a copy of arguments & pass the copy.The called method has a local copy of data.If the the method changes the data it changes the copy, So original value is not changed.
When we pass a prmitive like int ,the method gets its own int value.a copy of original.

When we pass an Object, the value that is copied & set along a referance or pointer to the object.the method gets its own copy of pointer,it does not gets own copy of object.
If method changes the copy of pointer to point to another object the original pointer is not effected.

If the method changes the sme of the attribute of the object,it canges original object.

Yes  1 User has rated as useful.
  
Login to rate this answer.
adurairaj

Answered On : Feb 20th, 2009

View all answers by adurairaj

Java does not support pass by reference.
To test it try the following code

{
myclass z = new myclass();
z.a=100;
test(z);
//the value of z.a will still be 100;
//if call by reference then z.a should be 50

}
public void test(myclass x)
{
x = new myclass();
x.a=50;
}

  
Login to rate this answer.
rhingarajiya

Answered On : Feb 22nd, 2009

View all answers by rhingarajiya

No.

Java uses object by reference.
Java passes object reference to methods by value.

  
Login to rate this answer.
hamidmushtaq

Answered On : Apr 5th, 2009

View all answers by hamidmushtaq

In Java copies of references of Objects are passed. So everything is passed by value. Be it an object reference or a primitive data type. If you modify the object reference in a method, it won't have any effect on the referenced that was passed.

  
Login to rate this answer.

Naveenjavasun is exactly correct.. i tried that..i am sure..

  
Login to rate this answer.
Sarje

Answered On : Aug 18th, 2009

View all answers by Sarje

Java supports pass by value only.
 
In case passed value is of primitive data type it is clear that this is pass by value.

But when we pass any reference variable value then confusion arrises because here changes are refelected in original object. In this case also copy of the reference value is passed but both original feference value and passed reference value denotes to the same object (because copy of the object is not created only copy of the reference is created) so the changes is reflected in original object.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
uncer

Answered On : Jan 17th, 2010

View all answers by uncer

Java does not support pass by reference. Java actually pass-by-value for all variables running within a single VM. pass-by-value means pass-by-variable-value and that means pass-by-copy-of the variable. It makes no difference if you are passing primitive or reference variable you always passing a copy of the bits in the variable.

For example: If you are passing the variable with the value 3 you are passing the copy of variable representing 3. If you are passing an object reference variable you are passing the copy of the bits representing the reference to an object.

Regard
shahid

  
Login to rate this answer.
babusek

Answered On : Dec 22nd, 2010

View all answers by babusek

Java Does not Support Pass by Reference

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.