2

I am recently wondering how functions like the InputStream int read(byte[] b) method are working. I know that Java is always passing method parameters by value, and not by reference.

Knowing this, I don't understand how an empty byte-array can contain values after passing it to the read-method (assuming there were bytes to read in the InputStream). From all I know, only the value passed to the read-method will be touched internally and the only output should be the number of read bytes (the integer return value). But still the byte-array somehow fills.

How is this actually done?

Chris
  • 53
  • 1
  • 5
  • The reference is passed by value, but it's still a reference to the original byte array, so it can be used to fill it. – Kayaman Mar 26 '15 at 06:55

2 Answers2

2

The reference to the byte[] is passed as value. Pass by reference would mean that the address to the variable holding the reference to the byte[] was passed.

Thomas Stets
  • 3,015
  • 4
  • 17
  • 29
0

Objects like arrays are passed by reference. You can think of it as the object variable being a pointer and that pointer being passed by value, which amounts to the same thing. It's only primatives like ints and floats that are really passed by value.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Are the primitive types the only exception that are actually passed by value? – Chris Mar 26 '15 at 07:13
  • @Chirs yes. Though note that for immutable objects like Strings, pass by reference and value are almost equivalent. – Antimony Mar 26 '15 at 15:28
  • Nope. The reference is passed by value, and this is different from pass by reference and from pass by value. – Louis Wasserman Mar 27 '15 at 06:18
  • @Louis What's the difference? I don't think there is any difference at the abstraction level of Java. – Antimony Mar 27 '15 at 15:02
  • @Louis I did mention that it is effectively pointers being passed by value. However, unless you're doing == comparisons, the distinction doesn't really matter. Colloquially people use pass by reference to refer to the style of passing in Java because that is the effect, even if the variables are really pointers. – Antimony Mar 27 '15 at 15:07
  • 1
    @Antimony: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value#comment2096_40523 explains some of the details, but pass by reference supports swapping and Java does not. The distinction is important. – Louis Wasserman Mar 27 '15 at 15:10
  • @Louis you can however swap the content of the objects being pointed to. There really is no difference except for == comparisons. In C++ parlance, this follows from the fact that references are just pointers under the hood and passing a pointer by value is the same thing as passing a reference to the object that pointer points to. The main difference is that Java doesn't let you take references to primitives or pointers, only objects. – Antimony Mar 28 '15 at 00:57
  • @Antimony bringing this back to the OP's question, if `read(byte[])` was actually pass by reference, then the `InputStream` could create a whole new `byte[]` and the `byte[]` reference passed into `InputStream.read` could be changed -- e.g. it could be a new size, for example. Java doesn't allow that, and it's relevant to the OP. – Louis Wasserman Mar 28 '15 at 02:41