Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

An array problem

 
Jan Tang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys.that makes sense to me.just one more question:Is integer an object either?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pass by reference


Well, more precisely pass by copy of the value of the reference.
 
John Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the reference is passed by value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic