1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: Parameter.
1 Repetition structures Overview while statement for statement do while statement.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
1 Modelling 1-D Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing Array.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
INF 523Q Chapter 5: Enhancing Classes (Examples).
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Introduction.
1 Memory Model of A Program, Methods Overview l Memory storage areas for an executing program l Introduction to methods and methods definitions l General.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
1 Introduction to Abstraction Overview l Introducing the concept of abstraction l Functional abstraction l Functional abstraction in action l Practice.
1 Repetition structures Overview while statement for statement do while statement.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Computer Programming Lab(5).
Saravanan.G.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference.
Methods Chapter Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
3: Controlling Program Flow Using Java operators Mathematical operators Relational operators Logical operators –Primitive type: ALL (the same with C) –String:
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Building java programs, chapter 3 Parameters, Methods and Objects.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Chapter 5 : Methods. Why Write Methods?  Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
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.
Classes - Intermediate
Methods.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Declaring console static and global import java.util.*; public class Test { static Scanner console = new Scanner (System.in); public static void main(String[]
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Staples are our staple Building upon our solution.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Java Memory Management
Java Memory Management
Methods.
More Object Oriented Programming
Lecture 11 C Parameters Richard Gesick.
March 29th Odds & Ends CS 239.
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.
Object Oriented Programming
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
CS2011 Introduction to Programming I Methods (II)
JAVA Constructors.
class PrintOnetoTen { public static void main(String args[]) {
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 6 – Methods Topics are:
Why did the programmer quit his job?
ITE “A” GROUP 2 ENCAPSULATION.
C Parameter Passing.
Presentation transcript:

1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data modeling

2 Parameter Passing Revisited l In Java variables of primitive types are Passed by value while objects are passed by reference l “Passing by value” means that the argument’s evaluated value is copied, and is passed to the method. »Inside the method this copy can be modified at will, and does not affect the original argument l Passing by reference means that a reference to (i.e., the address of) the argument is passed to the method. »Using the reference, the called method is actually directly accessing the argument, not a copy of it »Any changes the method makes to the parameter are made to the actual object used as the argument »So after you return from the method, that object will retain any new values set in the method

3 Parameter Passing : Passing Values import java.io.*; import TextIO; class ParameterPassing { static TextIO inputStream = new TextIO(System.in); static void modifyParameter (int num) { num *= num + 3; System.out.println("Value inside method modifyParameter() is: ” + num); } public static void main (String[] args) throws IOException { int n, n1; System.out.println("Enter an integer:"); n = inputStream.readInt(); System.out.println("Value before invoking modifyParameter() is: " + n); modifyParameter(n); System.out.println("Value after invoking modifyParameter() is: " + n); }

4 Parameter Passing : Passing Values public class ParameterPassing2 { static void f(char y) { y = 'z'; } public static void main(String[] args) { char x ; x = 'a'; System.out.println("1: x: " + x); f(x); System.out.println("2: x: " + x); }

5 A Brief Overview of Objects l Before considering passing objects as parameters, let us have a flavor of what objects are. l Real-world objects share two characteristics: they have state and they all have behavior. l Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods. l Definition: An object is a software bundle of variables and related methods through whose execution the variables are updated. An object is a run-time entity.

6 Parameter Passing : Passing Objects class Letter { char c; } public class ParameterPassing3 { static void f(Letter y) { y.c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; System.out.println("1: x.c: " + x.c); f(x); System.out.println("2: x.c: " + x.c); }

7 Parameter Passing : Passing Objects class ParameterPassing4 { public static void main(String [] args) { int i; StringBuffer sb = new StringBuffer(“Hello “); double d = 5.5; System.out.println("After initialization"); System.out.println(sb.toString()); System.out.println("d = " + d); System.out.println("Calling changeParams.."); changeParams(sb, d); System.out.println("Back from changeParams");

8 Parameter Passing : Passing Objects Parameterpassing4 continued: System.out.println(sb.toString()); System.out.println("d = " + d); } private static void changeParams(StringBuffer s,double dd){ int i; dd = 3.14; sb.add(“World”); System.out.println(“sb = “ + sb.toString()); System.out.println("dd = " + dd); }

9 Parameter Passing : Passing Objects class Person { int age; String name; public void print(){ System.out.println(age + " " + name); } public Person(int a, String b) { age = a; name = b; } What will the following output?

10 Parameter Passing : Passing Objects class Test { static int i = 10; public static void main(String[] args) { String str = "I think, therefore I am. I think."; Person p1 = new Person(21, “Khalid"); Person p2 = new Person(20,“Amr"); mixUp(i, str, p1, p2); System.out.println("i: " + i + " str: " + str); p1.print(); p2.print(); } static void mixUp(int i, String str, Person one, Person two) { i++; str = "First things first, but not necessarily in that order."; one = two; one.age = 34; one.name = “Ali"; }