Unit 1 and Unit 2 Notes
Notes on CB Unit 1 and 2.
Unit 1 Primitive Types
- String literal - an exact sequence of characters enclosed between two quotation marks
- Different Errors
- Syntax/compiler error: program won't compile due to typos
- Exception
- Logic error: actual output differs from anticipated output ### Data Types
- Determine what type of value is stored ### Variables
- variable: name for memory location holding a certain type of value
- no spaces
- can't start with #
- no special characters (anything besides letters/digits)
- no Java reserved keywords
- use camelCase conventions #### Declaring
- assignment operator (=) is used to initialize variable or change its associated value ### Primitive
- Examples:
- boolean (1 bit)
- int (32 bits)
- if the integer result of an expression is beyond the range there will be an overflow error
- double (64 bits) ### Reference ### Arthmetic Operators
- INCLUDES: +, -, *, /, %
- operations with only int ==> output int
- operations with only double ==> output double
- operations with both int and double ==> * output double
- division by zero ==> ArithmeticException ### Compound Assignment Operators and Incrememt/Decrement Operators
- +=, -=, *=, /=, %=
- Using these will change the value of the variable to the result of the operation
- Increment/Decrement:
- ++: adds 1
- --: subtracts 1
// Syntax example
public Turtle{
public Turtle(String nm, int ag, boolean it){ // signature with formal parameters
this.name = nm;
this.age = ag;
this.isTortoise = it;
}
public String name;
public int age;
public boolean isTortoise;
}
// call by value: pass actual parameters to constructor
Turtle Shellbert = new Turtle("Shellbert", 4, true); // creating an object with "new" keyword calls constructor
Vocab
- signature: constructor name and parameter list
- parameter list: lists type of values passed and their variable names (formal parameters)
- parameter: value passed into a constructor (actual parameters), must match types specified in parameter list
- call by value: initializes formal parameters with copies of the actual parameters
- overloaded constructors: there are multiple constructors with same name, differing signatures
- no argument constructor: constructor with no parameters, sets attributes (instance variables) of object to default values
- String: null
- boolean: false
- null: variable reference doesn't actually contain an object