0
0
Kotlinprogramming~5 mins

Calling Kotlin from Java - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Calling Kotlin from Java
O(n)
Understanding Time Complexity

When Java code calls Kotlin functions, it runs those Kotlin parts too.

We want to see how the time cost grows when Java calls Kotlin code.

Scenario Under Consideration

Analyze the time complexity of the following Kotlin function called from Java.


fun sumList(numbers: List): Int {
    var total = 0
    for (num in numbers) {
        total += num
    }
    return total
}
    

This Kotlin function adds up all numbers in a list. Java code calls this function.

Identify Repeating Operations

Look for loops or repeated steps inside the Kotlin function.

  • Primary operation: Looping through each number in the list.
  • How many times: Once for every item in the list.
How Execution Grows With Input

As the list gets bigger, the function does more additions.

Input Size (n)Approx. Operations
1010 additions
100100 additions
10001000 additions

Pattern observation: The work grows directly with the list size.

Final Time Complexity

Time Complexity: O(n)

This means the time to run grows in a straight line with the number of items.

Common Mistake

[X] Wrong: "Calling Kotlin from Java adds extra loops or slows down the function significantly."

[OK] Correct: The Kotlin function runs just like normal code; calling it from Java does not add hidden loops or repeated work.

Interview Connect

Understanding how Kotlin and Java work together helps you explain performance clearly and shows you know how code runs across languages.

Self-Check

"What if the Kotlin function called another function inside the loop? How would that affect the time complexity?"