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
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
- 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
Install and check
- Install JDK 21 (for example Eclipse Temurin 21 from adoptium.net).
- Check:
java -versionandjavac -versionshow 21. - Install Maven, or let IntelliJ create a Maven project for you (New Project → Maven).
- 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
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
# -> falseStep 4 — ask for JUnit 5 tests
Write JUnit 5 tests for PasswordCheck.isStrong: strong, too short, no digit, and null.
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: 0Settings and config
- Project rules: in
.github/copilot-instructions.mdorCLAUDE.mdwrite “Java 21, Maven, JUnit 5, no new dependencies without asking”. - IntelliJ: File → Project Structure → SDK = 21, so the AI's modern code (records, switch expressions) compiles.
- Format: Code → Reformat Code after accepting AI code, so style stays the same.
Good Java prompts (reuse these)
- “Turn this class into a Java record.”
- “Add null checks and throw IllegalArgumentException with a clear message.”
- “Replace this for loop with a stream, same behaviour, and keep the tests passing.”
- “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.Testis JUnit 4; JUnit 5 usesorg.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
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.