Data types and Arrays Flashcards
What are some characteristics of Primitive data?
Aspects of primitive data:
* When working with primitive data types Java stores them directly in memory
* When you copy a primitive variable, only the data is copied, creating a separate variable that holds the same value
* When working with primitives you can use mathematical operators to perform direct calculations like +, -, / and *
* You can use shortcuts to update the value of a primitive value like ++, – or += examples: int, double, boolean, char, byte, short, long, float
What are some characteristics of Object data?
Aspects of Object data:
* An object is a method of storing multiple pieces of data and the methods who act on this data under a single data type
* This means within a single object you could have multiple pieces of data including both primitives and other objects
* Because objects have the potential to store large amounts of data Java stores them in memory differently from primitives, storing the location for the data in the actual variable as a “reference”
* When you are working with Objects you can’t use the mathematical operators or shortcuts, instead you need to rely upon the methods provided by the object like “string”.equals(“string”) or scanner.nextInt()
What is Null?
Each primitive has its own associated “zero value”. This is a value that java uses as a stand in when creating a place in memory for a primitive before you actually give it a value to store.
All object types share a single “zero value”- null
Null means literally “no object”, which is different from an empty object. For example, if you try to use the String’s lengh method on a null string you get an error. However, calling length on an empty string is perfectly valid.
String temp; // if you don’t initialize a String it defaults to null temp.length(); // causes a “Null Pointer Exception”
String empty = “”;
empty.length(); // returns 0;
What is an Array?
The array is an object that holds multiple pieces of the same type of data. Within the structure each individual piece of data is called an “element” and it has its own address within the array called an “index”
What is the Syntax to declare an array?
The syntax to declare an array is:
dataType[] arrayVariableName = new dataType[numberOfElementsToStore];
The dataType is whatever type of data you want each index to store. An array can store multiple items, but they must all be of the same data type.
For example, you may have a list of ints that represent a single student’s grades across 10 different exams:
int[] studentGrades = new int[10];
The number between the square brackets is the capacity, or how many individual indexes there will be within the array. Keep in mind that once you create the array the capacity cannot be changed. In our above example it will always be 10, the array cannot grow or shrink.
When you first create an array it is filled with “zero values”. Each primitive has an associated zero value, where as the zero value for objects is null. Null literally means “no object”.
What is the Syntax to add an element to an array?
Here is the syntax to update the value stored in an index:
arrayName[index] = value;
In our example of student grades, let’s say we want to add in the first three test scores:
studentGrades[0] = 98;
studentGrades[1] = 86;
studentGrades[2] = 90;
This would result in an array with the following values:
What is the Syntax to add an element to an array and fill it with values in a single statement?
dataType[] name = {dataValue1, dataValue2, dataValue3};
The computer can determine the capacity from how many values you provide and places them in the array according to the order you specify.
For example, if we create an array of exam scores like so:
int[] studentGrades = {98, 86, 90};
This would result in an array with the following values:
What are some common Array methods?
Java also provides an Arrays class that has a series of methods you can use on your own array instances. If you have ever used the Java Math class you will find the syntax very familiar. You start the statement with “Arrays” followed by the method you want to use and pass in the array instance you want to operate on as a parameter.
int a = 10;
double aSq = Math.pow(a, 2);
int[] b = new int[10];
Arrays.toString(b);
These functions do not come included by default, so if you want to use them you have to include the following import statement.
import java.util.Arrays;
Here is a table of some of the most useful array methods:
What is the Syntax to return the data stored in an array?
The data type of an array includes whatever data it stores, so if you want to return an array of ints you would write a method header that looks like this:
public static int[] myMethod(int[] a) {}
If instead you wanted to return an array of doubles your method header would look like this:
public static double[] myMethod(double[] a) {}
Explain how one would trigger the “IndexOutOfBoundsException” error message
This exception this means that you have tried to access an index of the array that doesn’t exist. This could mean you are trying to access a negative index, but more likely it means you tried to access an index beyond the capacity of the array. This is a common mistake because the index of the last element is one fewer than the capacity. For example, an array of size 5 only has indexes 0 through 4, so if you tried to access index 5 you would get an IndexOutOfBounds exception.
However, arrays can tell you what their capacity is so you can protect yourself from making this mistake.
arrayName.length // returns the capacity of the array
You can use this to get the value of the last valid element like so:
arrayName[arrayName.length-1] // the last valid index
What is the “length” functionality in arrays and what is the Syntax?
arrayVariableName.length // returns the capacity of the array
This value is stored as a “field”, or a variable that exists within the entire scope of the object. To access it we call it by the name “length”. This functionality is built into Arrays by Java. You use the length field on the individual instance of an array, so you get the length of that specific array.
For example, if you had multiple different arrays here’s how you would use the length field for each:
int[] a = new int[5];
double[] b = new double[10];
char[] c = new char[20];
int aLength = a.length; // returns 5
int bLength = b.length; //
returns 10 int cLength = c.length; // returns 20
How do arrays and loops work together?
As arrays are often used to solve problems at a scale individual primitive variables can’t handle you will likely need to use a loop to move over the array in an efficient way.
For loops and arrays make great partners as you can use the for loop variable to access each individual index in the array like so:
for (int index = 0; index < a.length; index++) {
a[index] = value;
}
Notice that you’ll want to set the bounds of the for loop based on the array’s length, this will help protect you from running off the end of the array and getting an ArrayIndexOutOfBounds exception. You’ll likely want to start the for loop variable at 0 and go until array.length-1, that way the for loop variable will perfectly match the array indexes.
For example, after this code:
int[] a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
the array “a” will store these values:
What is the fundamental difference between how Java stores primitive data and object data in memory?
Primitives are stored directly in memory, so when you copy a primitive variable the value is copied, leaving the original variable unaffected. This is called value semantics.
Since objects are more complicated Java doesn’t store their data directly in memory with the variable name, instead it creates a special space in memory for the data and then stores the address of that location in the variable. This means that any variable holding an object (like an array) actually stores a reference to the object. Therefore, when you make a copy of an object variable, you copy the reference, which still points at the original data. This is called reference semantics.
How do individual arrays set equal to each other affect one another when altered?
Let’s say you create an array of ints:
int[] a = new int[5];
The variable “a” stores a reference to where the data for the array is going to be stored. Now let’s say we make a new array variable and set it equal to “a”, what we’re actually copying is the reference to the original data. Meaning if we alter something in our second variable it affects the data stored under a.
int a[] = new int[5]; // [0, 0, 0, 0, 0]
a[0] = 10; // [10, 0, 0, 0, 0]
int b[] = a; // [10, 0, 0, 0, 0]
b[0] = 5; // [5, 0, 0, 0, 0]
System.out.println(“a = “ + Arrays.toString(a)); System.out.println(“b = “ + Arrays.toString(b));
The two printlns at the end of this code would produce the following output:
a = [5, 0, 0, 0, 0]
b = [5, 0, 0, 0, 0]
What happens when you pass an array as a parameter?
When you pass an array as a parameter, the reference is passed into the method, which then alters the original array.
public static void main(String[] args) {
int[] a = new int[5];
System.out.println(“before method: “ + Arrays.toString(a)); myMethod(a);
System.out.println(“after method: “ + Arrays.toString(a));
}
public static void myMethod(int[] b) {
b[0] = 5;
}
The output of the above code would be:
before method: [0, 0, 0, 0, 0]
after method: [5, 0, 0, 0, 0]
Which means the original array “a” was changed by the method without having to return the results and save them back at the call site. This is because when you pass an array variable as a parameter it is the reference, not the actual array data that is passed. Arrays can store any type of data you like, so they can store objects. This means that each element then stores a reference to another object somewhere in memory and the rules of reference semantics still apply. That means that if you copy the value stored at this index you are again copying a reference to an object and not the data itself.