Either explicit or implicit type //assigning type can be considered best practice. It provides better errors and simplifies var x = 5 //implicit INT String coolWord = "antelope" //explicit type
VAR coolWord OBJECT String VALUE "antelope"
Six specific types: Integers, Strings, floating-point numbers, booleans, lists, maps
Operators are used to do things '=' is the assignment operator Basic arithmetic. ++ increments, -- decrements
Operator Description if Example == Equal To if (x == 10) {...} != Not Equal To if (x != 10) {...}
Greater Than if (x > 10) {...}
< Less Than if (x < 10) {...}
= Greater Than or Equal To if (x >= 10) {...} <= Less Than or Equal To if (x <= 10) {...} ! Not (Negates a value) !false == true
String Interpolation: inserting the value of our variables into our strings
int temp = 75;
String weatherReport = "It is rainy and
Used so the machine can make a decision if (temperature > 75) { print("It is hot today."); }
if (temperature > 75) { print("It is hot today."); } else { print("It is not that hot today."); }
if (temperature > 75) { print("It is hot today."); } else if (temperature > 50) { print("It is mild today."); } else { print("It is cold today."); }
Switch Statements A switch-statement makes it convenient to decide between many different possibilities.
switch (favoriteAnimal) { case "dog": // if favoriteAnimal is equal to "dog" do the following print("Bark!"); break; //we need one of these at the end of every case but default case "cow": print("Moo!"); break; case "cat": print("Meow!"); break; default: print("Your animal is a new species to me!"); }
int beersOnTheWall = 99;
while (beersOnTheWall > 0) {
print("$beersOnTheWall bottles of beers on the wall,
The while-loop will keep executing the lines between the parentheses until beersOnTheWall is no longer greater than 0 In a do-while-loop, the check is at the end of the loop instead of at the beginning. For-loops are compact and extremely useful. Within the parentheses, you have three statements. The first is an opportunity to create a variable and set its value (this variable is only accessible within the for loop, if it is declared here). The second is a comparison that must hold for us to continue the loop. The last is what we want to do at the end of each iteration of the loop.
Functions allow a program to be broken down into callable units. Can be "called" from another, unrelated, part of the code Variables within a function are a type of local variables They can also return information to their caller by way of a return value. Return is what answer is delivered when the function is called somewhere else void specifically states the function does not have a return
=> is used for separating the return type, name, and parameters of a single-line function from its implementation. The evaluated result of the expression on the right of the => is the return value.
Lists - [] - useful for storing information sequentially. That information may be sorted or unsorted. It can be of any type. It can be of any length. Maps - {} - useful for associating pieces of data with identifiers. Uses key-value pairs Sets - () - like a list, but no order and no duplicates.
Every object belongs to a class. The class of an object is its type. Perhaps we want to model a game of checkers using classes. We can have one class of objects represent each of the pieces, named Piece. The instance variables of this Piece class will determine their color, king status, and location on the board. The Piece class may have a method for moving pieces and another for drawing them on the screen. We can have another class that models the Board. The Board has a method for drawing itself and an instance variable, a List, that keeps track of all of the Piece objects. The Board may also have constant instance variables that determine the number of squares and dimensions of the board.
Methods used for getting and setting so-called properties. These properties are really just accessor names
Methods are functions that are attached to an object
Constructors are special methods used for setting up an object when it is first instantiated. A basic constructor is a method with the same name as the class. It can take parameters and is most often used for assigning an object’s instance variables. Dice(int ns, int nd) { sides = ns; numberOfDice = nd; }
And here’s how this new constructor can be called at instantiation time: Dice d = new Dice(6, 4); // d has 6 sides and 4 dies
class Pen extends WritingInstrument { class Pencil extends WritingInstrument {
the class extends (adds onto) the original class
An abstract class is a class that cannot be instantiated. Its purpose in life is to serve as a superclass for other classes. abstract class A { int get myProperty1; int get myProperty2; int myMethod1(); void myMethod2(); }