int

int[] array = {1, 2, 3, 4, 5};

for (int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
}
1
2
3
4
5

double

double[] doubleArray = {1.2, 2.3};
double sum = 0; 
for (double num : doubleArray) {
    sum += num; 
}
System.out.println(sum);
3.5

boolean

boolean rainy = false;
boolean sunny = false;

if (!!!(rainy || sunny) && ((!rainy && !sunny) || !(!(rainy || sunny))) ) {
    System.out.println("It's a cloudy day, not too hot, not too cold");
}
It's a cloudy day, not too hot, not too cold

char

char[] word = {'h', 'e', 'l', 'l', 'o', '!'};

for (int i = 0; i < word.length; i++) {
    System.out.print(word[i]);
}
hello!

Wrapper classes

Wrapper classes provide a class for each primitive type in Java. For example, the wrapper class for int is Integer, char is Character, and so on. These wrapper classes extend the object class.

The syntax for a wrapper object is: wrapper class name = value;

Beware, though. Not all data types that start with a capital letter is a wrapper class. For instance, I originally thought that String was a wrapper class. After googling "is String a wrapper class?", the results told me that String is not a wrapper class because it does not have a primitive class that it wraps.

Therefore, wrapper classes only wrap the eight primitive types, which means there are only eight wrapper classes.

The following cells consist of the previous primitive type examples converted into wrapper classes.

Integer[] nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

// regular
for(int i = 0; i < nums.length; i+=2)
{
    System.out.printf("%d\n", nums[i]);
}
0
2
4
6
8
int x;
int y;
Double casting1;
Integer casting2;

x = 10;
y = 15;

casting1 = (double) y / x;
casting2 = y / x;

System.out.println("With casting: " + casting1); 
System.out.println("With casting: " + casting2);
With casting: 1.5
With casting: 1
Boolean a = true;
Boolean b = false;
Boolean c = a && b;
Boolean d = !(a || b);

System.out.printf("%b\n", c);
System.out.printf("%b", d);
false
false
java.io.PrintStream@7efb4dbf
Character[] letters = {'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'};

// regular
for(int i = 0; i < letters.length; i++)
{
    System.out.printf("%c", letters[i]);
}
HELLO WORLD

Where are wrapper classes used?

One example of where wrapper classes are used is in Collection objects, such as ArrayList. ArrayList can not store primitive data types, and as such, need to be configured with wrapper classes.

ArrayList<Integer> nums = new ArrayList<Integer>(); 

nums.add(1); 
nums.add(2); 

for (int i = 0; i < nums.size(); i++) {
    System.out.println(nums.get(i)); 
}
1
2

Math.random()

Math.random() returns a random number between 0 and 1, but not including 1. The data type that Math.random() returns is a double.


DoNothingByValue

Key knowledge

  • The value of arguments are passed into a method's parameters

    • Java is pass-by-value, not pass-by-reference for methods

      • If you go down the StackOverflow rabbit hole, users claim that everything in java is pass-by-value. Honestly I'm not too sure how this works, but people on StackOverflow really pay close attention to detail so...

      Unfortunately I don't have enough time right now to look into it closely. I might in the future though 😃

      But basically since everything is pass-by-value, arrays are also passed-by-value. However, the value of an array is its reference. Therefore, when an array is passed into a method, the elements of the array can be changed directly.

  • Pass-by-value: The value of a variable is passed
  • Pass-by-reference: The variable's reference is passed. This allows the contents of the variable to be changed.
  • There can only be 1 return value per method in Java
  • Classes and generics allow "pass-by-reference"

Overall explanation of code

This is my thought process as I went through the code. It's easier for me to figure out key knowledge by going through the entire code and documenting what it does.

Anything that I highlight below are included in the key knowledge

Do Nothings

main()

An array called nums is created and set to {1, 3, 4, 5, 5}. The data type is the primitive data type int. An int of name value is also created and set to 6. A data type (but not wrapper class) of String with name name is created and set to blackboard. The code outputs Do Nothings and executes the changeIt method, passing in nums, value, and name.

changeIt()

Within the changeIt method, the argument nums relates to the parameter arr, value to val, and name to word. arr is created to have 5 elements, val is set to 0, and word is set to a substring of the word argumenent of blackboard. As such, word is set to black.

However, before the variables are reassigned to new values, the arguments were passed into the method's parameters. For instance, if I use a for loop to output the values of arr, I get 1, 2, 3, 4, 5, not 0, 0, 0, 0, 0.

The for loop (the actual one in the file) then assigns each element in arr to 0, and outputs it to the terminal. Lastly, the value of word (black) is outputted.


Next thing in main() is changeIt2()

changeIt2()

In changeIt2(), the arguments are passed in as parameters of nums, value, and name. Notice how these parameters have the same name as the arguments. Based on the comment in the file, we can see that the name of the parameter can be the same as the arguments' names.

changeIt2() essentially does the same thing as changeIt()

main()

What's important is that when you go back to main(), the for loop after changeIt2() outputs all of the original values of nums, value, and name. This is because Java is pass-by-value for methods, which means that the variable's values are passed into the method, NOT the reference. On the other hand, pass-by-reference allows you to edit the contents of a variable because the **reference** to the variable is passed.

Limited return

main()

An array named nums2 is assigned to 1, 2, 3, 4, 5. value is assigned to 6, and name is assigned to limited. name is then assigned to the return of changeIt3() and masses in nums2 and name.

changeIt3()

Within the changeIt3() method, you can see that the parameter word (passed in as argument name) is assigned to a new String. A simplified way of writing this would be word = word.substring(0, 5). Both ways work because the shorthand for creating wrapper classes is directly assigning the class to a value; however, behind the scenes, an object is still created.

Within this method, each element of the array is modified to 0, and the word is modified to limit.

main()

Back to main() again, the elements of nums2 are printed out. The edited elements from `changeIt3()` are present because arrays are objects, and the value of an object is its reference.

Do Something with Class

At this point, I got a general understanding of what the code does, so I did not need to write out what each line does.

This section creates a DoNothingByValue object with an object name of doSomething. The constructor runs. Since the object's value is its reference, the constructor can directly edit the variables. As a result, the output in main() shows the changes of the constructor.

Do Something with Generics

This is basically the same thing as class since generics are like objects but are more general, so you can specify different data types.

IntByReference

Key Knowledge

  • This switches two integer numbers to lowest to highest by using a tmp variable
  • These variables are passed by reference because they are part of an object. Therefore, the before and after are changed following the call to swapToLowHighOrder() method.

FRQ 2018

public double runSimulations(int num) {
    int count = 0;
    for (int i = 0; i < num; i++) {
        if (simulate()) {
            count++;
        }
    }
    return (double)count/num; 
}


Notes:

  • Answer what are Methods and Control Structures
  • Explore AP FRQ that teaches us about Methods and Control Structures FRQ
  • Look at Diverse Arrays, Matrix in Teacher code and see if you think this is Methods and Control structures.
  • Look at Diverse Arrays,Matrix in Teacher code an see if you thing this fits Data Types.