Docsity

Grouping Objects - Introduction to Programming in Java - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Grouping Objects - Introduction to Programming in Java - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

  • Objectives
    • discuss call-by-value and call-by-reference,

arrays, collections, and iterators

  1. Grouping Objects

Topics

• 1. Parameter Passing

• 2. Arrays: Similar but Different

• 3. Call-by-Reference with Classes

• 4. Grouping Objects

• 5. A Notebook Example

• 6. Iteration (looping)

• 7. An Auction Example

• 8. Fixed-size Collections

• 9. More Information

What is Call-by-Reference?

• An example for an imaginary language:

function foo()

integer x := 2;

bar(x);

print(“x is” + x);

function bar( ref integer w)

w := 5;

“x is 5” is printed

continued

  • Call-by-reference creates a link from the variable in the called function (e.g. w in bar()) to the original variable (e.g. x in foo()) - when w changes, x is also changed

Call-by-Value Example

public class SimpleCalls { public static void main(String[] args) { int x = 3; System.out.println("1. x = " + x); squareBad (x); // x = squareGood (x); System.out.println("2. x = " + x); } // end of main()

continued

private static void squareBad(int x) { System.out.println("sqBad 1. x = " + x); x = xx; System.out.println("sqBad 2. x = " + x); } private static int squareGood(int x) { System.out.println("sqGood 1. x = " + x); x = xx; System.out.println("sqGood 2. x = " + x); return x; } } // end of SimpleCalls class

static is used so that main() can call

these methods without creating an object first;

it has nothing to do with parameter passing

2. Arrays: Similar but Different

• Java arrays look like C arrays, but...

• Java arrays do not support pointer

manipulation.

• Arrays are objects , and so are passed to

methods using call-by-reference.

Declaring and Allocating Arrays

  • Some coding styles: int c[] = new int[12]; // creates a 12 element int array c[0] = 2 ;
  • or int c[]; // declares an array; no memory yet c[0] = 2 ; // ERROR! c = new int[12]; // allocates memory c[0] = 2; // OK - declare the type (e.g. int) - allocate memory with new

continued

A Different Syntax

• Instead of:

int c [] = new int[12];

• Can write:

int [] c = new int[12];

UseArray.java

import javax.swing.JOptionPane; public class UseArray { public static void main(String[] args) { int n[]; // declare array name n = new int[10]; // allocate memory to array // no values stored in n[], so will contain 0's String output = "Cell Value\n"; for(int i = 0; i < n.length; i++) output += "n[" + i + "] == " + n[i] + "\n"; JOptionPane.showMessageDialog( null, output, "Using an Array", JOptionPane.INFORMATION_MESSAGE ); } // end of main() } // end of UseArray class

two

steps

Notes

• n is declared: its type is specified

int n[]

• n is allocated memory with new

n = new int[10];

• n.length

– length always holds the length of the array

object (i.e. 10 in this case)

object

n

Using an Array

• Square brackets are used to access an array

element:

n[i]

• Array elements are used like ordinary variables

  • on the left of an assignment: n[0] = 3;
  • in an expression: x = n[1] – 3; n[i]++;

Example

for( int i = 0; i < n.length; i++) { System.out.println(i + ": " + n[i]); } int i = 0; while(i < n.length) { System.out.println(i + ": " + n[i]); i++; } for loop version while loop version

i only exists inside

the loop

Passing Arrays to Methods

• Arrays are Java objects

– they are passed to methods using

call-by-reference

– i.e. changes to an array inside a method

affects the original

– no return or pointers are required