Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 3 · LANGUAGES

How to Code in Java Using AI

Use an AI assistant to write a Java method, compile and run it, and prove it with JUnit 5 tests. Works in IntelliJ, VS Code or Eclipse.

In short: say the Java version and build tool, give the method signature and rules, ask for JUnit 5 tests, run them.

Why Java with AI

WHY

Java is used in big systems at banks, telecom and e-commerce companies like Flipkart and Amazon. It needs a lot of boilerplate: classes, getters, tests. AI writes that part fast, so you focus on the logic.

What you need

WHAT
  • JDK 21 (LTS): Eclipse Temurin or another OpenJDK build. Check with java -version.
  • Build tool: Maven or Gradle. They download JUnit for you.
  • IDE + assistant: IntelliJ with GitHub Copilot, or VS Code with Copilot or Claude Code.
  • JUnit 5: the standard Java test library.

How it flows

1 · Describe the method Java 21, signature, rules 2 · AI writes code read every line 3 · Compile + run javac / green arrow 4 · Ask for JUnit tests edge cases incl. null 5 · Run tests mvn test = green

Install and check

HOW
  1. Install JDK 21 (for example Eclipse Temurin 21 from adoptium.net).
  2. Check: java -version and javac -version show 21.
  3. Install Maven, or let IntelliJ create a Maven project for you (New Project → Maven).
  4. Add JUnit 5 to pom.xml (ask the AI: “add JUnit 5 test dependency to this pom.xml”, then check the version on Maven Central).

Step 1 — the prompt

COPY THIS PROMPT

Write a Java 21 class PasswordCheck with public static boolean isStrong(String pw). Strong means at least 8 characters, one uppercase, one lowercase and one digit. Null returns false. No external libraries. Add a main method that prints the result for "Mumbai2026" and "password". Only return the code.

Step 2 — the code (tested here)

PasswordCheck.java
// Check password strength. Built with an AI assistant, then tested.
public class PasswordCheck {
    public static boolean isStrong(String pw) {
        if (pw == null || pw.length() < 8) return false;
        boolean upper = false, lower = false, digit = false;
        for (char c : pw.toCharArray()) {
            if (Character.isUpperCase(c)) upper = true;
            else if (Character.isLowerCase(c)) lower = true;
            else if (Character.isDigit(c)) digit = true;
        }
        return upper && lower && digit;
    }

    public static void main(String[] args) {
        System.out.println(isStrong("Mumbai2026"));  // true
        System.out.println(isStrong("password"));    // false
    }
}

Step 3 — compile and run

javac PasswordCheck.java
java PasswordCheck
# -> true
# -> false

Step 4 — ask for JUnit 5 tests

FOLLOW-UP PROMPT

Write JUnit 5 tests for PasswordCheck.isStrong: strong, too short, no digit, and null.

PasswordCheckTest.java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class PasswordCheckTest {
    @Test void strongPassword() { assertTrue(PasswordCheck.isStrong("Mumbai2026")); }
    @Test void tooShort()       { assertFalse(PasswordCheck.isStrong("Ab1")); }
    @Test void noDigit()        { assertFalse(PasswordCheck.isStrong("MumbaiCity")); }
    @Test void nullIsWeak()     { assertFalse(PasswordCheck.isStrong(null)); }
}
mvn test
# -> Tests run: 4, Failures: 0

Settings and config

  1. Project rules: in .github/copilot-instructions.md or CLAUDE.md write “Java 21, Maven, JUnit 5, no new dependencies without asking”.
  2. IntelliJ: File → Project Structure → SDK = 21, so the AI's modern code (records, switch expressions) compiles.
  3. Format: Code → Reformat Code after accepting AI code, so style stays the same.

Good Java prompts (reuse these)

  1. “Turn this class into a Java record.”
  2. “Add null checks and throw IllegalArgumentException with a clear message.”
  3. “Replace this for loop with a stream, same behaviour, and keep the tests passing.”
  4. “Explain this stack trace and fix it:” + paste only the top lines.

Watch out (common AI mistakes in Java)

  • JUnit 4 vs 5 mix-up — org.junit.Test is JUnit 4; JUnit 5 uses org.junit.jupiter.api.Test.
  • Wrong dependency versions — check on Maven Central.
  • Null handling — always test null input.
  • Old Java — code for Java 8 is fine, but say your version so it can use newer features safely.

Quick quiz

1. Which test library does this chapter use?

JUnit 5 (org.junit.jupiter).

2. What does isStrong("Mumbai2026") return?

true.

3. How do you run Maven tests?

mvn test.

4. Common AI mistake with JUnit imports?

Mixing JUnit 4 org.junit.Test with JUnit 5.

Save tokens and money

SMART AI USE

Paste one class, not the whole project or target/ folder. Paste only the top 10 lines of a stack trace. Let IntelliJ generate getters and constructors for free (Alt+Insert) instead of asking the AI.

Save these cards

INTERVIEW CARDAI Coding

How do you use AI safely for Java?

Give version + build tool + signature + rules.
Compile, then prove with JUnit 5 tests.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

IDE generate vs AI generate

IDE (Alt+Insert): getters, constructors, free and exact.
AI: logic, tests, refactors. Needs review.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

Java with AI in 3

Java 21 + Maven + JUnit 5 in the prompt.
Test null and edge cases.
mvn test = proof.
Ravindra Bagale · ravindrabagale.com/ai-coding