+ - 0:00:00
Notes for current slide
Notes for next slide

CSCI-UA 102

Data Structures


Java Programs (Under the Hood)

Instructor: Joanna Klukowska


Copyright 2020 Joanna Klukowska. Unless noted otherwise all content is released under a
Creative Commons Attribution-ShareAlike 4.0 International License.
Background image by Stewart Weiss

1/61

From Source Code (Text) to a Running Program

2/61

© Joanna Klukowska. CC-BY-SA.

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.

atom editor
Atom editor
eclipse IDE
Eclipse IDE
VS Code window
VSCode IDE
vi editor
vi editor
3/61

© Joanna Klukowska. CC-BY-SA.

Byte Code and Java Virtual Machine

compilation and running process

  • 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.
4/61

© Joanna Klukowska. CC-BY-SA.

Byte Code and Java Virtual Machine

compilation and running process

  • 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.
  • 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
5/61

© Joanna Klukowska. CC-BY-SA.

Byte Code and Java Virtual Machine

compilation and running process

  • 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.
  • 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
  • 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
6/61

© Joanna Klukowska. CC-BY-SA.

Byte Code and Java Virtual Machine

compilation and running process

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

7/61

Program's Memory:
Stack and Heap

8/61

© Joanna Klukowska. CC-BY-SA.

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
      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
9/61

© Joanna Klukowska. CC-BY-SA.

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).
10/61

© Joanna Klukowska. CC-BY-SA.

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

variable declaration

11/61

© Joanna Klukowska. CC-BY-SA.

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

variable declaration with labels

when a variable is declared, it does not have any value (its value is undefined)

12/61

© Joanna Klukowska. CC-BY-SA.

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

variable declaration with initialization

after it is initialized, its value is set

13/61

© Joanna Klukowska. CC-BY-SA.

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.
14/61

© Joanna Klukowska. CC-BY-SA.

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.

reference declaration

when we create a reference, its value is undefined

15/61

© Joanna Klukowska. CC-BY-SA.

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.

object created

executing new Circle(15) creates an object in memory;
that object has NO name

16/61

© Joanna Klukowska. CC-BY-SA.

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.

reference variable stores the address

assigning that object to c means that the value of c variable is set to
the memory address of the newly created Circle object

17/61

© Joanna Klukowska. CC-BY-SA.

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.

reference variable points to the object

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

18/61

© Joanna Klukowska. CC-BY-SA.

Memory

  • When a program is executing on a computer, it is given a pool of memory to work with.
19/61

© Joanna Klukowska. CC-BY-SA.

Memory

  • When a program is executing on a computer, it is given a pool of memory to work with.

  • The program does not need to worry about any other program accessing that memory. (Stay tuned for the operating systems class to learn why/how.)

20/61

© Joanna Klukowska. CC-BY-SA.

Memory

  • When a program is executing on a computer, it is given a pool of memory to work with.

  • The program does not need to worry about any other program accessing that memory. (Stay tuned for the operating systems class to learn why/how.)

  • The program organizes its things in two different memory areas:

    • stack
    • heap
21/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.
22/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

23/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

empty stack

before the first method starts the stack is empty

24/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main

program starts: main is called

25/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main and foo

main calls function foo

26/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main, foo and bar(15)

foo calls function bar passing value of 15 to it

27/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main, foot, bar(15) and bat(15)

bar calls function bat passing its parameter to bat

28/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main, foo and bar(15)

bat finishes and its stack frame is removed from the stack

29/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main and foo

bar finishes and its stack frame is removed from the stack

30/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main, foo and bar(8)

foo calls function bar again with 8 as the parameter

31/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main, foot, bar(8) and bat(8)

bar calls function bat passing its parameter to bat

32/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main, foo and bar(8)

bat finishes and its stack frame is removed from the stack

33/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main and foo

bar finishes and its stack frame is removed from the stack

34/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

stack with main only

foo finishes and its stack frame is removed from the stack

35/61

© Joanna Klukowska. CC-BY-SA.

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. Each block belongs to a function/method. The block of a function that is currently executing is on top. Right below it is a block of a function that called the currently executing function, etc. The block for main is always at the bottom of the stack.

Example

public class StackExample {
public static void main (String [] args ) {
foo () ;
}
public static void foo (){
bar( 15 );
bar( 8 );
}
public static void bar ( int x ) {
bat(x);
}
public static void bat ( int x ) {
...
}
}

empty stack

main finishes and its stack frame is removed from the stack; the program ends

36/61

© Joanna Klukowska. CC-BY-SA.

Stack Frame Content

Very Simplified View

Each stack frame contains information about local variables that the function creates:

37/61

© Joanna Klukowska. CC-BY-SA.

Stack Frame Content

Very Simplified View

Each stack frame contains information about local variables that the function creates:

  • the function arguments
  • all locally created variables
  • the return value (if applicable)
38/61

© Joanna Klukowska. CC-BY-SA.

Stack Frame Content

Very Simplified View

Each stack frame contains information about local variables that the function creates:

  • the function arguments
  • all locally created variables
  • the return value (if applicable)

Consider this function:

double charStats ( String str, char c ) {
double ratio;
int count = 0;
for (int i = 0; i < str.length(); i++ ) {
if (str.charAt(i) == c ) {
count++;
}
}
ratio = count / str.length();
return ratio;
}

How many local variables are there?

39/61

© Joanna Klukowska. CC-BY-SA.

Stack Frame Content

Very Simplified View

Each stack frame contains information about local variables that the function creates:

  • the function arguments
  • all locally created variables
  • the return value (if applicable)

Consider this function:

double charStats ( String str, char c ) {
double ratio;
int count = 0;
for (int i = 0; i < str.length(); i++ ) {
if (str.charAt(i) == c ) {
count++;
}
}
ratio = count / str.length();
return ratio;
}

How many local variables are there?

charStat stack frame

The stack frame for charStats function should contain memory for five different local variables:

  • two parameters: str and c
  • the ratio variable
  • the count variable
  • the loop counter variable i
40/61

© Joanna Klukowska. CC-BY-SA.

Heap

  • Whenever the program uses the keyword new the memory for that object is allocated on the heap.

  • Heap is not as organized as the stack. The chunks of memory that are allocated to different arrays and objects can be all over the place. (Well, there is some logic in it, but we will not get into it and it is not relevant for our discussions.)

41/61

© Joanna Klukowska. CC-BY-SA.

Heap

  • Whenever the program uses the keyword new the memory for that object is allocated on the heap.

  • Heap is not as organized as the stack. The chunks of memory that are allocated to different arrays and objects can be all over the place. (Well, there is some logic in it, but we will not get into it and it is not relevant for our discussions.)

Circle reference on the stack, object on the heap

42/61

© Joanna Klukowska. CC-BY-SA.

Heap

  • Whenever the program uses the keyword new the memory for that object is allocated on the heap.

  • Heap is not as organized as the stack. The chunks of memory that are allocated to different arrays and objects can be all over the place. (Well, there is some logic in it, but we will not get into it and it is not relevant for our discussions.)

arrow in place of memory address

Again, we do not really care what exactly is the memory address stored in c.

43/61

© Joanna Klukowska. CC-BY-SA.

Heap

  • Whenever the program uses the keyword new the memory for that object is allocated on the heap.

  • Heap is not as organized as the stack. The chunks of memory that are allocated to different arrays and objects can be all over the place. (Well, there is some logic in it, but we will not get into it and it is not relevant for our discussions.)

memory addresses do not matter either

And the memory addresses at which c and the actual Circle objects are locted, do not matter for us either.

44/61

© Joanna Klukowska. CC-BY-SA.

Arrays and the Heap

Arrays in Java are always stored on the heap in consecutive memory locations.

Example: If our program tries to allocate an array of 10 integers, we will need 40 consecutive bytes of memory on the heap (because each int needs 4 bytes of memory on current computers).

int [] array = new int[10];
45/61

© Joanna Klukowska. CC-BY-SA.

Arrays and the Heap

Arrays in Java are always stored on the heap in consecutive memory locations.

Example: If our program tries to allocate an array of 10 integers, we will need 40 consecutive bytes of memory on the heap (because each int needs 4 bytes of memory on current computers).

int [] array = new int[10];

an array in memory

46/61

© Joanna Klukowska. CC-BY-SA.

Arrays and the Heap

Arrays in Java are always stored on the heap in consecutive memory locations.

Example: If our program tries to allocate an array of 10 integers, we will need 40 consecutive bytes of memory on the heap (because each int needs 4 bytes of memory on current computers).

int [] array = new int[10];

address of the array is 100

  • We'll assume that the memory address of the array is 100 (we'll use decimal numbers instead of hexadecimal numbers for addresses in this example to make things a bit easier ).

  • This means that the address of the first element is also 100.

47/61

© Joanna Klukowska. CC-BY-SA.

Arrays and the Heap

Arrays in Java are always stored on the heap in consecutive memory locations.

Example: If our program tries to allocate an array of 10 integers, we will need 40 consecutive bytes of memory on the heap (because each int needs 4 bytes of memory on current computers).

int [] array = new int[10];

address of the element at index 1 is 104

  • The address of an element at index 1 is exactly 4 bytes after the element at index 0 (because arrays are always allocated in consecutive memory locations).
  • Therefore the address of the element at index 1 is 104 (this is 100 + 1 * 4bytes ).
48/61

© Joanna Klukowska. CC-BY-SA.

Arrays and the Heap

Arrays in Java are always stored on the heap in consecutive memory locations.

Example: If our program tries to allocate an array of 10 integers, we will need 40 consecutive bytes of memory on the heap (because each int needs 4 bytes of memory on current computers).

int [] array = new int[10];

address of the element at index 5 is 120

  • With a bit of arithmetic we can figure out the address of the element at index 5:

    initial_address + index * size_of_int

    or

    100 + 5 * 4 = 120

49/61

© Joanna Klukowska. CC-BY-SA.

Arrays and the Heap

Arrays in Java are always stored on the heap in consecutive memory locations.

Example: If our program tries to allocate an array of 10 integers, we will need 40 consecutive bytes of memory on the heap (because each int needs 4 bytes of memory on current computers).

int [] array = new int[10];

address of the element at index 9 is 136

  • What do you think is the address of the last element?
50/61

© Joanna Klukowska. CC-BY-SA.

Arrays and the Heap

Arrays in Java are always stored on the heap in consecutive memory locations.

Example: If our program tries to allocate an array of 10 integers, we will need 40 consecutive bytes of memory on the heap (because each int needs 4 bytes of memory on current computers).

int [] array = new int[10];

address of the element at index 9 is 136

51/61

Examples and Things to Think About

52/61

© Joanna Klukowska. CC-BY-SA.

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.

Circle c1 = new Circle (10);
Circle c2 = new Circle (20);
Circle c3 = c1;
System.out.println(c1.radius + " " + c2.radius + " " + c3.radius);
c1.radius = 30;
System.out.println(c1.radius + " " + c2.radius + " " + c3.radius);
c1 = c2;
System.out.println(c1.radius + " " + c2.radius + " " + c3.radius);
c2.radius = 5;
c2 = c3;
System.out.println(c1.radius + " " + c2.radius + " " + c3.radius);
c1 = c2;
c1.radius = 15;
System.out.println(c1.radius + " " + c2.radius + " " + c3.radius);

What is the output of the above code?

53/61

© Joanna Klukowska. CC-BY-SA.

Example 2: What Happens in Memory

Circle [] circles = new Circle[10];
for (int i = 0; i < 10; i++ ) {
circles[i] = new Circle(i * 5);
}
  • How is the above array laid out in memory?
  • Assuming the code fragment is part of a main function, what is stored on the stack and what is stored on the heap?
  • Consider the following code fragment:
    circles[2] = circles[8];
    circles[8].radius = 1;
    • how does the memory image change?
    • what happens to the Circle object that used to be stored in circles[2]?
    • how many different Circle objects are accessible through the array?
54/61

© Joanna Klukowska. CC-BY-SA.

Think About: swap function

Given the swap function

void swap ( double d1, double d2 ) {
double tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}

consider the following code fragment:

double num1 = 3.1415;
double num2 = 2.1718;
swap ( num1, num2 );
55/61

© Joanna Klukowska. CC-BY-SA.

Think About: swap function

Given the swap function

void swap ( double d1, double d2 ) {
double tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}

consider the following code fragment:

double num1 = 3.1415;
double num2 = 2.1718;
swap ( num1, num2 );

the values of d1 and d2 and initialized to the arguments that are passed to the function:

swap stack fram

56/61

© Joanna Klukowska. CC-BY-SA.

Think About: swap function

Given the swap function

void swap ( double d1, double d2 ) {
double tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}

consider the following code fragment:

double num1 = 3.1415;
double num2 = 2.1718;
swap ( num1, num2 );

the value stored in d1 is copied to tmp:

swap stack fram

57/61

© Joanna Klukowska. CC-BY-SA.

Think About: swap function

Given the swap function

void swap ( double d1, double d2 ) {
double tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}

consider the following code fragment:

double num1 = 3.1415;
double num2 = 2.1718;
swap ( num1, num2 );

the value stored in d2 is copied to d1:

swap stack fram

58/61

© Joanna Klukowska. CC-BY-SA.

Think About: swap function

Given the swap function

void swap ( double d1, double d2 ) {
double tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}

consider the following code fragment:

double num1 = 3.1415;
double num2 = 2.1718;
swap ( num1, num2 );

the value stored in tmp is copied to d2:

swap stack fram

59/61

© Joanna Klukowska. CC-BY-SA.

Think About: swap function

Given the swap function

void swap ( double d1, double d2 ) {
double tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}

consider the following code fragment:

double num1 = 3.1415;
double num2 = 2.1718;
swap ( num1, num2 );

the value stored in tmp is copied to d2:

swap stack fram

and the swap is completed!

60/61

© Joanna Klukowska. CC-BY-SA.

Think About: swap function

Given the swap function

void swap ( double d1, double d2 ) {
double tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}

consider the following code fragment:

double num1 = 3.1415;
double num2 = 2.1718;
swap ( num1, num2 );

the value stored in tmp is copied to d2:

swap stack fram

and the swap is completed!

or is it?

If we execute

System.out.println(num1 + " " + num2 );

after the call to swap function what will the output be?

61/61

From Source Code (Text) to a Running Program

2/61
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow