- type
category of data
included int, long, float, double, char, String, boolean
every variable must be declared to be some type
- class
is a pattern or template
exists at compile time
java source files
analogy: recipe for a chocolate cake
- object
a wrapper around data and functions that operate on that data
exists at run time
created by the java machine when you run a program
made as instances of a class definition
analogy: the chocolate cake itself...
many cakes can come from one recipe
- methods
the OO name for the functions defined in a class definition
these functions will be callable in objects made from that class definition
let's say a class called "Employee" has a method called "reportHours()"
let's say we have this in the Java program:
Employee emp1;
emp1 = new Employee();
double hours;
this means we have made an object "emp1" that is of type/class "Employee"
then we can run the method "reportHours" with this syntax:
hours = emp1.reportHours();
- fields
the OO name for data (variables) in a class definition
a field is a variable that is declared in a class, but not in a method
- constructor
special method(s) in a class that is used to create a new object and
initialize the fields of the object
- data hiding
the software engineering principle that says information should not
be visible outside an object unless it is necessary.
- public interface
the collection of methods in an object that are visible outside the
object, and are therefore callable from other objects.
- inheritance
the concept of creating a new class by first including all the
fields and methods of an existing class; you may add new fields
and classes, and you may "override" a method by writing a new
version of the inherited one.
- executable java program
one or more class definitions
at least one class definition has a method with the signature
"public static void main (String[] args) "
- all variables must be declared (given a type) before they are used
variables declared in a class but not in a methods are "fields"
also called "instance variables"
each object created from the class has separate instance variables
a field in one object is different storage from that field name
in another object of the same class
variables can be declared in a methods... "local variable"
local variable comes into existence when the method is called,
goes away when the method execution ends.
variable can be declared as a parameter to a method.
these variables get initial values when the method is called
a variable declared as a field, but with the keyword "static"
this is a "class variable"... it is one storage location,
shared by all objects of that class.
example: static int _pid;
a variable declared with the keyword "final" is a constant.
example: final int PI = 3.1415927 ;
- built-in types:
int integer, 16 bits
long integer, 32 bits
float real number, 32 bits
double real number, 64 bits
boolean logical value, 1 bit
char single character, 16 bits (a character)
String quoted sequence of characters
-
classes are types too
declare a variable to have some class type, it will contain an object
- Inside a method (function), you write code in Java pretty much as you
did for JavaScript. Java has the same 6 main concepts as JavaScript,
with similar syntax:
assignment speed = 45.71;
expressions System.out.println("Results are: " + (speed*5.6/factor) );
loops int i; // loop index
for (i=0; i < maxVal; i++) {
x = x * i;
}
int k=0;
while (k < 10) {
speed = speed + k*factor;
k = k+1;
}
conditional if (speed > threshold) {
adjustment = speed * 0.10;
speed = speed - adjustment;
} else {
speed = speed * 1.05;
}
functions double increaseSpeed (double speed, int factor) {
return speed*factor;
}
data structure
(array) double[] grades;
grades = new double[maxGrades];