Parameter Passing in Java

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
How do Methods Work?. Let’s write a method that adds two integer values together and returns the result.
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,
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
Pointers Ethan Cerami Fundamentals of Computer New York University.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
1 CS161 Introduction to Computer Science Topic #10.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Week 12 Methods for passing actual parameters to formal parameters.
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.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Integer, Double, and Other Wrapper Classes … Sometimes a primitive value needs to be passed in as an argument, but the method definition creates an object.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Parameter Passing: Arrays 1.Create new variables (boxes) for each of the formal parameters allocated on a fresh stack created for this function call. int.
Implementing Subprograms
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
User-Written Functions
Introduction to Programming Using C
Building Java Programs
Principles of programming languages 4: Parameter passing, Scope rules
Introduction to Computing Using Java
Learning Objectives Pointers Pointer in function call
Programming Fundamentals Lecture #7 Functions
Advanced Programming Behnam Hatami Fall 2017.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Implementing Subprograms
Building Java Programs
March 29th Odds & Ends CS 239.
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Building Java Programs
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.
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from
Defining methods and more arrays
Subroutines – parameter passing
Array and Method.
CS2011 Introduction to Programming I Methods (II)
Simulating Reference Parameters in C
Initializing variables
Why did the programmer quit his job?
The Function Prototype
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Java Programming Language
Defining Classes and Methods
Names of variables, functions, classes
Lecture 03 & 04 Method and Arrays Jaeki Song.
A+ Computer Science PARAMETERS
Lecture 6: References and linked nodes reading: 16.1
C++ Array 1.
Building Java Programs
Implementing Subprograms
Week 7 - Monday CS 121.
C Parameter Passing.
Presentation transcript:

Parameter Passing in Java CS 239

General Rule In Java, all parameters are passed by value Value of actual parameter is copied into the formal parameter. The formal parameter holds a copy of the actual parameter

Specifics When the formal parameter is a primitive type, the formal parameter acts as a local variable. This means that changes made to the formal parameter inside the method do not affect the actual parameter. When the formal parameter is an object, what is copied in to it is not a value but an address. This means that changes made to the formal parameter inside the method may affect the actual parameter.

Clarification of Previous Slide Suppose you have a swap method which interchanges the two parameters where, for the moment type is not specified. public void swap (type x, type y) { type temp; temp = x; x = y; y = temp; }

When type is a primitive type public void swap (int x, int y) { int temp; temp = x; x = y; y = temp; } The formal parameters x and y hold copies of the actual parameter values and act as local variables. Changes made to formal parameters x and y do not affect the actual parameters.

When type is a simple object public void swap (Integer x, Integer y) { Integer temp; temp = x; x = y; y = temp; } Since x and y are objects, the formal parameters x and y hold copies of the actual parameter addresses which act as local variables. This swap locally swaps the addresses stored in x and y. Changes made to formal parameters x and y do not affect the values stored in the actual parameters.

When type is a object public void swap (Int [] numbers, int x, int y) { int temp; temp = numbers[x]; numbers[x] = numbers[y]; numbers[y] = temp; } Here, formal parameters x and y have local copies of values which represent positions in an array. Nothing is being done to them. Formal parameter numbers holds the address of an array which holds int values. When you access numbers[x], you are asking for that value stored there. Thus swapping numbers[x] and numbers[y] swaps the values stored in those positions in the actual parameter corresponding to numbers.