© A+ Computer Science - www.apluscompsci.com. In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.

Slides:



Advertisements
Similar presentations
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Advertisements

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,
1 Various Methods of Populating Arrays Randomly generated integers.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
© 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 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
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.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Introduction to C# By: Abir Ghattas Michel Barakat.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
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.
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.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
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.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Chapter 9 A Second Look at Classes and Objects - 2.
Functions (Methods) Pascal C, C++ Java Scripting Languages
OOP Powerpoint slides from A+ Computer Science
More Object Oriented Programming
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Object Oriented Programming COP3330 / CGS5409
An Introduction to Java – Part II
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.
Variables and Their scope
© 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
Understanding Parameter Passing
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.
See requirements for practice program on next slide.
© A+ Computer Science - Variables & Data Types © A+ Computer Science - ©A+ Computer Science
Lecture 03 & 04 Method and Arrays Jaeki Song.
A+ Computer Science PARAMETERS
C Parameter Passing.
Presentation transcript:

© A+ Computer Science -

In Java, any variable that refers to an Object is a reference variable. The variable stores the memory address of the actual Object.

© A+ Computer Science - String x = new String("Chuck"); String y = x; x and y store the same memory address. "Chuck" xy 0x2B3

© A+ Computer Science - String x = "Chuck"; String y = "Chuck"; x and y store the same memory address. "Chuck" x y 0x2B7

© A+ Computer Science - "Chuck" String x = new String("Chuck"); String y = new String("Chuck"); x and y store different memory addresses. x y 0x2B7 0x2FE

© A+ Computer Science - String x = "Chuck"; String y = "Chuck"; "Chuck" x y 0x2B7 x = null; null

© A+ Computer Science -

A parameter/argument is a channel used to pass information to a method. Parameters provide important information for methods. window.setColor( Color.red );

© A+ Computer Science - 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 ); method call with parameter

© A+ Computer Science - void fillRect (int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); method call with parameters

© A+ Computer Science - 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 - Java passes all parameters by VALUE. Primitives are passed as values by VALUE. References are passed as addresses by VALUE.

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

© A+ Computer Science - 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 - 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); out.println(one + " " + two); OUTPUT

© A+ Computer Science - 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.

© A+ Computer Science - 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); out.println(one + " " + two); OUTPUT

© A+ Computer Science - public static void swap( Integer x, Integer y ) { Integer t=x; x=y; y=t; } 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 -

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); out.println(Arrays.toString(nums)); OUTPUT [5, 4, 3, 2, 1] [0, 1, 3, 2, 1]

© A+ Computer Science - 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.

© A+ Computer Science - 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]

© A+ Computer Science - 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.

© A+ Computer Science -

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); OUTPUT stuff something stuff x was changed

© A+ Computer Science - 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(); } A x-something A x-stuff A x-x was changed

© A+ Computer Science - 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. 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);

© A+ Computer Science -