L30 - CompSci 101 Lecture 31 1 Computer Science 101 SS C...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CompSci 101 Lecture 31 1 Computer Science 101 SS C Lecture 30 Contents Passing primitives as parameters Passing objects as parameters Arrays of Point objects Arrays of Rectangle objects Passing arrays as parameters 1 2 3 4 5 6 7 8 9 10 11 2 Passing primitive parameters When we pass a parameter of a primitive type to a method a copy of the parameter is made. This copy is local to the method. public class Program { public void start () { int num1 = 15; methodA(num1 + 1); System.out.println("num1: " + num1); } private void methodA (int num) { num = 33; System.out.println("num: " + num); } } (1) start() num1 15 num 16 (2) methodA() Inside method1(), the parameter, num, contains a separate copy of num1. Assigning 33 to the variable num, does NOT effect the variable, num1, in the start() method. 33 3 Passing object parameters Object variables hold a reference or a pointer to an object of the type of the variable: String name = "Jed"; Point p1 = new Point(3, 5); When we pass an object to a method as a parameter we make a copy of the REFERENCE to the object; we do not make a copy of the object itself. This can lead to unexpected results....
View Full Document

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 2

L30 - CompSci 101 Lecture 31 1 Computer Science 101 SS C...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online