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
© Joanna Klukowska. CC-BY-SA.
The program's source code is plain text.
We can use any text editor or an IDE (Integrated Development Environment) to write it.
© Joanna Klukowska. CC-BY-SA.
© Joanna Klukowska. CC-BY-SA.
javac SOURCE_CODE_FILE
© Joanna Klukowska. CC-BY-SA.
javac SOURCE_CODE_FILE
java CLASS_FILE
© Joanna Klukowska. CC-BY-SA.
javac SOURCE_CODE_FILE
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.
© Joanna Klukowska. CC-BY-SA.
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:
x
in the above casex=5
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 belowtype | 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 |
© Joanna Klukowska. CC-BY-SA.
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).© Joanna Klukowska. CC-BY-SA.
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).© Joanna Klukowska. CC-BY-SA.
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).when a variable is declared, it does not have any value (its value is undefined)
© Joanna Klukowska. CC-BY-SA.
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).after it is initialized, its value is set
© Joanna Klukowska. CC-BY-SA.
© Joanna Klukowska. CC-BY-SA.
when we create a reference, its value is undefined
© Joanna Klukowska. CC-BY-SA.
executing new Circle(15)
creates an object in memory;
that object has NO name
© Joanna Klukowska. CC-BY-SA.
assigning that object to c
means that the value of c
variable is set to
the memory address
of the newly created Circle
object
© Joanna Klukowska. CC-BY-SA.
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
© Joanna Klukowska. CC-BY-SA.
© Joanna Klukowska. CC-BY-SA.
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.)
© Joanna Klukowska. CC-BY-SA.
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:
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
main
is always at the bottom of the stack.© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
main
is always at the bottom of the stack.Example
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
before the first method starts the stack is empty
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
program starts: main
is called
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
main
calls function foo
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
foo
calls function bar
passing value of 15 to it
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
bar
calls function bat
passing its parameter to bat
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
bat
finishes and its stack frame is removed from the stack
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
bar
finishes and its stack frame is removed from the stack
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
foo
calls function bar
again with 8 as the parameter
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
bar
calls function bat
passing its parameter to bat
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
bat
finishes and its stack frame is removed from the stack
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
bar
finishes and its stack frame is removed from the stack
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
foo
finishes and its stack frame is removed from the stack
© Joanna Klukowska. CC-BY-SA.
Stack is where all the local variables and temporary information for functions/methods are stored.
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 ) { ... }}
main
finishes and its stack frame is removed from the stack; the program ends
© Joanna Klukowska. CC-BY-SA.
Each stack frame contains information about local variables that the function creates:
© Joanna Klukowska. CC-BY-SA.
Each stack frame contains information about local variables that the function creates:
© Joanna Klukowska. CC-BY-SA.
Each stack frame contains information about local variables that the function creates:
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?
© Joanna Klukowska. CC-BY-SA.
Each stack frame contains information about local variables that the function creates:
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?
The stack frame for charStats
function should contain memory for five
different local variables:
str
and c
ratio
variablecount
variablei
© Joanna Klukowska. CC-BY-SA.
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.)
© Joanna Klukowska. CC-BY-SA.
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.)
© Joanna Klukowska. CC-BY-SA.
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.)
Again, we do not really care what exactly is the memory address stored in c
.
© Joanna Klukowska. CC-BY-SA.
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.)
And the memory addresses at which c
and the actual Circle
objects are locted, do not matter for us either.
© Joanna Klukowska. CC-BY-SA.
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];
© Joanna Klukowska. CC-BY-SA.
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];
© Joanna Klukowska. CC-BY-SA.
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];
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
.
© Joanna Klukowska. CC-BY-SA.
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];
104
(this is 100
+ 1 * 4bytes ).© Joanna Klukowska. CC-BY-SA.
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];
initial_address + index * size_of_int
100
+ 5 * 4 = 120
© Joanna Klukowska. CC-BY-SA.
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];
© Joanna Klukowska. CC-BY-SA.
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];
© Joanna Klukowska. CC-BY-SA.
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?
© Joanna Klukowska. CC-BY-SA.
Circle [] circles = new Circle[10];for (int i = 0; i < 10; i++ ) { circles[i] = new Circle(i * 5);}
main
function, what is stored on the stack
and what is stored on the heap? circles[2] = circles[8]; circles[8].radius = 1;
Circle
object that used to be stored in circles[2]
?Circle
objects are accessible through the array?© Joanna Klukowska. CC-BY-SA.
swap
functionGiven 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 );
© Joanna Klukowska. CC-BY-SA.
swap
functionGiven 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:
© Joanna Klukowska. CC-BY-SA.
swap
functionGiven 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
:
© Joanna Klukowska. CC-BY-SA.
swap
functionGiven 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
:
© Joanna Klukowska. CC-BY-SA.
swap
functionGiven 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
:
© Joanna Klukowska. CC-BY-SA.
swap
functionGiven 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
:
and the swap is completed!
© Joanna Klukowska. CC-BY-SA.
swap
functionGiven 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
:
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?
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 |