COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf ·...
Transcript of COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf ·...
![Page 1: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/1.jpg)
COMP15111: Introduction to ArchitectureLecture 10: Methods – Parameters and Variables
Dr. Javier Navaridas
School of Computer Science, University of Manchester
Autumn 2016
COMP15111 Lecture 10 1/33
![Page 2: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/2.jpg)
Lecture 10: You are here
An Example Java Program
Lifespan of Variables in a Running Java Program
COMP15111 Lecture 10 An Example Java Program 2/33
![Page 3: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/3.jpg)
An example Java Program – AgeHistory2
static int presentDay, presentMonth, presentYear;
void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;
System.out.println(...);... birthYear ... presentYear ... someYear ...
void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);
void println(String string)print(string);
write(newline);
void print(String string)write(nextchar);
COMP15111 Lecture 10 An Example Java Program 4/33
![Page 4: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/4.jpg)
An example Java Program – AgeHistory2
static int presentDay, presentMonth, presentYear;
void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;
System.out.println(...);... birthYear ... presentYear ... someYear ...
void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);
void println(String string)print(string);
write(newline);
void print(String string)write(nextchar);
COMP15111 Lecture 10 An Example Java Program 4/33
![Page 5: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/5.jpg)
An example Java Program – AgeHistory2
static int presentDay, presentMonth, presentYear;
void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;
System.out.println(...);... birthYear ... presentYear ... someYear ...
void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);
void println(String string)print(string);
write(newline);
void print(String string)write(nextchar);
COMP15111 Lecture 10 An Example Java Program 4/33
![Page 6: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/6.jpg)
An example Java Program – AgeHistory2
static int presentDay, presentMonth, presentYear;
void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;
System.out.println(...);... birthYear ... presentYear ... someYear ...
void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);
void println(String string)print(string);
write(newline);
void print(String string)write(nextchar);
COMP15111 Lecture 10 An Example Java Program 4/33
![Page 7: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/7.jpg)
An example Java Program – AgeHistory2
static int presentDay, presentMonth, presentYear;
void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;
System.out.println(...);... birthYear ... presentYear ... someYear ...
void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);
void println(String string)print(string);
write(newline);
void print(String string)write(nextchar);
COMP15111 Lecture 10 An Example Java Program 4/33
![Page 8: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/8.jpg)
Lecture 10: You are here
An Example Java Program
Lifespan of Variables in a Running Java Program
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 5/33
![Page 9: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/9.jpg)
Running AgeHistory2
birthYear1 etc.
presentYear etc.
main starts
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 7/33
![Page 10: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/10.jpg)
Running AgeHistory2
intString etc.
birthYear1 etc.
presentYear etc.
main calls parseInt (*6)
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 9/33
![Page 11: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/11.jpg)
Running AgeHistory2
birthYear1 etc.
presentYear etc.
parseInt returns to main
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 11/33
![Page 12: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/12.jpg)
Running AgeHistory2
someYear etc.
birthYear1 etc.
presentYear etc.
main calls printAgeHistory (*2)
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 13/33
![Page 13: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/13.jpg)
Running AgeHistory2
string etc.
someYear etc.
birthYear1 etc.
presentYear etc.
printAgeHistory calls println (*3?)
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 15/33
![Page 14: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/14.jpg)
Running AgeHistory2
string etc.
string etc.
someYear etc.
birthYear1 etc.
presentYear etc.
println calls print
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 17/33
![Page 15: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/15.jpg)
Running AgeHistory2
char etc.
string etc.
string etc.
someYear etc.
birthYear1 etc.
presentYear etc.
print calls write (*n)
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 19/33
![Page 16: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/16.jpg)
Running AgeHistory2
string etc.
string etc.
someYear etc.
birthYear1 etc.
presentYear etc.
write returns to print
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 21/33
![Page 17: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/17.jpg)
Running AgeHistory2
string etc.
someYear etc.
birthYear1 etc.
presentYear etc.
print returns to println
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 23/33
![Page 18: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/18.jpg)
Running AgeHistory2
char etc.
string etc.
someYear etc.
birthYear1 etc.
presentYear etc.
println calls write
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 25/33
![Page 19: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/19.jpg)
Running AgeHistory2
string etc.
someYear etc.
birthYear1 etc.
presentYear etc.
write returns to println
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 27/33
![Page 20: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/20.jpg)
Running AgeHistory2
someYear etc.
birthYear1 etc.
presentYear etc.
println returns to printAgeHistory
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 29/33
![Page 21: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/21.jpg)
Running AgeHistory2
birthYear1 etc.
presentYear etc.
printAgeHistory returns to main
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 31/33
![Page 22: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer](https://reader033.fdocuments.net/reader033/viewer/2022042212/5eb5c96c9782ba3f9963411b/html5/thumbnails/22.jpg)
Running AgeHistory2
presentYear etc.
main finishes
COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 33/33