An object in the Heap
This program creates a Product. The object lives in the Heap, the memory of the JVM. While main runs, the object exists and can be used.
This program creates a Product. The object lives in the Heap, the memory of the JVM.
While main runs, the object exists and can be used.
This program creates a Product. The object lives in the Heap, the memory of the JVM. While main runs, the object exists and can be used.
At the last brace, main ends and the JVM stops.
Without a JVM there is no Heap: the memory evaporates and the object is lost.
At the last brace, main ends and the JVM stops. Without a JVM there is no Heap: the memory evaporates and the object is lost.
Now the program saves the data: FileWriter opens a file with explicit UTF-8.
BufferedWriter wraps it: a decorator that adds a buffer to it.
Now the program saves the data: FileWriter opens a file with explicit UTF-8. BufferedWriter wraps it: a decorator that adds a buffer to it.
out.write() hands the product to the decorator as a CSV line.
The line stays in the buffer: it has not touched the disk yet.
The buffer gathers the data and goes to the disk only once per block.
out.write() hands the product to the decorator as a CSV line. The line stays in the buffer: it has not touched the disk yet. The buffer gathers the data and goes to the disk only once per block.
On leaving the block, the file is closed for you: try-with-resources calls close().
Closing flushes the buffer: the line goes through FileWriter and reaches the disk.
If the file is not closed, whatever stayed in the buffer is lost.
On leaving the block, the file is closed for you: try-with-resources calls close(). Closing flushes the buffer: the line goes through FileWriter and reaches the disk. If the file is not closed, whatever stayed in the buffer is lost.
The program prints "Saved to disk" and main ends again.
The JVM stops and the Heap evaporates again…
…but catalog.csv is still on the disk: it survives the program.
The program prints "Saved to disk" and main ends again. The JVM stops and the Heap evaporates again… …but catalog.csv is still on the disk: it survives the program.
javac compiles the code into .class files: bytecode for the JVM.
jar packages them into app.jar, which is a ZIP with every class.
The e in cvfe writes the line Main-Class: Main into MANIFEST.MF.
javac compiles the code into .class files: bytecode for the JVM. jar packages them into app.jar, which is a ZIP with every class. The e in cvfe writes the line Main-Class: Main into MANIFEST.MF.
java -jar app.jar opens the JAR and reads Main-Class in the manifest.
That is how it knows where to start: it runs main on any machine with a JVM.
Memory that evaporates, a disk that persists and a JAR to deliver the program.
java -jar app.jar opens the JAR and reads Main-Class in the manifest. That is how it knows where to start: it runs main on any machine with a JVM. Memory that evaporates, a disk that persists and a JAR to deliver the program.