Hollow Diamond Inside Square Star Pattern in Java

What You'll Learn
You print a frame with a solid row of * on the first line and again on the last line, and mirror the rows in between so the middle row is narrowest (only the left and right border stars with a wide hollow gap).
The width is 2 * rows; the number of lines is 2 * rows - 1. This layout differs from the standalone hollow diamond outline (Program 9).
⭐ Pattern Output
When you run the program with rows = 5:
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********Complete Java Program
Fixed rows = 5 version:
public class Main {
public static void main(String[] args) {
int rows = 5;
int height = 2 * rows - 1;
int width = 2 * rows;
for (int line = 1; line <= height; line++) {
if (line == 1 || line == height) {
for (int j = 1; j <= width; j++) System.out.print("*");
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (int j = 1; j <= left; j++) System.out.print("*");
for (int j = 1; j <= gap; j++) System.out.print(" ");
for (int j = 1; j <= left; j++) System.out.print("*");
}
System.out.println();
}
}
}🧠 How It Works
Grid size
height = 2 * rows - 1 lines; width = 2 * rows characters per line (even width for the closed frame). For rows = 4 that is 7 lines × 8 columns, matching the sample output.
Top and bottom bars
When line == 1 or line == height, for (int j = 1; j <= width; j++) System.out.print("*"); draws a solid horizontal edge.
Inner rows: left, gap, right
Else branch: int i = (line <= rows) ? line : (2 * rows - line);, int left = rows - i + 1;, int gap = 2 * (i - 1);. Three loops: left stars, gap spaces, left stars again—all via System.out.print. The hollow region grows until i == rows, then shrinks symmetrically.
Finish each line
After either the full bar or the three inner segments, System.out.println() advances to the next row. Every line is exactly width = 2 * rows characters wide before the newline.
Diamond in a frame
O(n²) output for n = rows (each of 2n-1 lines writes up to 2n cells), O(1) extra space. Top and bottom bars are the widest; on small screens the green preview uses horizontal scroll so all 2n columns stay readable.
Variation — User Input Version
Accept rows with Scanner:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
int height = 2 * rows - 1;
int width = 2 * rows;
for (int line = 1; line <= height; line++) {
if (line == 1 || line == height) {
for (int j = 1; j <= width; j++) System.out.print("*");
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (int j = 1; j <= left; j++) System.out.print("*");
for (int j = 1; j <= gap; j++) System.out.print(" ");
for (int j = 1; j <= left; j++) System.out.print("*");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Validate
rows > 0before printing - Print only the hollow diamond outline (Program 9) for comparison
- Change the frame character (e.g., use
#) - Use
StringBuilderfor largerowsto reduce prints - Try making the frame hollow (border only) instead of solid top/bottom bars
Avoid
- Using
2 * rows - 1as width (this pattern uses2 * rows) - Forgetting to mirror
ifor the bottom half - Mixing tabs and spaces (alignment breaks)
- Forgetting newline after each line
- Assuming user input is always valid
Key Takeaways
Width is 2 * rows and height is 2 * rows - 1.
Top and bottom lines are fully filled with stars (frame).
Middle lines are symmetric using mirrored i values.
Time complexity is O(n²) for n rows.
This is a different layout than the standalone hollow diamond (Program 9).
❓ Frequently Asked Questions
2 * rows and height 2 * rows - 1 so the top and bottom can be solid horizontal bars while the inner rows close on the left and right sides.2 * rows - 1. Program 11 adds a frame-like look using width 2 * rows and solid top/bottom rows.Explore: All Star Patterns
Browse the full Java star pattern series.
This pattern is a popular variation because it combines a frame-like look with a symmetric hollow diamond.
9 people found this page helpful
