A+ Computer Science PARAMETERS

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
© A+ Computer Science - Chicken yeller = new Chicken();
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
Variables and memory addresses
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
Chapter 9 A Second Look at Classes and Objects - 2.
A structured walkthrough
Objects Real and Java COMP T1 3
Functions + Overloading + Scope
Computer Organization and Design Pointers, Arrays and Strings in C
OOP Powerpoint slides from A+ Computer Science
Chapter 8 Classes and Objects
A Second Look at Classes and Objects - 2
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
More Object Oriented Programming
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Object Oriented Programming COP3330 / CGS5409
March 29th Odds & Ends CS 239.
Multiple Choice -answer the easiest question 1st
Chapter 4: Writing classes
A+ Computer Science INPUT.
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Using PARAMETERS In Java.
A+ Computer Science METHODS.
Arrays Strings and Parameter Passing CSCI N305
Pointers Call-by-Reference CSCI 230
Parameter Passing in Java
© A+ Computer Science - OOP © A+ Computer Science -
Programs and Classes A program is made up from classes
Java Classes Aliases & Null & This
Simulating Reference Parameters in C
Why did the programmer quit his job?
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
A+ Computer Science METHODS.
Take out a piece of paper and PEN.
Defining Classes and Methods
Take out a piece of paper and PEN.
Lecture 03 & 04 Method and Arrays Jaeki Song.
Lecture 6: References and linked nodes reading: 16.1
C Parameter Passing.
Presentation transcript:

A+ Computer Science PARAMETERS © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is a refefence? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com References In Java, any variable that refers to an Object is a reference variable. The variable stores the memory address of the actual Object. All variables in Java that refer to Objects are called references. Reference variables store the location / memory address of the actual Object. For most situations, the value stored in a reference is a memory address. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com References String x = new String("Chuck"); String y = x; x and y store the same memory address. x y In this example, x and y both the store the location / address of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. For this example, x==y is true. x==y compares the values stored in x and y. x and y both store the same location / address. For this example, x.equals(y) is true. x.equals(y)compares the contents of the Objects referred to by x and y. Chuck is being compare to Chuck. 0x2B3 0x2B3 0x2B3 "Chuck" © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com References String x = "Chuck"; String y = "Chuck"; x and y store the same memory address. y x In this example, x and y both the store the location of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. For this example, x==y is true. x==y compares the values stored in x and y. x and y both store the same location / address. For this example, x.equals(y) is true. x.equals(y)compares the contents of the Objects referred to by x and y. Chuck is being compare to Chuck. 0x2B7 0x2B7 0x2B7 "Chuck" © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com References String x = new String("Chuck"); String y = new String("Chuck"); x and y store different memory addresses. y x In this example, x stores the location / address of a String Object that stores the value Chuck. y also stores the location of a different String Object that stores the value Chuck. x and y do not store the same location / address. For this example, x==y is false. x and y do not store the same location / address. For this example, x.equals(y) is true. 0x2FE 0x2B7 0x2FE 0x2B7 "Chuck" "Chuck" © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com References String x = "Chuck"; String y = "Chuck"; x = null; y x In this example, x and y both the store the location / address of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. At the start, x==y is true. x is then referred to null. x now stores null. y was in no way changed. y still stores the address of Chuck. After changing the value of x, x==y is false. 0x2B7 0x2B7 0x2B7 null "Chuck" © A+ Computer Science - www.apluscompsci.com

references.java

© A+ Computer Science - www.apluscompsci.com What is a parameter? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Parameters A parameter/argument is a channel used to pass information to a method. Parameters provide important information for methods. window.setColor( Color.red ); Parameters are used to pass information to a method. Many methods need information in order to perform some operation. The parameters tell the method what to do and how to do it. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Parameters A parameter/argument is a channel used to pass information to a method. setColor() is a method of the Graphics class the receives a Color. void setColor(Color theColor) window.setColor( Color.red ); Most, if not all, of the Graphics class methods require parameters. The parameters communicate to the Graphics methods information about what needs to be done. The setColor() method changes the current drawing color to the color passed in. setColor() cannot be called without a color parameter. method call with parameter © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Parameters void fillRect (int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); method call with parameters The fillRect() method requires four pieces of information. fillRect() needs an x value, a y value, a width, and a heigth. fillRect() will draw a filled rectangle on the window at x,y with height and width as stated by the parameters. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Parameters void fillRect(int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); The call to fillRect would draw a rectangle at position 10,50 with a width of 30 and a height of 70. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value Java passes all parameters by VALUE. Primitives are passed as values by VALUE. References are passed as addresses by VALUE. Java passes all parameters by VALUE. What does that mean? Java makes a copy of what is being passed to the method. Because a copy of the original value is sent in, the original value sent in cannot be changed. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value Passing by value simply means that a copy of the original is being sent to the method. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value If you are sending in a primitive, then a copy of that primitive is sent. If you are sending in a reference or memory address, then a copy of that memory address is sent. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value public static void swap( int x, int y){ int t = x; x = y; y = t; out.println(x + " " + y); } //test code int one=5, two=7; out.println(one + " " + two); swap(one,two); OUTPUT5 7 7 5 5 7 The original value of one is copied and pasted into x. The original value of two is copied and pasted into y. What is the value of one? One stores a simple integer value. There is no connection between one and x other than the fact that they both store the same value. The same is true for two and y. Changes made to x do not have any affect on one. Changes made to y do not have any affect on two. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value public static void swap( int x, int y) { int t = x; x = y; y = t; } This attempted swap has local effect, but does not affect the original variables. Copies of the original variable values were passed to method swap. The original value of one is copied and pasted into x. The original value of two is copied and pasted into y. What is the value of one? One stores a simple integer value. There is no connection between one and x other than the fact that they both store the same value. The same is true for two and y. Changes made to x do not have any affect on one. Changes made to y do not have any affect on two. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value public static void swap(Integer x, Integer y){ Integer t=x; x=y; y=t; out.println(x + " " + y); } //test code Integer one=8, two=9; out.println(one + " " + two); swap(one,two); OUTPUT8 9 9 8 8 9 The original value of one is copied and pasted into x. The original value of two is copied and pasted into y. What is the value of one? One stores the location/address of an Integer Object. There is no connection between one and x other than the fact that they both store the same location/address. The same is true for two and y. Changes made to x do not have any affect on one. Changes made to y do not have any affect on two. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value public static void swap( Integer x, Integer y ) { Integer t=x; x=y; y=t; } The original value of one is copied and pasted into x. The original value of two is copied and pasted into y. What is the value of one? One stores the location/address of an Integer Object. There is no connection between one and x other than the fact that they both store the same location/address. The same is true for two and y. Changes made to x do not have any affect on one. Changes made to y do not have any affect on two. This attempted swap has local effect, but does not affect the original variables. Copies of the original references were passed to method swap. © A+ Computer Science - www.apluscompsci.com

passbyvalueone.java

© A+ Computer Science - www.apluscompsci.com Passing by Value public static void changeOne(int[] ray) { ray[0] = 0; ray[1] = 1; } //test code int[] nums = {5,4,3,2,1}; out.println(Arrays.toString(nums)); changeOne(nums); OUTPUT [5, 4, 3, 2, 1] [0, 1, 3, 2, 1] The original value of nums is copied and pasted into ray. What is the value of nums? Nums stores the location/address of an int[] array. Nums and ray store the same location/address of the same int[] array. Changing the location/address of ray would in no way affect the location/address stored in nums. Changing the values in the int[] array that both refer to does affect both. Both nums and ray store the exact same location/address of the exact same int[] array. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value public static void changeOne(int[] ray) { ray[0] = 0; ray[1] = 1; } Changing the values inside the array referred to by ray is a lasting change. A copy of the original reference was passed to method changeOne, but that reference was never modified. The original value of nums is copied and pasted into ray. What is the value of nums? Nums stores the location/address of an int[] array. Nums and ray store the same location/address of the same int[] array. Changing the location/address of ray would in no way affect the location/address stored in nums. Changing the values in the int[] array that both refer to does affect both. Both nums and ray store the exact same location/address of the exact same int[] array. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value public static void changeTwo(int[] ray) { ray = new int[5]; ray[0]=2; out.println(Arrays.toString(ray)); } //test code int[] nums = {5,4,3,2,1}; changeTwo(nums); out.println(Arrays.toString(nums)); OUTPUT [2, 0, 0, 0, 0] [5, 4, 3, 2, 1] The original value of nums is copied and pasted into ray. What is the value of nums? Nums stores the location/address of an int[] array. Nums and ray store the same location/address of the same int[] array. Changing the location/address of ray does not affect the location/address stored in nums. The code in changeTwo refers ray to a new int[] array. This change to ray does not affect nums as nums’ value was copied and pasted into ray. All changes made to the new array that ray refers to have affect in method changeTwo only. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Passing by Value public static void changeTwo(int[] ray) { ray = new int[5]; ray[0]=2; } Referring ray to a new array has local effect, but does not affect the original reference. A copy of the original reference was passed to method changeTwo. The original value of nums is copied and pasted into ray. What is the value of nums? Nums stores the location/address of an int[] array. Nums and ray store the same location/address of the same int[] array. Changing the location/address of ray does not affect the location/address stored in nums. The code in changeTwo refers ray to a new int[] array. This change to ray does not affect nums as nums’ value was copied and pasted into ray. All changes made to the new array that ray refers to have affect in method changeTwo only. © A+ Computer Science - www.apluscompsci.com

passbyvaluetwo.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com class A{ private String x; public A( String val ){ x = val; } public void change( ){ x = "x was changed"; public String toString(){ return x; class B{ public void mystery(A one, A two) { one = two; two.change(); //test code in the main in another class B test = new B(); A one = new A("stuff"); A two = new A("something"); System.out.println(one + " " + two); test.mystery(one,two); System.out.println(one + " " + two); Passing by Value OUTPUT stuff something stuff x was changed The original values of one and two are copied and pasted into mystery. What is the value of one? One stores the location/address of an A as does two. In mystery, one gets the location/address of one from the main and two gets the location/address of two from the main. In mystery, the location/address of two is copied to one. This change has local affect only. Calling the change method on two changes the instance variable x inside of the A Object referred to by two. This change affects the entire program as it changes the Object referred to and not the actual reference. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com class A{ private String x; public A( String val ){ x = val; } public void change( ){ x = "x was changed"; public String toString(){ return x; class B{ public void mystery(A one, A two) { one = two; two.change(); Passing by Value The original values of one and two are copied and pasted into mystery. What is the value of one? One stores the location/address of an A as does two. In mystery, one gets the location/address of one from the main and two gets the location/address of two from the main. In mystery, the location/address of two is copied to one. This change has local affect only. Calling the change method on two changes the instance variable x inside of the A Object referred to by two. This change affects the entire program as it changes the Object referred to and not the actual reference. A x-stuff A x-x was changed A x-something © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com class A{ private String x; public A( String val ){ x = val; } public void change( ){ x = "x was changed"; public String toString(){ return x; class B{ public void mystery(A one, A two) { one = two; two.change(); //test code in the main in another class B test = new B(); A one = new A("stuff"); A two = new A("something"); System.out.println(one + " " + two); test.mystery(one,two); System.out.println(one + " " + two); Passing by Value mystery() is passed the address of one and the address of two. two’s address is copied to one. This copy has local effect. two.change() changes the value in the A referred to by two. This change effects the entire program. The original values of one and two are copied and pasted into mystery. What is the value of one? One stores the location/address of an A as does two. In mystery, one gets the location/address of one from the main and two gets the location/address of two from the main. In mystery, the location/address of two is copied to one. This change has local affect only. Calling the change method on two changes the instance variable x inside of the A Object referred to by two. This change affects the entire program as it changes the Object referred to and not the actual reference. © A+ Computer Science - www.apluscompsci.com

passbyvaluethree.java

© A+ Computer Science - www.apluscompsci.com Work on Programs! Crank Some Code! © A+ Computer Science - www.apluscompsci.com

A+ Computer Science PARAMETERS © A+ Computer Science - www.apluscompsci.com