CSE 14 Lecture Notes - Lecture 11: Dynamic Array

41 views5 pages
8 May 2018
School
Course
Professor
same object
CMPS12A Lecture 11: Functions with Arrays
A reference variable is basically a variable that stores the address of an object, which is basically
what you have done ever since learning Java.
This is a picture of memory on how a reference variable looks like, with the name of variable
pointing towards the actual value of what is stored to that variable.
Let us recall that the primitive types are:
byte, short, int, long, char, float, double, Boolean
Reference types are:
“trig, “aer, … all array types like Boolea[], “trig[], it[], et.
So one example on how memory looks like when making values equal something:
Example:
int a = 10, b;
b = a;
b = 12;
Reference variables are a bit more different, as making one variable equal another variable
does not copy but rather make both variables now point towards the address of the object. This
basically means this:
var
a
b
a
a
b
b
10
10
10
10
12
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
Example:
int [] A = {1, 2, 3}
int [] B;
B = A;
B[2] = 10; //changes 3 to 10 since index 0 is at 1 and index 2 is at 3
System.out.println(A[2]); //prints out 10 since both point to same address
To print out arrays, it is different than just trying to print out a variable since an array stores an
item in each index, thus if you have something like this:
Example:
class ArrayExample{
public static void main (String [] args) {
int [] a = {1, 2, 3};
int [] b = {4, 5, 6, 7};
A
B
1
2
3
A
B
1
2
3
10
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

A reference variable is basically a variable that stores the address of an object, which is basically what you have done ever since learning java. var same object. This is a picture of memory on how a reference variable looks like, with the name of variable pointing towards the actual value of what is stored to that variable. Let us recall that the primitive types are: byte, short, int, long, char, float, double, boolean. Tri(cid:374)g, (cid:272)a(cid:374)(cid:374)er, all array types like boolea(cid:374)[], tri(cid:374)g[], i(cid:374)t[], et(cid:272). So one example on how memory looks like when making values equal something: a. Reference variables are a bit more different, as making one variable equal another variable does not copy but rather make both variables now point towards the address of the object. Example: int [] a = {1, 2, 3} int [] b; //changes 3 to 10 since index 0 is at 1 and index 2 is at 3.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions