Hollow Diamond Star Pattern in Java

What You'll Learn
This pattern is Program 7 (inverted V, upper half) plus the lower half using the same outer idea as Program 8: after i runs 1…rows, run i from rows - 1 down to 1 with the same diagonal conditions.
Each printed line has width 2 * rows - 1 (9 characters when rows = 5).
⭐ 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;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= 1; j--) {
if (i == j) System.out.print("*");
else System.out.print(" ");
}
for (int k = 2; k <= rows; k++) {
if (i == k) System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j >= 1; j--) {
if (i == j) System.out.print("*");
else System.out.print(" ");
}
for (int k = 2; k <= rows; k++) {
if (i == k) System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
}
}🧠 How It Works
Upper half (i = 1 … rows)
for (int i = 1; i <= rows; i++) runs the hollow widening V: each iteration prints one full row with the two inner loops below, then System.out.println(). At i == rows you get stars at both outer columns of the waist.
Lower half (i = rows - 1 … 1)
for (int i = rows - 1; i >= 1; i--) repeats the same nested j / k bodies so the shape narrows again. Starting at rows - 1 avoids printing the widest row twice.
System.out.print on each cell
for (int j = rows; j >= 1; j--): if (i == j) System.out.print("*"); else System.out.print(" ");. Then for (int k = 2; k <= rows; k++) with the same if / else. When i == 1, only the j loop draws the top and bottom apex stars.
New line and width
After both inner loops, System.out.println() ends the row. Every line has fixed width rows + (rows - 1) = 2 * rows - 1 (including interior spaces).
Full hollow diamond
Line count: rows + (rows - 1) = 2 * rows - 1. Each line does Θ(rows) writes → O(n²) for n = rows, O(1) extra space. Width 2n - 1 scrolls horizontally in the green preview on narrow screens.
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();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= 1; j--) {
if (i == j) System.out.print("*");
else System.out.print(" ");
}
for (int k = 2; k <= rows; k++) {
if (i == k) System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j >= 1; j--) {
if (i == j) System.out.print("*");
else System.out.print(" ");
}
for (int k = 2; k <= rows; k++) {
if (i == k) System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Change the character from
*to#or@ - Print a filled diamond by printing stars everywhere inside the boundaries (Program 10)
- Turn this into a box-diamond by adding border stars around the diamond (Program 11 style)
- Use
StringBuilderfor largerowsto reduce prints - Validate
rows > 0before printing
Avoid
- Starting the lower half at
rows(middle row duplicates) - Mixing tabs and spaces (alignment breaks)
- Forgetting newline between rows
- Starting the second inner loop from
k = 1(center duplicates) - Assuming user input is always valid
Key Takeaways
The diamond is built by printing the upper half then mirroring it as the lower half.
Each row is 2 * rows - 1 characters wide.
Lower half starts at rows - 1 to avoid duplicating the middle row.
Time complexity is O(n²) for n rows.
This diamond uses the same diagonal logic as Programs 7 and 8.
❓ Frequently Asked Questions
rows - 1 prevents printing the middle row twice.Next: Filled Diamond Pattern
Continue to Program 10 to print a filled diamond star pattern in Java.
The hollow diamond is a classic interview pattern because it combines symmetry, nested loops, and conditional printing.
14 people found this page helpful
