Object creation in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we create objects in Java, it takes some time for the computer to set up each new object.
We want to understand how the time needed grows as we create more objects.
Analyze the time complexity of the following code snippet.
public class Example {
public static void createObjects(int n) {
for (int i = 0; i < n; i++) {
Object obj = new Object();
}
}
}
This code creates n new objects one after another in a loop.
- Primary operation: Creating a new object inside the loop.
- How many times: The loop runs
ntimes, sonobjects are created.
Each time we increase n, we create more objects, so the time grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 object creations |
| 100 | 100 object creations |
| 1000 | 1000 object creations |
Pattern observation: If you double the number of objects to create, the time roughly doubles too.
Time Complexity: O(n)
This means the time to create objects grows in a straight line with the number of objects you want to make.
[X] Wrong: "Creating objects inside a loop is instant and does not affect performance."
[OK] Correct: Each object creation takes time, so doing it many times adds up and affects how long the program runs.
Understanding how object creation time grows helps you write code that runs smoothly and shows you can think about efficiency clearly.
"What if we created objects only when a condition inside the loop is true? How would the time complexity change?"
Practice
Car in Java?Solution
Step 1: Understand object creation syntax
In Java, objects are created using thenewkeyword followed by the class name and parentheses.Step 2: Check each option
Car myCar = new Car(); usesnew Car()correctly with proper capitalization and assignment. Others have syntax errors or wrong capitalization.Final Answer:
Car myCar = new Car(); -> Option AQuick Check:
Usenew ClassName()to create objects [OK]
- Omitting the new keyword
- Using wrong capitalization for class name
- Missing parentheses after class name
Book?Solution
Step 1: Recall syntax for object creation
In Java, when creating an object, parentheses must follow the class name even if the constructor has no parameters.Step 2: Identify the incorrect option
Book b = new Book; misses the parentheses afternew Book, causing a syntax error. Others are correct.Final Answer:
Book b = new Book; -> Option CQuick Check:
Always use parentheses after class name in new [OK]
- Forgetting parentheses after class name
- Confusing object creation with method calls
- Using semicolon inside parentheses
class Dog {
String name;
Dog(String n) {
name = n;
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog("Buddy");
System.out.println(d.name);
}
}Solution
Step 1: Understand constructor assignment
The constructor sets thenamefield to the string passed, which is "Buddy".Step 2: Check output of print statement
Printingd.nameoutputs the string "Buddy" stored in the object.Final Answer:
Buddy -> Option AQuick Check:
Constructor sets field, printing field shows assigned value [OK]
- Expecting default null instead of assigned value
- Confusing object reference print with field print
- Missing constructor parameters
class Person {
String name;
Person(String n) {
name = n;
}
}
public class Test {
public static void main(String[] args) {
Person p = Person("Alice");
System.out.println(p.name);
}
}Solution
Step 1: Check object creation syntax
The code tries to create an object withPerson("Alice")but misses thenewkeyword.Step 2: Confirm other parts are correct
Constructor name matches class name, variable is declared, and semicolon is present.Final Answer:
Missing new keyword when creating object -> Option DQuick Check:
Use new keyword to create objects [OK]
- Omitting new keyword
- Confusing method call with object creation
- Incorrect constructor naming
Student objects with names "John" and "Jane" and prints their names?class Student {
String name;
Student(String n) {
name = n;
}
}Solution
Step 1: Check constructor usage
The constructor requires a String parameter. Student s1 = new Student("John"); Student s2 = new Student("Jane"); System.out.println(s1.name + ", " + s2.name); correctly passes names "John" and "Jane".Step 2: Verify object creation and printing
Student s1 = new Student("John"); Student s2 = new Student("Jane"); System.out.println(s1.name + ", " + s2.name); creates both objects properly and prints their names separated by a comma.Final Answer:
Student s1 = new Student("John"); Student s2 = new Student("Jane"); System.out.println(s1.name + ", " + s2.name); -> Option BQuick Check:
Use new with constructor parameters to create objects [OK]
- Calling constructor without new keyword
- Using default constructor when none exists
- Not passing required parameters
