Search code examples
javaobjectfileoutputstreamdataoutputstream

reference variable DataOutputStream and FileOutputStream


Given this sample of code:

{
try (DataOutputStream dataOut= new DataOutputStream (new FileOutputStream ("testdata")

please, let me know, is dataOutput a reference variable also for FileOutputStream since it somehow works? Otherwise, why to put in a parameter holder new FileOutputStream ("testdata"), how does it actually work if there is no reference variable of it?

Ps.: please, don't beat me too much, I'm still new to all of this.


Solution

  • The reference variable is of type DataOutputStream which encapsulates internally the FileOutputStream object. And no, you have no access to FileOutputStream directly if you instantiate it in that way.

    Actually you could work on FileOutputStream itself without wrapping(decorate - please check Decorator Pattern) it. Even though DataOutputStream provides you additional write methods for primitive types.