Using Java IO streams, it is quite often we use objects solely as constructors for other objects. I am interested in the memory implications of this prospect. For example, how does memory allocation differ in these two statements that do the same thing?
FileInputStream inputFile = new FileInputStream("filepath");
Scanner inStream = new Scanner(inputFile);
and
Scanner inStream = new Scanner(new FileInputStream("filepath"));
The first one will allocate a named variable in the current stack frame. On the heap, there is no difference – or there shouldn’t be but the VM is of course free to optimize the code in some way as long as the rules are obeyed.