CS2011 Introduction to Programming I Arrays (II)

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
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,
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Run-Time Storage Organization
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Sadegh Aliakbary Sharif University of Technology Spring 2011.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
Topic: Classes and Objects
Java Memory Management
Java Memory Management
CSCI 162 – Introduction to Programming II William Killian
AKA the birth, life, and death of variables.
Building Java Programs
Dr. Kyung Eun Park Summer 2017
Week 8 - Friday CS 121.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Building Java Programs
Building Java Programs
Objects First with Java CITS1001
Advanced Programming Behnam Hatami Fall 2017.
CSC 253 Lecture 8.
Starting Out with Java: From Control Structures through Objects
Building Java Programs
Building Java Programs Chapter 7
Chapter 6 Arrays Solution Opening Problem
CSC 253 Lecture 8.
Building Java Programs
An Introduction to Java – Part II
Stack Memory 2 (also called Call Stack)
An Introduction to Java – Part I, language basics
Building Java Programs
Chapter 8 Slides from GaddisText
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from
Building Java Programs
Building Java Programs
Why did the programmer quit his job?
Object Oriented Programming in java
Building Java Programs
CS2011 Introduction to Programming I Objects and Classes
CS2011 Introduction to Programming I Loop Statements (II)
CS2011 Introduction to Programming I Methods (II)
CS2011 Introduction to Programming I Methods (I)
CS2011 Introduction to Programming I Arrays (I)
Chapter 6 Methods.
AKA the birth, life, and death of variables.
Assessment – Java Basics: Part 1
CS2011 Introduction to Programming I Multidimensional Arrays
Why did the programmer quit his job?
CS2011 Introduction to Programming I Strings
CS2011 Introduction to Programming I Selections (I)
Suggested self-checks: Section 7.11 #1-11
CSS161: Fundamentals of Computing
Chapter 9: Pointers and String
Building Java Programs
Building Java Programs
Building Java Programs
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Classes and Objects Object Creation
Building Java Programs
CMSC 202 Constructors Version 9/10.
Presentation transcript:

CS2011 Introduction to Programming I Arrays (II) Chengyu Sun California State University, Los Angeles

Primitive Types Are Passed By Value If a parameter is of a primary type (e.g. int and double), the value of the argument is passed to the parameter public static void inc( int n ) { ++n; } public static void main( String args[] ) { int n = 10; inc(n); System.out.println(n);

Arrays Are Passed By Reference Why the difference? public static void inc( int[] n ) { ++n[0]; } public static void main( String args[] ) { int[] n = {10}; inc(n); System.out.println(n[0]);

Copy An Array Is b a copy of a, or are they the same array?? int[] a = {1, 2, 3}; int[] b = a; b[0] = 10; System.out.println( a[0] ); Is b a copy of a, or are they the same array?? How do we make a copy of an array??

Return An Array Notice the return type public static int[] createArray( int n ) { int[] a = new int[n]; for( int i=0 ; i < a.length ; ++i ) a[i] = i + 1; return a; } Notice the return type Why it works when a is a local variable?

Heap JVM divides the memory it uses to run a program into a stack and a heap Stack is the part of the memory that stores activation records (i.e. local variables and parameter) Heap is the part of the memory that stores data that is created with the new keyword (i.e. objects and arrays)

Array Reference Stack Heap Stack Heap create Array() a main() main() b a references the array created on the heap The array references is returned to main() and assigned to b

Garbage Collection Data on the heap will be removed when the JVM determines that it's no longer used by any code (i.e. no more references to the data), and this process is known as Garbage Collection

Variable-Length Argument List A.K.A. varargs Allows any number of arguments of the specified type The arguments are treated as an array in the method public static int sum( int… numbers ) { int sum = 0; for( int i=0 ; i < numbers.length ; ++i ) sum += numbers[i]; return sum; }

Arrays Methods Arrays contains some useful methods for common array operations Examples copyOf equals sort binarySearch

Readings Chapter 7 of the textbook (there will be a quiz next week)