Hello, new to this forum and quite new to Java, have a bit of C++ experience.
I wonder why this is not working:
I think the code and what's it intended to do should be pretty clear, if not please ask.
Here's the console output I get:
Seems to me the bubbleSort() method is working on a copy of the passed array (as the array is unchanged afterwards) ?
In C++ I probably would have passed a pointer to an int array, how is this in Java ? (if that's the mistake) ?
Dennis Schroeder wrote: . . .
Because array is passed by reference, . . .
No, There is no such thing as pass by reference in Java®. All values are passed by value, whether reference types or primitives. Some languages do support pass by reference, but not Java®.
What you are passing is a mutable reference type, so you are not altering the reference passed. You are altering the state of the object behind that reference. Try modifying your method like this:-Now print out the original array and the value returned from that methodYou can read about that toString method here.
posted 3 years ago
Campbell Ritchie wrote:(Whole of last post)
Thanks for clarifying and giving input, appreciated.
I've done what you suggest and I get this on the console:
If it would have been passed by reference, the original passed in array would have also been altered, that's what you wanted to show me I assume ?
which further clarifies it.
So to summarize, Java passes pointers to objects ("adresses to objects", hence "pass by value") to functions but these are called Object references in Java speech, is this correct ?