Hollow Diamond Star Pattern in Java

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n − 1 rows total

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 1rows, 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:

Output
    *    
   * *   
  *   *  
 *     * 
*       *
 *     * 
  *   *  
   * *   
    *    
1

Complete Java Program

Fixed rows = 5 version:

Java
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

1

Upper half (i = 1rows)

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.

Expanding
2

Lower half (i = rows - 11)

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.

Mirrored
3

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.

Diagonals
4

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).

Line break
=

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.

2

Variation — User Input Version

Accept rows with Scanner:

Java
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 StringBuilder for large rows to reduce prints
  • Validate rows > 0 before 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

1

The diamond is built by printing the upper half then mirroring it as the lower half.

2

Each row is 2 * rows - 1 characters wide.

3

Lower half starts at rows - 1 to avoid duplicating the middle row.

4

Time complexity is O(n²) for n rows.

5

This diamond uses the same diagonal logic as Programs 7 and 8.

❓ Frequently Asked Questions

Because the upper half already prints the widest row. Starting from rows - 1 prevents printing the middle row twice.
Instead of printing stars only on the diagonals, print stars for all positions that are inside the diamond boundary (Program 10).
It’s O(n²) because each row prints Theta(n) characters across Theta(n) rows.

Next: Filled Diamond Pattern

Continue to Program 10 to print a filled diamond star pattern in Java.

Program 10 →
Did you know?

The hollow diamond is a classic interview pattern because it combines symmetry, nested loops, and conditional printing.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful