Key Terminology
Class
Class
A blueprint for creating objects, defining their data (attributes) and actions (methods).
In class:
- Data: Represented by instance variables (e.g., name, height).
- Actions: Represented by methods (e.g., sleep(), walk()).
In the Person class name, height, and weight are instance variables, while sleep(int hours) and walk(double distance) are methods.
class Person{
String name;
double height;
double weight;
void sleep(int hours){
...
}
void walk(double distance){
...
}
}Instance Variable
Instance variable
A variable defined in a class for which each instantiated object of the class has its own copy.
If you create two Person objects Mike and Sara, each will have its own name, height, and weight.
Exampleclass Vehicle { // define a class
// here are the attributes
String brand;
int wheels;
public static void main(String[] args) {
Vehicle car = new Vehicle(); // create first object
car.brand = "Toyota"; // edit instance variables
car.wheels = 4;
Vehicle truck = new Vehicle(); // create second object
truck.brand = "Ford"; // edit instance variables
truck.wheels = 8;
System.out.println(car.brand + " " + car.wheels); // output instance variables
System.out.println(truck.brand + " " + truck.wheels);
}
}
- Instance variables are often confused with static variables.
- Remember, instance variables are unique to each object, while static variables are shared across all instances.
Identifier
Identifier
A name used to identify a programming entity, such as a class, variable, or method.
Rules for identifiers:
- Must start with a letter or underscore, not a digit.
- Can include letters (uppercase or lowercase), digits, and underscores.
Avoid using reserved keywords (e.g. class, int) as identifiers.
Exampleclass Car{
...
}Primitive
Primitive
A basic data type provided by a programming language.
These are not objects and are stored directly in memory.
Common primitive types (and Java examples):
- Integer:
- int
- Size: 32 bits
- Range: -2,147,483,648 to 2,147,483,647
- Use: Most common for integer calculations
- long
- Size: 64 bits
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Use: When int is insufficient, such as for large numbers
- int
- Floating-point:
- double
- Size: 64 bits
- Range: Approximately 4.9e-324 to 1.7e+308
- double