File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes An array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "An array problem" Watch "An array problem" New topic
Author

An array problem

Jan Tang
Greenhorn

Joined: Sep 23, 2003
Posts: 9


I thought the output should be 213639,however it's 639639.I'm really confused on this,could anyone please show me a hint?thanks.
John Simpson
Ranch Hand

Joined: Nov 28, 2005
Posts: 30
Hi,

It's because arrays are objects. Java always uses pass by value. The tricky bit is that in the case of an object the value it passes is a copy of the reference to the object.

John
aslam parveez
Ranch Hand

Joined: Apr 25, 2006
Posts: 47
When you pass an array as an argument to a function then it's a pass by reference. So what ever the changes you'll do in the function are reflected in the caller function also.

Now in your code in function "treble" first line is

int [] b =a; // assigning the refrence of a to be.

Now what ever u'll do with b is as good as doing with a , bcs both a and b will have the same refrence to an array.
Jan Tang
Greenhorn

Joined: Sep 23, 2003
Posts: 9
thanks guys.that makes sense to me.just one more question:Is integer an object either?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
pass by reference


Well, more precisely pass by copy of the value of the reference.
John Simpson
Ranch Hand

Joined: Nov 28, 2005
Posts: 30
An int is a primitive, not an object.
The wrapper class Integer is an object. (I think, haven't looked that up.)

John
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
An int is a primitive, however in 1.5, autoboxing can automatically wrap it inside an Integer which is an object.
[ June 19, 2006: Message edited by: Keith Lynn ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"Jiaoziren,"

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name. For example, your previous display name of Jayne Tang.

You can edit your name here. Thank you for your prompt attention!

-Marc


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Campbell Ritchie:


Well, more precisely pass by copy of the value of the reference.


Which is a convoluted way of saying it's pass by value. Everything is passed by value, period.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
Originally posted by Ken Blair:


Which is a convoluted way of saying it's pass by value. Everything is passed by value, period.


Problem is, if you just tell people it's "pass by value", they will be confused if you don't remind them that it's the reference being passed by value, not the object. A lot like passing a pointer by value in C++.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Yes, the reference is passed by value.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: An array problem