V-Shaped Hollow Star Pattern in Java

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

What You'll Learn

This program prints a hollow V (two outer stars on the first row, then closer together each row until one star on the last row). See Program 7 for the inverted V. It is the lower half building block for the hollow diamond (Program 9).

Each 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 = rows; 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

Setup

rows, i, j, k match the hollow-V pattern: two inner loops per row, each cell is System.out.print("*") or System.out.print(" ") from if (i == j) / if (i == k).

Setup
2

Outer loop (reverse rows)

for (int i = rows; i >= 1; i--) prints the widest pair of legs first. The last row (i == 1) only gets a star from the j loop—the k loop starts at 2, so the bottom is a single apex star with leading spaces.

Order
3

Left leg: j from rows down to 1

for (int j = rows; j >= 1; j--) with if (i == j) places the left diagonal. As i counts down, the star in this block moves leftward row by row.

Left leg
4

Right leg: k from 2 to rows

for (int k = 2; k <= rows; k++) mirrors the left leg. When i > 1, you print a second *; when i == 1, this loop never emits a star.

Right leg
5

Finish the line

System.out.println() ends each row. Line width is still 2 * rows - 1, same geometry as the upward hollow V, only the row order is reversed.

Line break
=

Hollow V

Vertex at the bottom, opening upward. O(n²) output for n = rows, O(1) extra space. Wide rows use horizontal scroll in the green preview on small screens. Stack with Program 7 to picture a full hollow diamond.

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 = rows; 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

  • Print the inverted hollow V (Program 7) to see the mirror
  • Build a hollow diamond by printing Program 7 then printing this starting from rows - 1 (Program 9)
  • Replace * with another symbol
  • Use fixed-width font for clean alignment
  • Validate rows > 0 before printing

Avoid

  • Starting the second loop from k = 1 (center duplicates)
  • Forgetting the newline after each row
  • Mixing tabs and spaces
  • Printing stars everywhere (becomes filled)
  • Assuming user input is always valid

Key Takeaways

1

Each row is 2 * rows - 1 characters wide.

2

Stars appear only on the two diagonals in each row (hollow pattern).

3

Start the second loop at k = 2 to avoid duplicating the center column.

4

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

5

This is the lower-half building block for the hollow diamond (Program 9).

❓ Frequently Asked Questions

The first inner loop already includes the center column. Starting from 2 avoids printing the middle position twice.
Print Program 7 (upper half), then print this outer loop starting from rows - 1 to avoid duplicating the middle row (Program 9).
It’s O(n²) because each row prints Theta(n) characters across n rows.

Next: Hollow Diamond Pattern

Continue to Program 9 to combine the two halves into a hollow diamond.

Program 9 →
Did you know?

If you start this descending row loop from rows - 1, you get the lower half of the hollow diamond without printing the middle row twice.

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.

12 people found this page helpful