Results 1 to 16 of 16
Thread: java-object passing by value
- 01-01-2012, 12:37 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
java-object passing by value
Java only follow 'pass by value'..
But not the pass by refference like in c,c++..
In c,c++ we can pass value to other functions by pass by value and pass by address(rfference)..
But in java only pass by value iz pozible..
Any explanation abou it??a detail or a programme illustrating it..
- 01-01-2012, 01:06 PM #2
Re: java-object passing by value
But in java only pass by value is possible.
- 01-01-2012, 09:56 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: java-object passing by value
C is also pass by value.
For Java you might like to read
JavaRanch Campfire - Cup Size: a Story About Variables
JavaRanch Campfire - Pass By Value, Please
- 01-02-2012, 08:07 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
Re: java-object passing by value
Guys,
I think pass by value and pass by reference , both possible in Java.
Changes that occurred due to pass by reference will get reflected outside the method. But in case of pass by value changes wont reflected outside the loop.
Please corect me If i am wrong.
If u need any sample code, then please do let me know
- 01-02-2012, 09:33 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: java-object passing by value
When people speak of pass by value in Java they are referring to the fact that there will be no change to the value of a variable used as an argument that the caller can see.But in case of pass by value changes wont reflected outside the loop.
Please corect me If i am wrong.
In both cases "true" will be printed. That's because the values of foo and foo2 do not change and cannot change whatever the methods are. If they were equal to bar and bar2 then they will remain so. Methods do not and cannot change the value of variables. That's because a copy of the value of the variable is passed to the method, and the method sees only this value. Java is pass by value.Java Code:int bar = 42; int foo = bar; someMethod(foo); System.out.println(foo == bar); // prints "true" because foo still has the same value Point bar2 = new Point(42, 666); Point foo2 = bar2; someOtherMethod(foo2); System.out.println(foo2 == bar2); // prints "true" because foo2 still has the same value.
Of course, lots of other things might change when you call a method. Otherwise methods would be somewhat useless. But the value of variables won't change because the method is only passed a copy of this value.
-----
The links I posted above are well worth reading.
- 01-03-2012, 01:54 AM #6
Re: java-object passing by value
Nope, pbrockway2 is correct. Some people might say java is pass by reference when dealing with objects, since you can 'pass' them to methods and it modifies the original, however, java is still passing by value.I think pass by value and pass by reference , both possible in Java.
- 01-03-2012, 05:24 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: java-object passing by value
@quad64bit-----can we say java is both pass by value and pass by refference
- 01-03-2012, 05:32 PM #8
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: java-object passing by value
Plz post the sample code...
- 01-03-2012, 05:38 PM #9
Re: java-object passing by value
@aumkar0406
Nope! because it isn't. It is purely pass by value.can we say java is both pass by value and pass by refference
Java is Pass-by-Value, Dammit! - Scott Stanchfield
- 01-03-2012, 05:56 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: java-object passing by value
This topic shows up again and again, doesn't it?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-03-2012, 06:07 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
- 01-03-2012, 06:17 PM #12
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: java-object passing by value
As far as I know variables are saved in the stack.
Everything that is saved in the stack is copied when it passed as a parameter to a method.
But objects(saved in the heap) are not copied and every edit that is done in the method will be vaild after that.
Arrays for example, send them through a method, change a value and you will see that the value has changed after the method.
If I didn't accurate in something correct me :)
- 01-03-2012, 07:29 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 01-03-2012, 07:31 PM #14
Re: java-object passing by value
That's a bit above stock and taps and dies.I'm too low level; nuts and bolts
- 01-03-2012, 08:04 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 01-04-2012, 02:08 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
Error Passing Object through a constructor
By mpankhurst in forum New To JavaReplies: 9Last Post: 12-15-2011, 10:12 AM -
passing an object from one JDialog to Jframe
By sinha in forum AWT / SwingReplies: 7Last Post: 12-16-2009, 04:56 PM -
Passing SWT object to another class
By Vinaya Lal Shrestha in forum SWT / JFaceReplies: 0Last Post: 04-03-2009, 01:50 PM -
passing object as value for checkbox values??
By Pooja Deshpande in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-17-2009, 11:55 AM -
Passing a Vector object to a function
By evapisces in forum New To JavaReplies: 4Last Post: 09-27-2008, 03:18 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks