COMP 250
INTRODUCTION TO COMPUTER SCIENCE
Week 2-3: Reference types, and Random
Giulia Alberini, Fall 2020
WHAT ARE WE GOING TO DO IN THIS VIDEO?
Reference types Random
REFERENCE TYPES
PRIMITIVE VS REFERENCE TYPES
Both arrays and Strings are Objects.
In java, except for the primitive data types (those whose names start with lowercase letters like int, double, etc.), everything is an Object.
Variables of Objects, arrays included, don’t store the values of the objects, but a reference to the location in memory containing that value. You can think of it as an address which points to where the data is located in memory.
REFERENCE TYPES
PRIMITIVE VS REFERENCE TYPES – EXAMPLES
public static void main(String[] args) { int x = 5;
int y = x;
x++;
System.out.println(x + ” ” + y);
}
What does the program print?
65
PRIMITIVE VS REFERENCE TYPES – EXAMPLES
public static void main(String[] args) { int[] x = {1, 2, 3};
int[] y = x;
y[0] = 4;
System.out.println(x[0] + ” ” + y[0]); }
What does the program print?
44
PRIMITIVE VS REFERENCE TYPES – EXAMPLES
public static void main(String[] args) { int x = 5;
example(x);
System.out.println(x);
}
public static void example(int x) { x = x*5;
}
What does the program print?
5
PRIMITIVE VS REFERENCE TYPES – EXAMPLES
public static void main(String[] args) { int[] x = {1,2,3};
example(x);
System.out.println(x[0]);
}
public static void example(int[] x) {
x[0] = 4; }
What does the program print?
4
ARRAY VS STRING
Both arrays and strings are reference types.
Variables of array-type and String-type both store the address
in memory at which the elements of the object begins.
Arrays are mutable, Strings are immutable!!
Once a String has been created it cannot be changed!
The elements of an array can be updated anytime we want.
REFERENCE TYPES
String[] pets = {“cat”, “dog”};
“cat”
pets 0 1
“dog”
REFERENCE TYPES
String[] pets = {“cat”, “dog”};
pets[0] = pets[0] + “s”;
pets 0 1
“cat”
“cats”
To note:
• We changed the array. Arrays are mutable the
reference stored in pets did not change!
• We changed the first String. Strings are immutable
the reference in pets[0] did change!
“dog”
ARRAY VS STRING – EXAMPLE 1
public static void main(String[] args) { int[] x = {1, 2, 3, 4};
myMethod(x); System.out.println(Arrays.toString(x));
}
public static void myMethod(int[] a) {
for(int i=0; i
String[] days = new String[7];
int numLettersMonday = days[0].length();
NullPointerException
RANDOM
THE RANDOM CLASS
Up to now you probably learned how to use Math.random() to get random numbers between a minimum value and a maximum value.
We can also use the Random class to generate random numbers.
The Random class allows us to seed the random numbers such that we
will see the same sequence of ‘random’ numbers each time.
Why is it useful?
Easier to debug code that is not working.
Comparing outputs from different codes (for instance your assignments)
HOW TO USE RANDOM
FirstimporttheRandomclass:addimport java.util.Random;
Then you can create a random number generator using the following statement:
int seed = 123;
Random randomGenerator = new Random(); Random otherGenerator = new Random(seed);
HOW TO USE RANDOM
FirstimporttheRandomclass:addimport java.util.Random;
Then you can create a random number generator using the following statement:
int seed = 123;
Random randomGenerator = new Random(); Random otherGenerator = new Random(seed);
Declaration of two variables of type Random.
HOW TO USE RANDOM
FirstimporttheRandomclass:addimport java.util.Random;
Then you can create a random number generator using the following statement:
int seed = 123;
Random randomGenerator = new Random(); Random otherGenerator = new Random(seed);
Declaration of two variables of type Random.
Creation of a Random object. Note the new keyword! Random is a reference type.
METHODS IN RANDOM
https://docs.oracle.com/javase/7/docs/api/java/util/Random.html
DICE ROLL
Here’s an example of using Random to simulate a dice roll
Random randomGenerator = new Random();
int diceRoll = randomGenerator.nextInt(6) + 1; System.out.println(“The dice rolled ” + diceRoll);
In the next video we will be talking about errors and exceptions, as well as try/catch blocks.