0% found this document useful (0 votes)
4 views

2java

The document outlines a lecture on Data Structures using JAVA, covering course introduction, office hours, tutoring, and an overview of JAVA's features. It explains the advantages and disadvantages of JAVA, including its 'Write Once, Run Anywhere' capability, and discusses memory allocation for primitive and reference types, as well as stack and heap memory. The lecture concludes with examples of memory behavior in JAVA.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2java

The document outlines a lecture on Data Structures using JAVA, covering course introduction, office hours, tutoring, and an overview of JAVA's features. It explains the advantages and disadvantages of JAVA, including its 'Write Once, Run Anywhere' capability, and discusses memory allocation for primitive and reference types, as well as stack and heap memory. The lecture concludes with examples of memory behavior in JAVA.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Data Structures

<Lecture 2: JAVA>

Sungmin Cha
New York University

09.09.2024
Outline
• Course introduction
– Office hours
– Tutoring

• Overview of this course

• JAVA

2
Outline
• Course introduction
– Office hours
– Tutoring

• Overview of this course

• JAVA

3
Office Hours
• Office hours

– The schedule and link


is updated in Brightspace

4
Tutoring
• Tutoring (Offline and online)

– Check the link in Brightspace

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)

• The JAVA slogan


– “Write Once, Run Anywhere (WORA)”

How is it possible?

8
WORA
• Program build process of C++

Win C++ Win executable file


Compiler

C++ Linux C++ Linux executable file


Source code Compiler

Mac C++ Mac executable file


Compiler
9
WORA
• Program build process of C++
– Problems
1. Due to different compilers, the same code may behave differently
depending on the OS
2. It might not compile on specific OS

10
WORA
• Program build process of JAVA
– Java Runtime Environment(JRE)

JAVA JRE

JAVA JAVA JAVA byte code JAVA JRE


Source code Compiler

– Java Virtual Machine(JVM) in JRE execute


JAVA byte code JAVA JRE
11
WORA
• Program build process of JAVA
– Advantage
1. It can support various platforms from the same source code.
2. Regardless of the OS, consistent behavior and results can be achieved.

– 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.

• In the case of simple programming..


– Ed Workspace is also good!

13
Memory and Memory Address

• Memory map (64bit machine) Figure form this link

– Maximum size of memory?


 64-bit: 2^64 addresses x 1byte = about 16 million TB
 32-bit: 2^32 addresses x 1byte = about 4GB
14
Memory and Memory Address

• Memory allocation map of JVM Figure form this link

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

4byte 5 (as a binary)

… 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

stack 0x3EF3B 0x3EF3B

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

How many local variables are there?

44
Stack and Heap Memory
• Stack frame contents
– Example: simplified view

Stack frame of charStats

str c

ratio count

How many local variables are there?

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

stack 0x3EF3B 0x3EF3B

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

address 100 104 108 112 … 136

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;’

address 100 104 108 112 … 136

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;’

address 100 104 108 112 … 136

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;’

address 100 104 108 112 … 136

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.

What is the output of


this code?

54
Thank you!

E-mail: [email protected]

55

You might also like