2java
2java
<Lecture 2: JAVA>
Sungmin Cha
New York University
09.09.2024
Outline
• Course introduction
– Office hours
– Tutoring
• JAVA
2
Outline
• Course introduction
– Office hours
– Tutoring
• JAVA
3
Office Hours
• Office hours
4
Tutoring
• Tutoring (Offline and online)
5
Outline
• Course introduction
– Office hours
– Tutoring
• JAVA
6
JAVA
• One of the most popular programming languages
Figure form this link
JAVA
7
JAVA
• Power of JAVA
– Most libraries are compatible with JAVA
Except for some special fields (e.g., Machine Learning)
– Object-Oriented Programming (OOP)
How is it possible?
8
WORA
• Program build process of C++
10
WORA
• Program build process of JAVA
– Java Runtime Environment(JRE)
JAVA JRE
– Disadvantage
1. It is relatively slower compared to other languages like C++.
2. It demands a significant amount of memory usage for JVM.
3. The code tends to be overly verbose.
12
Development Environment
• Integrated Development Environments (IDE)
– 34 Listings in Java IDE available
:Visual Studio, Eclipse, Xcode, etc.
13
Memory and Memory Address
15
JAVA Variables
• JAVA’s 8 primitive type variables
– There are 8 primitive types and save a data value
– These are allocated into Stack memory
Primitive Values Primitive Size
Datatypes
byte 1 byte
short 2 bytes
Integers
Numbers int 4 bytes
long 8 bytes
Floating float 4 bytes
Values
double 8 bytes
Character char 2 bytes
Boolean boolean 1 bit 16
JAVA Variables
• Memory allocation of primitive type variables
– Example:
Stack memory area
…
0x12AB5
… 17
JAVA Variables
• Memory allocation of primitive type variables
– Example:
Stack memory area
…
int x;
4byte
0x12AB5
4byte
… 18
JAVA Variables
• Memory allocation of primitive type variables
– Example:
Stack memory area
…
int x;
x=5; 0x12AB5
4byte
… 19
JAVA Variables
• Memory allocation of primitive type variables
– Example:
Stack memory area
…
int x;
x=5; 0x12AB5
4byte
… 20
JAVA Variables
• Memory allocation of primitive type variables
– Example:
Stack memory area
…
int x;
x=5; 0x12AB5
… 21
JAVA Variables
• Memory allocation of primitive type variables
– Example:
abstracted figure name
x
int x; value
0x12AB5
location
22
JAVA Variables
• Memory allocation of primitive type variables
– Example:
abstracted figure
x
int x;
x=5;
5
0x12AB5
23
JAVA Variables
• JAVA’s reference types
– All types except for primitive types
Class, Array, etc.
– Refer to the object through the location value
– In Stack, a reference variable is allocated
The real object is allocated into Heap
24
JAVA Variables
• Memory allocation of reference type variables
– Example:
Circle c;
stack ?
0x4AF23
25
JAVA Variables
• Memory allocation of reference type variables
– Example:
Circle c;
c = new circle(15);
c Circle
heap
radious = 15
stack ? 0x3EF3B
0x4AF23
26
JAVA Variables
• Memory allocation of reference type variables
– Example:
Circle c;
c = new circle(15);
Allocate new memory in Heap!
c Circle
heap
radious = 15
stack ? 0x3EF3B
0x4AF23
27
JAVA Variables
• Memory allocation of reference type variables
– Example:
Circle c;
c = new circle(15);
c Circle
heap
radious = 15
0x4AF23
28
Stack and Heap Memory
• Stack
– Stack is where all the local variables and temporary
information for functions/methods are stored.
– It is organized in a collection of memory blocks, called stack
frames.
29
Stack and Heap Memory
• Stack
– Example: Stack
30
Stack and Heap Memory
• Stack
– Example: Stack
main
31
Stack and Heap Memory
• Stack
– Example: Stack
foo
main
32
Stack and Heap Memory
• Stack
– Example: Stack
bar(15)
foo
main
33
Stack and Heap Memory
• Stack
– Example: Stack
bat(15)
bar(15)
foo
main
34
Stack and Heap Memory
• Stack
– Example: Stack
bar(15)
foo
main
35
Stack and Heap Memory
• Stack
– Example: Stack
foo
main
36
Stack and Heap Memory
• Stack
– Example: Stack
bar(8)
foo
main
37
Stack and Heap Memory
• Stack
– Example: Stack
bat(8)
bar(8)
foo
main
38
Stack and Heap Memory
• Stack
– Example: Stack
bar(8)
foo
main
39
Stack and Heap Memory
• Stack
– Example: Stack
foo
main
40
Stack and Heap Memory
• Stack
– Example: Stack
main
41
Stack and Heap Memory
• Stack
– Example: Stack
42
Stack and Heap Memory
• Stack frame contents
– Each stack frame contains information about local variables
that the function creates:
The function arguments
All locally created variables
The return values (if applicable)
43
Stack and Heap Memory
• Stack frame contents
– Example: simplified view
44
Stack and Heap Memory
• Stack frame contents
– Example: simplified view
str c
ratio count
45
Stack and Heap Memory
• Heap
– Whenever the program uses the keyword ‘new’, the memory
for that object is allocated on the heap.
– Example:
Circle c;
c = new circle(15);
c radious = 15 heap
0x4AF23 46
Stack and Heap Memory
• Arrays and the Heap
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example:
47
Stack and Heap Memory
• Arrays and the Heap
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example:
Reference variable
array
stack ?
48
Stack and Heap Memory
• Arrays and the Heap
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example:
Allocate object on the heap Size = 4byte * 10
address 100
0 0 0 0 0 0 0 0 0 0 heap
array
What is the address of each index?
stack 100
49
Stack and Heap Memory
• Arrays and the Heap
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example:
Allocate object on the heap Size = 4byte * 10
0 0 0 0 0 0 0 0 0 0 heap
array
What is the address of each index?
stack 100
50
Stack and Heap Memory
• Arrays and the Heap
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example: What happens if we run ‘array[4] = 12;’
0 0 0 0 0 0 0 0 0 0 heap
array
stack 100
51
Stack and Heap Memory
• Arrays and the Heap
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example: What happens if we run ‘array[3] = 12;’
0 0 0 0 0 0 0 0 0 0 heap
array
stack 100
52
Stack and Heap Memory
• Arrays and the Heap
– Arrays in JAVA are always stored on the heap in consecutive
memory locations
– Example: What happens if we run ‘array[3] = 12;’
0 0 0 12 0 0 0 0 0 0 heap
array
stack 100
53
Examples
• Example 1: What Happens in Memory
– Assume that there is a class called Circle. It has a public data
field called radius.
– It has a one-parameter constructor that takes a radius of a circle
as its argument and creates a Circle object with that radius.
54
Thank you!
E-mail: [email protected]
55