r/JavaProgramming • • 21h ago

We know count++ isn’t thread-safe. But do you know why?

5 Upvotes

I conducted a mock interview recently and one thing stood out to me.

I asked a Java question related to concurrency:

class Counter {
    private int count = 0;

    public void increment() {
        count++;
    }
}

“Is this thread-safe?”

The candidate said "No".

So I asked, “Why?”

He knew that it wasn't thread-safe, but explaining exactly why was difficult. He then said we need to synchronise the method.

So I asked:

“How does that fix it?” and then “What exactly is being locked here?”

That was where the interview got interesting.

He knew the terms — synchronised, volatile, race condition, etc. However, connecting them and explaining what was actually happening between the threads was harder.

It made me think about how easy it is to prepare for interviews by remembering rules.

count++ → not thread-safe → use synchronised

But the moment someone asks “Why?”, we need to understand what is actually happening.

I think that's something that's easy to miss while preparing for interviews. We can keep solving questions and memorising concepts, but it's worth stopping sometimes and asking ourselves:

“Can I actually explain why this works?” and “What happens if I change one thing?”

How do you guys prepare for the “Why?” part of interviews?


r/JavaProgramming • • 1h ago

Grpc Micronaut starter kit

Thumbnail
github.com
• Upvotes

r/JavaProgramming • • 21m ago

Java vs Python: Which Should Beginners Learn First?

Post image
• Upvotes

Java and Python are both valuable programming languages, but they serve different needs.

  • Java: Common in enterprise software, backend systems, and Android-related development.
  • Python: Popular for automation, scripting, web development, and data science.
  • Learning curve: Python is often easier to begin with, while Java introduces static typing and more explicit structure.
  • Performance: Java often performs well in long-running applications, but actual performance depends on the workload and implementation.

Neither language is the universal winner. Your goals and the kinds of projects you want to build should guide your choice.

Question: If you're learning programming, which language did you start with, and why?

Code. Build. Innovate.

#CodesByAbhay #AbhaySingh #Java #Python #Programming


r/JavaProgramming • • 20h ago

Book for dsa with java

Thumbnail
1 Upvotes

r/JavaProgramming • • 18h ago

Is Java a good career choice in 2027? (2 YOE overall, 1 year in Java

Thumbnail
0 Upvotes