Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
C Language.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
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,
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Written by: Dr. JJ Shepherd
Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Java Basics M Taimoor Khan
Lecture 2: Object Oriented Programming I
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Enhancing classes Visibility modifiers and encapsulation revisited
Java Syntax Primitive data types Operators Control statements.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Understanding class definitions Looking inside classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Writing Classes (Chapter 4)
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
CSC 212 Object-Oriented Programming and Java Part 1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
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.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
Methods We write methods in our programs for many reasons:
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
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.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Chapter 5: Enhancing Classes
JAVA MULTIPLE CHOICE QUESTION.
3 Introduction to Classes and Objects.
Introduction to Programming in Java
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
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.
Understanding Parameter Passing
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Programs and Classes A program is made up from classes
Chapter 6 – Methods Topics are:
Defining Classes and Methods
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Presentation transcript:

Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

Parameter Passing – Primitive Type Java passes all parameters to a method by value. The actual parameters (the values that are passed in) are assigned to the formal parameters (declared in the method header). This means that the value of the actual parameter is copied into the formal parameter.

Parameter Passing – Primitive Type In case of primitive parameter types, the formal parameter is a copy of the actual parameter. In case of a reference type parameter (to an object), the formal parameter is an alias of the actual parameter.

Parameter Passing –Primitive Type Example public class PrimitiveParameter { public void changeValue (int parameter) {parameter = 20;} public static void main (String args[]) { int parameter = 10; PrimitiveParameter test = new PrimitiveParameter(); test.changeValue(parameter);} }

Parameter Passing –Primitive Type Example In the main method: –The “parameter” is declared and assigned the value 10 (the actual parameter). –After the new class instance creation the method changeValue is called. As a result of the call a copy of formal parameter is made and sent to the method.

Parameter Passing – Primitive Type Example In the changeValue method: –The method refers to the formal parameter, not to the actual parameter. –The formal parameter is changed to 20. –When the method is left the formal parameter is destroyed and does not exist anymore. In the main method: –The “parameter” is still 10.

Parameter Passing – Reference Type When an object is passed to a method, Java passes a reference to that object. The value that is copied is the address of the actual object. Therefore it is important to understand that when a formal parameter object is used it modifies the actual object state permanently.

Parameter Passing – Reference Type Example public class RefParam { public void changeObject (Car parameter) {parameter.changeParameter();}... RefParam change = new RefParam(); parameter = new Car(); change.changeParameter(parameter); }

Parameter Passing – Reference Type Example In code section: –When the changeObject method is called, a formal object parameter is made and sent. In changeObject method: –The changeParameter method calls the actual object’s method. –This causes the state change of the object.

Parameter Passing – Reference Type Example In code section: –The formal parameter does not exist anymore, but the object still exists in its changed state. –In case the value of the formal parameter is changed it points then to a different object.

Parameter Passing – Applet Parameters are also passed from HTML files to Java applets. –The HTML file passes parameters as string types, therefore they need to be converted if used as another type. –The parameters are initialized in applet tag section of HTML file and retrieved in init() method of Java applet.

Parameter Passing – Applet Example An HTML file code section:

Parameter Passing – Applet Example The Java applet code section: init() { String FontName = getParameter( “ fontName ” ); String FontSize = getParameter( “ fontSize ” ); String Leading = getParameter( “ leading ” ); String PaidUp = getParameter( “ accountEn ” );... }

Methods and Parameters Methods are the cornerstone of OO programming, the basic unit of action Parameters allow the caller of the Method to affect its outcome by providing a value or values when it is invoked Without parameters, adjusting the Method outcome based upon invoker state would be quite cumbersome, if not impossible

Primitive Parameters Primitive types: boolean, byte, char, short, int, long, float, double In Java, all primitives are passed by value. This means a copy of the value is passed into the method Modifying the primitive parameter in the method does NOT change its value outside the method

Array Parameters Java has native support for Arrays as parameters A Java array is self-aware of its own length, unlike C/C++. All array information array can be passed as a single parameter, no length parameter is needed. Arrays are passed in the same way as Objects

Object Parameters Anything that is not a primitive or array falls under the umbrella of Object Objects can be passed natively, just like primitives or arrays In Java, handles to anything that is not primitive or an array is a reference. Does this mean Java passes Object parameters by Reference?

Objects are passed by Value It is often misstated that Object parameters are passed by Reference. While it is true that the parameter is a reference to an Object, the reference itself is passed by Value. This means modifications to the Object will be seen externally, but pointing the reference to another object will not.

Modify a Parameter Value … Point p = new Point(10, 20); addTen(p); System.out.println(p.x + “, “ + p.y); … void addTen(Point p) { p.x = p.x + 10; p.y = p.y + 10; }

Modify a Parameter Value The output of the System.out.println will be 20, 30 What if you do not want an outside method modifying your parameter? Ways to handle this will be explored later

Modify a Parameter Reference … Point p1 = new Point(10, 20); Point p2 = new Point(30, 40); System.out.println(p1.x + “, “ + p1.y); swap(p1, p2); System.out.println(p1.x + “, “ + p1.y); …

Modify a Parameter Reference // Does not work in Java! void swap(Point p1, Point p2) { Point tmp = p1; p1 = p2; p2 = tmp; }

Modify a Parameter Reference The output of BOTH System.out.println calls will be 10, 20 Note that this differs greatly from reference passing in C++. If modified for syntax (including the & modifier), the previous method would have performed a swap operation if written in C++

Parameter Protection Since methods can modify the Objects they receive as parameters, we can not be sure of the object state after it is used as a parameter, other than it is still a reference to the same Object. What about final parameters, does that protect the parameter from modification?

final Parameters The final keyword can be used as a modifier in the formal parameter list This asserts (at the compiler level) that the actual parameter reference will not be pointed to a different object. Unfortunately, this does not protect our parameter’s internal values

Parameter Protection Check your object for equality before and after the method call –expensive –tedious –error prone Use an Immutable object –often self-identifying and documenting

Immutable Parameters The only guarantee that a parameter’s internal values can not be changed is if an immutable object, or some construct that enforces the concept of immutability is used Care must be taken for containers –Contents must be immutable –Iterator must not allow add/remove operations

References Mary Campione. The Java Tutorial Third Edition, book. Addison-Wesley, December 22, Holzner, S., (2001). Java 2 Black Book. Scottsdale: Coriolis. Patric Naughton. The Java Handbook, book. Green Hills Publishing, October, Xiaoping Jia. Object-Oriented Software Development Using Java Second Edition, book. Addison-Wesley, 2002.