Glossary
2D Array
An array of arrays, used to represent tabular data or grids, accessed using two indices (row and column).
Example:
A chessboard can be represented as a 2D array like String[][] board = new String[8][8];.
Arithmetic Operators
Symbols used to perform mathematical calculations such as addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), and modulo (`%`).
Example:
To find the total cost, you'd use total = price * quantity; where * is an arithmetic operator.
Array
An ordered collection of elements of the same data type, stored in contiguous memory locations and having a fixed size once created.
Example:
To store a list of student scores, you might use an array: int[] scores = new int[5];.
Array Length (.length)
A property of an array that indicates the number of elements it can hold.
Example:
To iterate through all elements of myArray, you'd use myArray.length in your loop condition to get the array's length.
ArrayIndexOutOfBoundsException
A runtime error that occurs when a program tries to access an array element using an index that is outside the valid range (0 to length-1).
Example:
Trying to access myArray[10] when myArray only has 5 elements will cause an ArrayIndexOutOfBoundsException.
ArrayList
A dynamic, resizable array implementation in Java that is part of the `java.util` package, allowing elements to be added or removed after creation.
Example:
To store a list of names that can grow or shrink, you'd use an ArrayList: ArrayList<String> names = new ArrayList<>();.
Base Case
The condition within a recursive method that stops the recursion, providing a direct solution without further recursive calls.
Example:
In a factorial method, if (n == 0) return 1; is the base case that prevents infinite recursion.
Boolean Expressions
Expressions that evaluate to either `true` or `false`, often combining conditions using logical operators like AND (`&&`), OR (`||`), and NOT (`!`).
Example:
The condition (age > 18 && hasLicense) is a boolean expression that is true only if both parts are true.
Class
A blueprint or template for creating objects, defining their common properties (instance variables) and behaviors (methods).
Example:
The Car class defines what all cars have (like color, speed) and what they can do (like accelerate, brake).
Code Tracing
The process of manually following the execution of a program line by line, tracking the values of variables to predict the output or state.
Example:
When debugging, you might perform code tracing by hand, writing down variable values after each line to understand program flow.
Compound Assignment Operators
Operators that combine an arithmetic operation with an assignment, providing a shorthand for updating a variable's value.
Example:
Instead of x = x + 5;, you can write x += 5; which is a compound assignment operator.
Constructor
A special method within a class used to initialize new objects of that class, having the same name as the class and no return type.
Example:
The public Dog(String name, int age) is a constructor that sets up a new Dog object with a given name and age.
Decrement Operator
An operator (`--`) that decreases the value of a numeric variable by 1.
Example:
To count down from a starting value, you might use count--; which applies the decrement operator.
Encapsulation
An object-oriented principle of bundling data (instance variables) and the methods that operate on the data within a single unit (class), and restricting direct access to some of the object's components.
Example:
Using private for instance variables and providing public getter/setter methods is a common way to achieve encapsulation.
Enhanced for Loop
A simplified `for` loop syntax in Java used for iterating over elements in arrays or collections without needing to manage indices.
Example:
To print each number in int[] nums, you can use an enhanced for loop: for (int num : nums) { System.out.println(num); }.
IS-A relationship
A relationship between classes established through inheritance, where a subclass 'is a' type of its superclass (e.g., a Dog IS-A Animal).
Example:
Because Dog extends Animal, we say a Dog has an IS-A relationship with Animal.
Immutable (Strings)
A characteristic of String objects in Java meaning that once a String object is created, its content cannot be changed.
Example:
When you modify a string like str = str + " World";, you're not changing the original str but creating a new immutable String object.
Increment Operator
An operator (`++`) that increases the value of a numeric variable by 1.
Example:
In a loop, i++; uses the increment operator to advance the counter i by one in each iteration.
Inheritance
An object-oriented programming principle where a new class (subclass) acquires the properties and behaviors (fields and methods) of an existing class (superclass).
Example:
A Car class might use inheritance from a Vehicle class, gaining its speed property and move() method.
Instance Variables
Variables declared within a class but outside any method, representing the data or attributes specific to each object (instance) of that class.
Example:
In a Student class, name and grade would be instance variables, unique to each student object.
Integer Division
Division performed on two integers where any fractional part of the result is truncated (discarded), resulting in an integer.
Example:
If you calculate int result = 7 / 3;, the integer division will yield 2, not 2.33.
Method
A block of code within a class that defines a specific behavior or action that objects of that class can perform.
Example:
The bark() method in a Dog class defines the action a dog object can take.
Method Overriding
The ability of a subclass to provide its own specific implementation for a method that is already defined in its superclass.
Example:
If an Animal has a makeSound() method, a Dog subclass might use method overriding to implement its own makeSound() that prints "Woof!".
Modulo Operator
An arithmetic operator (`%`) that returns the remainder of a division operation.
Example:
To check if a number is even, you can use if (num % 2 == 0); because the modulo operator will return 0 for even numbers.
Object
An instance of a class, representing a concrete entity with its own unique set of data (instance variable values) and the behaviors defined by its class.
Example:
If Car is a class, then myCar = new Car("Red"); creates a specific red object of the Car class.
Polymorphism
An object-oriented principle meaning "many forms," allowing objects of different classes to be treated as objects of a common superclass, enabling flexible and extensible code.
Example:
If Dog and Cat both extend Animal, then Animal myPet = new Dog(); demonstrates polymorphism, as myPet can take the form of a Dog.
Post-increment
An increment operation (`x++`) where the original value of the variable is used in the expression first, and then the variable is incremented.
Example:
If int x = 5; int y = x++;, y will be 5 because post-increment uses x's value before incrementing it to 6.
Pre-increment
An increment operation (`++x`) where the variable is incremented first, and then its new value is used in the expression.
Example:
If int x = 5; int y = ++x;, y will be 6 because pre-increment updates x to 6 before assigning it to y.
Recursive Methods
Methods that solve a problem by calling themselves with smaller inputs until a base case is reached, then combining the results.
Example:
A method to calculate factorial, factorial(n), that calls factorial(n-1) is an example of a recursive method.
Recursive Step
The part of a recursive method that makes one or more recursive calls to solve a smaller version of the same problem.
Example:
In return n * factorial(n - 1);, the factorial(n - 1) call is the recursive step.
Short-Circuit Evaluation
A feature of logical operators (`&&` and `||`) where the second operand is only evaluated if the first operand is not sufficient to determine the result of the expression.
Example:
In if (condition1 && condition2), if condition1 is false, condition2 is never checked due to short-circuit evaluation.
StackOverflowError
A runtime error that occurs when a recursive method calls itself too many times without reaching a base case, exhausting the memory allocated for the call stack.
Example:
Forgetting the base case in a recursive function will inevitably lead to a StackOverflowError.
String
A sequence of characters in Java, treated as an object rather than a primitive type.
Example:
You can store a user's name as a String: String userName = "Alice";.
String Concatenation
The process of joining two or more strings together to form a single, longer string, typically using the `+` operator.
Example:
You can combine a greeting and a name using string concatenation: String message = "Hello, " + userName + "!";.
Traversing 2D Arrays
The process of iterating through all elements of a 2D array, typically using nested `for` loops (one for rows, one for columns).
Example:
To print every cell in a game board, you'd use nested loops for traversing 2D arrays.
Traversing Arrays
The process of iterating through each element of an array, typically using a `for` loop, to access or process its contents.
Example:
A for loop from i = 0 to array.length - 1 is used for traversing arrays to print each element.
add() (ArrayList method)
A method of the ArrayList class used to append an element to the end of the list or insert an element at a specified index.
Example:
To add a new score, you'd use scores.add(95); which is the add() method for an ArrayList.
boolean
A primitive data type in Java that can only hold one of two logical values: `true` or `false`.
Example:
A game might use boolean gameOver = false; to track if the game is over or still running.
compareTo() (String method)
A method of the String class that compares two strings lexicographically (based on dictionary order) and returns an integer indicating their relative order.
Example:
If str1.compareTo(str2) returns a negative number, str1 comes before str2 alphabetically, as determined by compareTo().
double
A primitive data type in Java used to store floating-point numbers, which are numbers that can have decimal points.
Example:
To calculate a precise average, you might use double average = total / count; ensuring the average includes decimal values.
equals() (String method)
A method of the String class used to compare the content of two strings for equality, returning `true` if they are identical and `false` otherwise.
Example:
To correctly compare two names, you must use name1.equals(name2) instead of ==, as equals() compares content.
extends keyword
A keyword in Java used in a class declaration to indicate that the class is a subclass of another class, thereby inheriting from it.
Example:
The declaration public class Dog extends Animal uses the extends keyword to show that Dog inherits from Animal.
for Loops
Control flow constructs used to repeat a block of code a specific, predetermined number of times.
Example:
To print numbers from 1 to 10, you'd use a for loop: for (int i = 1; i <= 10; i++) { System.out.println(i); }.
get() (ArrayList method)
A method of the ArrayList class used to retrieve the element at a specified index.
Example:
To access the first item in a list, you'd use list.get(0); which is the get() method.
if Statements
Control flow constructs that allow a program to execute different blocks of code based on whether a specified condition is true or false.
Example:
An if statement like if (score > 100) { System.out.println("New High Score!"); } checks a condition before printing.
indexOf() (String method)
A method of the String class that returns the index within the string of the first occurrence of the specified substring or character.
Example:
For String sentence = "Hello World";, sentence.indexOf("World") would return 6, indicating the starting index of "World".
int
A primitive data type in Java used to store whole numbers (integers) without any decimal points.
Example:
You can declare an integer variable like int score = 100; to keep track of a player's score in a game.
length() (String method)
A method of the String class that returns the number of characters in the string.
Example:
If String word = "Hello";, word.length() would return 5, indicating the length of the string.
remove() (ArrayList method)
A method of the ArrayList class used to delete the element at a specified index, shifting subsequent elements to the left.
Example:
To take out an item from a shopping list at index 1, you'd use shoppingList.remove(1); which is the remove() method.
set() (ArrayList method)
A method of the ArrayList class used to replace the element at a specified index with a new element.
Example:
If a player's score at index 2 needs updating, you'd use scores.set(2, newScore); which is the set() method.
size() (ArrayList method)
A method of the ArrayList class that returns the number of elements currently in the list.
Example:
To know how many items are in your inventory, you'd call inventory.size(); which is the size() method.
substring() (String method)
A method of the String class that returns a new string that is a portion of the original string, specified by start and end indices.
Example:
From String text = "Computer";, text.substring(2, 5) would extract "mpu" using the substring() method.
super keyword
A keyword in Java used within a subclass to refer to the superclass's constructor or methods.
Example:
In a Dog constructor, super(name); uses the super keyword to call the Animal class's constructor.
this Keyword
A keyword in Java that refers to the current object, often used to distinguish between instance variables and parameters with the same name.
Example:
In a constructor, this.name = name; uses the this keyword to assign the parameter name to the object's instance variable name.
while Loops
Control flow constructs that repeatedly execute a block of code as long as a specified boolean condition remains true.
Example:
A game might use a while loop like while (playerAlive) { /* game logic */ } to continue until the player's health runs out.