简体   繁体   中英

How could InputStream.read(byte) method ever work?

I have question about one method of InputStream class, because it doesn't seem to me it could ever work.

Let we have something like this:

InputStream is;
byte[] b = new byte[64];

is.read(b);
// and now the byte array b contains data comming through InputStream???

I would understand if usage of the .read() method would look something like this:

b = is.read();

Because the read method would be returning byte array.

But how can the real method write something to its argument and make it visible outside of itself?

It's like I would have this:

String myString = "myText";

public void myMethod(String s) {
  s = "abc123";
}

myMethod(myString);
// and now is the content of myString equal to "abc123" instead of "myText" ???
// ANSWER: no!

Thanks for your replies.

Everything except primitives types are objects in java(including array). The objects are passed by copy of reference from one method to another. So if the called method makes changes to the object passed to it, method is eventually making changes to the same object which was passed to it. Hence, changes are reflected to the calling method as well.

You need to learn about objects and how are they passed between method calls to understand this in detail. Please refer this link for better understanding.

Because the read method would be returning byte array.

Eh? Where did you read that? InputStream 's .read() method returns an integer .

how can the real method write something to its argument and make it visible outside of itself?

Because you pass in a reference to an array where the .read(byte[]) will write . And the return value of this method is the number of bytes actually written to the byte array passed as an argument.

This code works:

public void writeOneToFirstElement(final int[] array)
{
    array[0] = 1;
}

final int[] foo = { 0 };
writeOneToFirstElement(foo);
System.out.println(foo[0]); // prints 1

THe Array is just a reference to the object and cause the adress where the data resides doesnt change on modifications it can work that way Explained here: http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html
Edit: Typo

byte array as well as String are reference types ..When you pass them as argument there reference are copied and they all refer to the same object..


For example, a remote is like a reference to a TV .When you pass the remote to another person,he's still able to access the TV

The objects are passed by copy of reference from one method to another. So if the called method makes changes to the object passed to it, method is eventually making changes to the same object which was passed to it. Hence, changes are reflected to the calling method as well. For example replace the String in the parameter with String[] and check the output.

public static void main(String[] args) 
{
 String[] myString =  {"myText"};
 myMethod(myString);
 System.out.println(myString[0]);
}

public void myMethod(String[] s) {
  s[0] = "abc123";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM