Translates from source code to bytecode, which runs on the "Java Virtual Machine"
Syntax
The way to write something in a language
Errors
Compile-time errors
Run-time errors
Semantic errors
Compile-time errors
Syntax errors (can't be interpreted as valid code in the language)
Type errors (misuse of language features so that it is not valid code)
Run-time errors
Code compiled, but attempted something illegal/impossible (e.g., dividebyzero, out-of-boundsarrayindex, usingnulllikeanactualobjectvalue)
Semantic errors
The code compiles and runs (doing what it implied), but we have written code that doesn't do quite what we want to occur (such as dividing instead of multiplying, or not-quite-finding the maximum in an array)
Edit-Compile-Run cycle
1. Edit a document
2. Compile it (to translatetoJavabytecode)
3. Run it
Whitespace
Any whitespace allowed between any identifiers, operators, etc. (as long as they can be distinguished, we don't even need whitespace)
Identifiers
Names for things in our code. Consist of letters, numbers, underscores, but don't start with numbers
Keywords
Identifiers that have built-inmeaning in the language, such as: for, while, if, caseswitch, public, private, etc.
Primitive types(8)
boolean
char
byte
short
int
long
float
double
Reference types
Types created through a classdefinition or arraydefinition (whether we write the class or we get it from a library of code, such as java.util or java.lang)
Casting
A conversion from a value of one type to a value of anothertype
Implicit casting conversions
When noinformation is lost, Java will convertbetweentypes for us. E.g., from int to double; from short to int; from anything to String (for printing purposes, usually; uses the toString() method for objects)
Explicit casting conversions
Programmer-specified conversions. Usually required when the conversion loses precision or other information; tells Java that it is 'okay' to perform the lossy action. E.g.: from double to int; from long to short
Declaration
A type and an identifier. Tells Java that there should be a namedstoragelocation that can containonevalueofthegiventype, and will be accessed using the given identifier
Expression
A representation of a calculation that can be evaluated to result in a single value; no indication what to do with the value
Statement
A command/instruction for the computer to perform some action; often, statements contain expressions
Strings
Concatenation: + operator
Implicit conversion of everything to String values when added to a String, or when a String value is needed
Escape sequences: \t, \n, \", \\, etc.
Checking for string equality: can't just use ==. Use the equalsmethod: e.g., s1.equals(s2)
Strings are immutable (can'tchange)
Scanner
Class providing convenient methods to readtextinput from a source (such as keyboard/System.in)
Constant
A variable whose value will neverchange from its initial value. Indicated with the final keyword
Increment/decrement
e.g., x++, ++x, x--, --x. "Syntactic sugar" for an increment on the previous or following line
Control flow
Heavy use of boolean expressions. (expressions that result in a booleanvalue)
if/if-else
No elif: since we use {}'s instead of indentation, we can get the same effect with chained if-else's
switch
Old-fashioned, fast way to branch
Can only switch based on primitive value (or enum, or String as of Java 1.7)
Cases must be values, not expressions
Use break statements to manually leave after each case statement (or don't, with unusual control flow behavior compared to if-elses)
Default case can catch all non-cased values
while loop
Keep executing body as long as guard (boolean expression) is true. Body might execute zero times (if guard is false first time)
do-while loop
Like while loop, but guaranteed to run at least once (guard checked after each loop iteration, not before)
for-each loop
Requires an 'Iterator' (arrays are Iterators). Looks like value-based for loop from Python; avoids index usage but still lets us access/modify each element in the Iterator (array, for us for now)
break, continue
Ways to modify loop/switchcontrolflow
Arrays
Fixed-length sequences of values, all of a single type
Arraydeclaration
Give a type and an identifier; now the type is an array type. ex: int[ ] xs ;
Class
A class is a type. It is like a blueprint for makingobjects. It defines the state that each object will have, and what behaviors (methods) are available for those objects
Object
An object is a value. (A value of a particular class type). It is an instance of its class (one distinct value of that type). It has its own copy of each instancevariable and method
Variable versus Reference versus Object
A variable is a namedcontainer (on the stack). A reference is just a (type,address) pair of an address of an object in memory that also knows what type it points to. Some variables only contain primitive values (no reference), while other variables only contain references (the value is elsewhere). An object is an instance of a class (it's a value), and it resides in the heap memory. The object contains its own copy of each instance variable and method
Aliases
Many references can have the address of the sameobject; they are called aliases. Since there's only one object involved, updates via one reference are visible through the otherreferences
Instance variable
A (non-static) variable declared inside a class, indicating that each object of the class will have its own maintainedcopy of this variable
Constructor
Special method that is used when creating a new object
No returntype listed (returns reference to object of the class's type)
Method name must be the class name exactly
Parameters: entirely at programmer's discretion. (Often one per instancevariable)
Default constructor: If a class definition doesnot explicitly list a constructor definition, then a default implementation is available: no parameters, and all instance variables receive default values: 0, false, and null (for reference types)
Methods
Named block of code that can be called
Defined in a class → thus has access to things defined in the class (no matter what visibility)
Method signature
Modifiers, return type, name, parameter list
Parameters
Formal parameters defined in parameter list (declares them); when a method is called, the actual parameters (arguments) are supplied to instantiate the formal parameters for this particular invocation of the method. Parameters are local variables