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.
class 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.
class 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
- Use: For decimal values, such as scientific calculations
- double
- Boolean:
- boolean
- Values: true or false
- Use: For conditional logic and control flow
- Character: char
int age;Primitives are often more efficient than objects because they do not require additional memory for object overhead.
- String is not a primitive type; it is a class.
- Moreover, they are immutable.
- An immutable object is an object whose state cannot be changed after it is created.
- Any “modification” creates a new object instead of altering the original one.
Parameter Variable
Parameter variable
A variable used to pass data into a method.
Parameter variables are only accessible within the method that declares them.
void drive(String direction){
...
}Here, the parameter variable is direction (of type String).
Parameter variables are temporary and exist only during the execution of the method.
Local Variable
Local variable
A variable declared within a method or block of code.
Local variables are only accessible within the block where it is declared and are destroyed once the block of code is exited.
void sayHello(String name){
String res = "Hello, " + name + "!"; // local variable
System.out.println(res);
}- Do not confuse local variables with instance variables.
- Local variables are temporary and exist only within a specific block of code.
Return Value
Return value
The data that a method sends back to the code that called it.
If a method does not return a value, its return type is void.
Here, the wakeup() method returns a boolean value indicating whether the person woke up.
boolean wakeup(){
// some code
return false; // example of return value
}In Java, when defining a method, the first thing you need to specify is the return data type.
- Don't confuse the return type with the return value.
- The return type is part of the method's declaration, while the return value is the actual data returned during execution.
Method Signature
- A method signature includes the method's name and its parameters (number and types).
- The return type is not part of the signature.
Signatures are used to differentiate methods, especially in cases of method overloading.
- Why are primitives more efficient than objects?
- What is the difference between an instance variable and a local variable?
- How do parameter variables differ from instance variables?