GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 459 of 502    Print  
Does Java support pass by reference. If yes/no- why?

  
Total Answers and Comments: 12 Last Update: August 18, 2009     Asked by: sha_kr2001 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: priyaranjan_BISWAL
 
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 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.

Above answer was rated as good by the following members:
interviewprep9, Naveenjavasun, madhoo_09, swpnl_ptl, sujithkthomas
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
January 03, 2008 01:30:27   #1  
priyaranjan_BISWAL Member Since: August 2007   Contribution: 2    

RE: Does Java support pass by reference. If yes/no- why?
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 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.

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 1Overall Rating: +1    
January 09, 2008 00:37:27   #2  
keshavgopal.se Member Since: December 2007   Contribution: 5    

RE: Does Java support pass by reference. If yes/no- why?
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.

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 1Overall Rating: +1    
February 07, 2008 10:24:14   #3  
cricketfan04 Member Since: February 2008   Contribution: 2    

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

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.


 
Is this answer useful? Yes | No
February 11, 2008 06:02:51   #4  
sampra Member Since: February 2008   Contribution: 278    

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

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...........


 
Is this answer useful? Yes | No
June 08, 2008 07:27:25   #5  
ramesh124 Member Since: June 2008   Contribution: 2    

RE: Does Java support pass by reference. If yes/no- why?
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.

 
Is this answer useful? Yes | No
June 09, 2008 11:29:42   #6  
vegetto Member Since: June 2008   Contribution: 10    

RE: Does Java support pass by reference. If yes/no- why?
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

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 08, 2008 03:38:53   #7  
Naveenjavasun Member Since: July 2008   Contribution: 6    

RE: Does Java support pass by reference. If yes/no- why?
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.

 
Is this answer useful? Yes | No
February 20, 2009 05:35:08   #8  
adurairaj Member Since: February 2009   Contribution: 1    

RE: Does Java support pass by reference. If yes/no- why?
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;
}

 
Is this answer useful? Yes | No
February 22, 2009 13:43:11   #9  
Ravi.Hingarajiya Member Since: February 2009   Contribution: 8    

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

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

 
Is this answer useful? Yes | No
April 05, 2009 06:46:07   #10  
hamidmushtaq Member Since: April 2009   Contribution: 1    

RE: Does Java support pass by reference. If yes/no- why?
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.
 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape