JAVA: What is the difference between Outputstream and getOutputStream()?

Asked

Viewed 1,329 times

7

I’m studying java and came across several classes of input and output data, but I had a doubt in the difference between OutputStream and getOutputStream().

From what I understand, the getOutputStream() take what you’re going through. And the OutputStream?

  • Related: https://answall.com/a/142604/132

2 answers

11


OutputStream is a class. Corresponds to an object that writes sequences of bytes somewhere.

There are several other objects that have one method called getOutputStream() which, as the name implies, returns an object of the type OutputStream (or any of his subclasses).

Finally, the getOutputStream() is the method you call to obtain an instance of a OutputStream to write something somewhere.

The place where the writing will occur depends on the instance of the OutputStream obtained, and therefore depends on the object that has the method getOutputStream().

  • The object as the Outputstreamwriter, right ?

  • @Daniel O OutputStreamWriter is a class that serves as a bridge between the java.io.Writer and the OutputStream. The Writer is similar to the OutputStream, but instead of writing bytes, it writes characters.

7

In addition to Victor’s response, which correctly emphasizes the difference between type (class OutputStream) and the method of access to the object of that type (getOutputStream), I think it’s important to point out that...

1. Methods with the same name can return other types of objects

The method ServletResponse#getOutputStream, for example, returns a ServletOutputStream, that is still a subclass of OutputStream, but still it’s important to know that it’s a different concrete type.

2. Methods of the same name exist in different classes with different purposes

Examples:

3. Java has a complex hierarchy of stream for input and output

inserir a descrição da imagem aqui

It is not necessary to be an expert in each of them, but for each situation that involves streams It is important to consult the documentation and understand what is the best type to work according to the features offered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.