CIS 110: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
COP 3330 : EXAM 1 REVIEW Page 1 © Dr. Mark Llewellyn COP 3330: Object-Oriented Programming Summer 2011 EXAM #1 – In Class Practice Department of Electrical.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Scoping and Recursion CSC 171 FALL 2001 LECTURE 8.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Building java programs, chapter 3 Parameters, Methods and Objects.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Output Programs These slides will present a variety of small programs. Each program has a compound condition which uses the Boolean Logic that was introduced.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 7.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Staples are our staple Building upon our solution.
Lecture 3: Method Parameters
Introduction to Programming Using C
Examples of Classes & Objects
Advanced Programming TA Session 2
using System; namespace Demo01 { class Program
Introduction to programming in java
Building Java Programs
INF3110: Exercises Part 3 Comments and Solutions
Building Java Programs
Haidong Xue Summer 2011, at GSU
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Instance Method Review - CIS 1068 Program Design and Abstraction
CSC240 Computer Science III
CSC 253 Lecture 8.
Chapter 8: Collections: Arrays
Computing Adjusted Quiz Total Score
CSC 253 Lecture 8.
CSC 113 Tutorial QUIZ I.
An Introduction to Java – Part II
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Lecture 18 Arrays and Pointer Arithmetic
CIS 110: Introduction to Computer Programming
CIS 110: Introduction to Computer Programming
Parameter Passing in Java
References, Objects, and Parameter Passing
CSE 214 – Computer Science I More on Linked Lists
CS2011 Introduction to Programming I Arrays (II)
Topics discussed in this section:
Methods Again Parameter Passage
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Lecture 3: Method Parameters
slides created by Ethan Apter
Why did the programmer quit his job?
slides created by Alyssa Harding
Scope of variables class scopeofvars {
CIS 110: Introduction to Computer Programming
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Building Java Programs
slides created by Ethan Apter
Building Java Programs
CIS 110: Introduction to Computer Programming
Lecture 6: References and linked nodes reading: 16.1
Methods/Functions.
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
PROGRAMMING ASSIGNMENT I
CIS 110: Introduction to Computer Programming
CIS 110: Introduction to Computer Programming
Presentation transcript:

CIS 110: Introduction to Computer Programming Lecture 18 Reference semantics (§ 7.2-7.3) 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Outline Reference semantics Array traversals 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Reference Semantics 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Review: pass by copy public static void change(int x) { x = 5; } public static void main(String[] args) { int x = 0; change(x); System.out.println(x); // Prints 0 x The x variables are distinct. We pass a copy of the contents of x to change. Primitive variables contain their values directly. 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

The twist: arrays behave differently! public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x Array variables (generally object variables) contain references to the arrays rather than the arrays themselves. 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

Passing array parameters (1) public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

Passing array parameters (2) public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x x 5 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

Passing array parameters (3) public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x 5 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

Pass by value vs. pass by reference public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x 5 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

Pass by value vs. pass by reference For primitive types we pass copies of the contents of the variables. For reference types, we pass references to the objects the variables refer to. public static void change(int x) { x = 5; } x 5 x 5 x 5 public static void change(int[] x) { x[0] = 5; } x 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

Alternative view: we copy references Alternatively, we always copy the contents of variables along. But we copy references of variables of object type. Either viewpoint is valid --- pick the one that makes the most sense! public static void change(int x) { x = 5; } x 5 x 5 x 5 public static void change(int[] x) { x[0] = 5; } x 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

Alternative view: we copy references Alternatively, we always copy the contents of variables along. But we copy references of variables of object type. public static void change(int x) { x = 5; } x 5 public static void change(int[] x) { x[0] = 5; } x 5 7/17/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Array Traversals See Traversals.java, ExtractDigit.java 7/17/2019 CIS 110 (11fa) - University of Pennsylvania