class: left, middle, title-slide # CSCI-UA 102 ## Data Structures
## Java Programs (Under the Hood) .author[ Instructor: Uday Kusupati
] .license[ Copyright 2020 Joanna Klukowska. Unless noted otherwise all content is released under a
[Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/)] --- layout:true template: default name: section class: inverse, middle, center --- layout:true template: default name: breakout class: breakout, middle --- layout:true template:default name:slide class: slide .bottom-left[© Joanna Klukowska. CC-BY-SA.] --- template: section # From Source Code (Text) to a Running Program --- ## Program's Source Code - The program's __source code__ is plain text. - We can use any text editor or an IDE (Integrated Development Environment) to write it.
.small[Atom editor]
.small[Eclipse IDE]
.right[
.small[.left[VSCode IDE]]
.small[.left[vi editor]]
] --- ## Byte Code and Java Virtual Machine .right-column2[
] .smaller[ - A __Java _Virtual_ Machine__ (JVM) is not a real machine. It is a program that can be installed on a computer. Java programs are interpreted by a JVM. ] -- .smaller[ - A compiler translates our source code into Java bytecode. The __bytecode__ consists of instructions that can be executed by our JVM. This is done using ``` javac SOURCE_CODE_FILE ``` ] -- .smaller[ - When we run a Java program, a JVM interprets the bytecode instructions by translating them one by one into the actual machine instructions (specific to the computer on which the program is executed). The CPU then executes the actual machine instruction. This is done using ``` java CLASS_FILE ``` ] -- __The consequence of this is that Java code that we write is independent of the actual computer on which we run it. As long as there is a JVM installed, the Java bytecode can be interpreted and executed on the actual hardware.__ --- template: section # Program's Memory:
Stack and Heap --- template: slide ## Why Do Programs Need Memory - Every time a program needs to store a piece of data, it uses memory to do so. - When we create a variable to store information in the program, ex: ``` int x; ``` that variable has four things associated with it: - __name__; that's `x` in the above case - __value__; that is not really defined above, but could be easily set with `x=5` - __location__ or memory address; the exact memory address is not really relevant (and, depending on the programming language may not be easily determined); whenever the program needs to retrieve the value of the variable, it needs to find the memory address and obtain the value from the bytes at that address - __type__; that's `int` in the above case; the type determines how many bytes in memory should be allocated; the most commonly used type sizes these days are shown below .center50[.small[ | type | number of bytes | |:---|:---:| |`byte`| 1 byte | |`char`, `short`| 2 bytes | |`int`, `float`| 4 bytes | |`long`, `double`| 8 bytes | |`boolean` | may vary | |memory address | 8 bytes | ]] --- name: primitive-variables ## Primitive Types Storage in Memory - For the primitive type variables (`int`, `long`, `float`, `double`, `char`, `boolean`) the value stored at the memory address allocated to that variable is the actual value of the variable (in binary format). -- .center[
] --- template: primitive-variables .center[
] .center[ when a variable is declared, it does not have any value (its value is undefined) ] --- template: primitive-variables .center[
] .center[ after it is initialized, its value is set ] --- name: reference-variables ## Reference Types Storage in Memory - All reference type variables store as their value the memory address of an object or an array that they refer to. -- .center[
] .center[ when we create a reference, its value is undefined ] --- template: reference-variables .center[
] .center[ executing `new Circle(15)` creates an object in memory;
that object has NO name ] --- template: reference-variables .center[
] .center[ _assigning that object to `c`_ means that the value of `c` variable is set to
the memory address of the newly created `Circle` object ] --- template: reference-variables .center[
] .center[ since we do not really care about the numerical value of that memory address,
we often use an arrow to indicate that
_`c` points to the object_, or
_`c` refers to the object_ ] --- template: poll ## Poll Time