Unit 1 Primitives

Note: Credit to Period 1's Primitives presentation for the information below

What are primitives?

Primitives include certain data types, such as integer, doubles, booleans, char, float, and long. Primitive names typically start with a lowercase letter, and if needed, subsequent words start with an uppercase letter.

To switch between primitive data types, casting can be used.

There are 5 types of operators: +=, -=, *=, /=, and %= and an increment and decrement operator: ++ and --.

Scanners

To take in input in java, the Scanner class needs to be first imported with import java.util.Scanner;. Next, a scanner object can be created with Scanner object name = new Scanner(System.in). To take in input, specify the data type that the scanner takes in with scanner.nextdata type

Unit 2 Objects

What are objects?

Objects are instances of a class. There are three steps to create an object: declaration, instantiation, and initialization. Declaration is specified through a type followed by the variable name. Instantiation instantiates the object with the new keyword. Initialization occurs the name of the constructor method is specified after the new keyword.

Creating objects

The syntax to create objects is: Class-name object-name = new constructor-name

Methods

Methods are defined with a return type (int, bool, etc. or void (no return type)) and the method name.

Static vs non static

A static method can only access other static methods, while a nonstatic method can access both static and nonstatic methods.

Math class

Important methods: Math.abs, Math.random()

Unit 4 Iteration

while loops

The syntax of a while loop looks like this:

while (condition) {

}

Everytime the while loop executes, it first checks the condition. If the condition is true, the code inside the loop is ran. After the code finishes, the while loop checks again with the condition.

for loops

The syntax of a for loop looks like this:

for (initialize variable; condition; change variable) {

}

Every time the for loop executes, it checks the condition. If the condition is true, the code inside the for loop is ran. After the code finishes running, the variable is incremented by the amount specified.


There can also be nested iteration (ex: for loop within for loop)

for each loop

The syntax for a for-each loop looks like this:

for (dataType item : array){

}

This is especially useful for iterating through arrays.

Unit 5 Writing Classes

What are classes?

Classes are the blueprints for objects. They contain instance variables, constructors, and methods. Objects are instances of classes.

Access modifiers

public: Can be accessed in classes outside of the declaring class

private: Can only be accessed in the declaring class

Getters and setters

Getters help make variables read only. Setters allow write only permissions.

The syntax for getters is:

public data-type getName

Unit 6 Arrays

What are arrays?

Arrays can only be used to store one data type. They are different from ArrayLists in that they are not as flexible; the size of an array is fixed.

To create an array, you can either use constructors (dataType[] arrayName = new dataType[numerOfItems]), or you can use pre-initiliazed arrays (dataType[] arrayName = {elements})

Traversing arrays

Arrays can be traversed using a for or while loop.

ArrayIndexOutOfBoundsException: This occurs when the loop tries to access a nonexistant index in the array.

for each loop

The syntax is:

for (dataType i : arrayName) {
    // code
}

This is usually used to iterate through each item in the for each loop.