You can print a hollow pyramid by placing stars along its two sloping sides and its base. Nested for loops control the leading spaces and the width of each row, while an if statement leaves the interior empty.
Source code
Save this example as HollowPyramidPatternExample.java. It uses a compact source file with void main() and the IO console methods. Use JDK 25 or later; no preview flags or external libraries are required. Compact source files allow the method to appear without an explicit class declaration. See the Java documentation on compact source files.
void main() {
int height = 5;
for (int row = 1; row <= height; row++) {
for (int space = 0; space < height - row; space++) {
IO.print(" ");
}
for (int column = 1; column <= 2 * row - 1; column++) {
if (column == 1 || column == 2 * row - 1 || row == height) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
Explanation
The outer loop counts from 1 through height, producing one line for each row. Two inner loops run in sequence on that line.
The first inner loop prints height - row leading spaces. With a height of five, the leading-space counts are 4, 3, 2, 1, and 0. Reducing this count moves the left edge one position to the left on each successive row.
The second inner loop visits 2 * row - 1 positions. Its width increases through the odd numbers 1, 3, 5, 7, and 9. This makes the right edge move one position to the right while the left edge moves left, keeping the pyramid centered.
At each position, the condition checks whether a star belongs there:
| Condition | Purpose |
|---|---|
column == 1 |
Print the left edge. |
column == 2 * row - 1 |
Print the right edge. |
row == height |
Fill the entire bottom row. |
The logical OR operator, ||, selects a star when any of these conditions is true. All remaining positions contain a single space, creating the empty interior. On the first row, the left and right edges share one position, so only one star is printed.
Both the star and the empty position occupy one character. Use "*" rather than "* " here so the spacing matches the leading-space calculation.
IO.print() writes without ending the current line. After both inner loops finish, IO.println() starts the next row. The IO class is available without an explicit import because it belongs to java.lang.
Run the example
With JDK 25 or later installed, open a terminal in the directory containing the source file and run it directly:
java HollowPyramidPatternExample.java
The Java launcher compiles and runs the source file. You can also compile it explicitly and then run the compiled program:
javac HollowPyramidPatternExample.java
java HollowPyramidPatternExample
Output
*
* *
* *
* *
*********
Display the output in a monospaced font to preserve alignment. Leading spaces are part of the pattern.
Try it yourself
Change height to 3, then to 7. For each row, calculate the number of leading spaces and the row width before running the program. A positive height n produces n rows and a base of 2 * n - 1 stars.
Try height = 1, which prints a single star, and height = 2, which prints a tip above a three-star base. Neither has an empty interior. A zero or negative height produces no output because the outer loop never executes.
Finally, remove || row == height from the condition to leave the base open. Compare the result with the filled shape in How do I print a pyramid pattern using loops in Java?.
