# Compilation and Execution

## Introduction

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFW3x2aqEO8GF2kr3VU%2F-MJ18AUavAEMcbGQQQPV%2F-MJ18QwGCQVoq7v2jLUW%2Fjava.jpg?alt=media\&token=81b69c54-f8ad-4bbb-be77-986e8ad45cb9)

* During Compilation(Compile time), the compiler convert source code (.java) into bytecode (.class)
* Compile Time Error is mainly related to syntax error of source code
* During Execution (Run time), the class file will read ,the related class will be loaded and the relevant information (e.g: static variable, method ) will be stored into memory of JVM&#x20;
* Run time Error is mainly related to out of memory,  divided by 0 ,dereferencing a null pointer

![JVM Memory](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFW3x2aqEO8GF2kr3VU%2F-MJ1A1VqXwYoimvaDN6b%2F-MJ1Mtl51xKHVq0Ep6EJ%2Fjvm-memory-2.jpg?alt=media\&token=53abe526-1518-43cc-8b42-2272080acff6)

* Finally , the class file will be read line by line , data in memory area , convert into native machine code and be executed by execution engine &#x20;

1. **JDK**(Java Development Kit) : JDK is intended for software developers and includes development tools such as the Java compiler, Javadoc, Jar, and a debugger.
2. **JRE**(Java Runtime Environment) : JRE contains the parts of the Java libraries required to run Java programs and is intended for end users. JRE can be view as a subset of JDK.
3. **JVM:** JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms.&#x20;



## Heap and Stack

### Heap

* Used to store the address of the object such as String, ...
* If heaps is fulled, Out of memory error will be thrown

### Stack

* When the method is executed, a new stack will be created and the local primitive variable and the reference of the object which can be found in heaps will be stored&#x20;
* After execution of the method, the corresponding stack frame will be flushed
* Last in First Out

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFW3x2aqEO8GF2kr3VU%2F-MVX8fe7k5lY8SBoSGCp%2F-MVXAPZ3m52bcVTL9CxL%2Fimage.png?alt=media\&token=11be6081-951e-4029-ae7d-2634b7408175)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFW3x2aqEO8GF2kr3VU%2F-MVX8fe7k5lY8SBoSGCp%2F-MVXAWhdb4_8U1Kv_qK_%2Fimage.png?alt=media\&token=c7b594bf-1707-4be9-b17d-eb90195de176)

### Garbage Collection

* In order to prevent out of memory, as there are many unused objects, the heap memory will be cleared automatically in the background
