You can approximate a hollow circle in the console by printing stars only at grid points within a narrow ring. Nested for loops visit the grid, and an if statement checks whether each point lies between an inner and an outer radius.
Source code
Save this example as HollowCirclePatternExample.java. It uses a Java 25 compact source file with void main() and the IO console methods. Use JDK 25 or later; no preview flags or external libraries are required.
void main() {
int radius = 5;
int innerRadius = radius - 1;
int outerRadiusSquared = radius * radius;
int innerRadiusSquared = innerRadius * innerRadius;
for (int y = -radius; y <= radius; y++) {
for (int x = -radius; x <= radius; x++) {
int distanceSquared = x * x + y * y;
if (distanceSquared >= innerRadiusSquared
&& distanceSquared <= outerRadiusSquared) {
IO.print("* ");
} else {
IO.print(" ");
}
}
IO.println();
}
}
Explanation
Imagine a circle centered at (0, 0). The squared distance from the center to a point (x, y) is x * x + y * y. Comparing squared distances lets the program test positions without calculating square roots.
The outer radius is five, and the inner radius is four. A position contains a star when its squared distance is at least 16 and at most 25. This selects a ring between the two radii, including both boundaries.
| Condition | Purpose |
|---|---|
distanceSquared >= innerRadiusSquared |
Exclude points strictly inside the inner circle. |
distanceSquared <= outerRadiusSquared |
Exclude points outside the outer circle. |
The logical AND operator, &&, requires both conditions to be true. Points too close to the center remain empty, as do points outside the ring.
The outer loop visits vertical coordinates from -radius through radius. The inner loop visits horizontal coordinates over the same range. Including both endpoints produces an 11-by-11 grid of logical positions for a radius of five.
Each star position prints "* ", and each empty position prints " ". Both strings occupy two character positions, preserving alignment. The extra horizontal spacing often makes the circle look more balanced because terminal characters are usually taller than they are wide. Its apparent proportions still depend on the font.
IO.print() stays on the current line, while IO.println() ends each grid row. The IO class belongs to java.lang, so no explicit import is needed.
The ring is an approximation on a discrete grid. Its edge will look stepped, and the number of stars per row will vary. Keep the radius small for this beginner example; very large values can overflow the int arithmetic and produce impractically large output.
Run the example
With JDK 25 or later installed, open a terminal in the directory containing the source file and run it directly:
java HollowCirclePatternExample.java
You can also compile the source file explicitly and then run the compiled program:
javac HollowCirclePatternExample.java
java HollowCirclePatternExample
Output
*
* * * * * * *
* * * *
* *
* *
* * * *
* *
* *
* * * *
* * * * * * *
*
Display the output in a monospaced font to preserve alignment. Trailing spaces are omitted from the output shown here; leading spaces are preserved.
Try it yourself
Change radius to 8 and keep innerRadius = radius - 1. Compare the larger pattern with the original. More grid positions give the circle more detail, though its outline remains stepped.
For the original radius of five, change innerRadius to radius - 2. The inner radius becomes three, creating a thicker ring. Use an inner radius greater than zero and smaller than the outer radius to keep a hole in the center.
Try radius = 1 with the original innerRadius = radius - 1. The inner radius becomes zero, so the center is included and the result has no hole. Use a radius of at least two for a hollow pattern with this formula.
Finally, remove the inner-radius test and keep only distanceSquared <= outerRadiusSquared. The result becomes a filled circle, as in How do I print a circle pattern using loops in Java?.
