The Wayback Machine - https://web.archive.org/web/20240930211742/https://www.geeksforgeeks.org/java-pattern-programs/
Open In App

Java Pattern Programs – Learn How to Print Pattern in Java

Last Updated : 05 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In many Java interviews Star, number, and character patterns are the most asked Java Pattern Programs to check your logical and coding skills. Pattern programs in Java help you to sharpen your looping concepts(for loop). Now if you are looking for a place to get all the Java pattern exercises with solutions, then stop your search here.

Here we have compiled a top pattern program on Java. Now, remember that to learn pattern programs, you must know Java Loops.

Patterns Programs in Java

Patterns Programs in Java

Java Pattern Programs

Here, you will find the top 25 Java pattern programs with their proper code and explanation. 

All Pattern Programs in Java are mentioned below:

1. Square Hollow Pattern

Java
// Java Program to print pattern
// Square hollow pattern
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = 0; i < n; i++) {
            //  inner loop to handle number of columns
            for (j = 0; j < n; j++) {
                // star will print only when  it is in first
                // row or last row or first column or last
                // column
                if (i == 0 || j == 0 || i == n - 1
                    || j == n - 1) {
                    System.out.print("*");
                }
                // otherwise print space only.
                else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
******
*    *
*    *
*    *
*    *
******

2. Number triangle Pattern

Java
// Java Program to print pattern
// Number triangle pattern
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to print space
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print star
            for (j = 1; j <= i; j++) {
                System.out.print(i + " ");
            }
            // print new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
     1 
    2 2 
   3 3 3 
  4 4 4 4 
 5 5 5 5 5 
6 6 6 6 6 6 

3. Number-increasing Pyramid Pattern 

Java
// Java Program to print pattern
// Number-increasing pyramid 
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to handle number of columns
            for (j = 1; j <= i; j++) {
                // printing column values upto the row
                // value.
                System.out.print(j + " ");
            }

            // print new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 

4. Number-increasing reverse Pyramid Pattern

Java
// Java Program to print pattern
// Number-increasing reverse pyramid
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = n; i >= 1; i--) {
            // inner loop to handle number of columns
            for (j = 1; j <= i; j++) {
                // printing column values upto the row
                // value.
                System.out.print(j + " ");
            }

            // print new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

5. Number-changing Pyramid Pattern

Java
// Java Program to print pattern
// Number-changing pyramid
import java.util.*;

// Java code for printing pattern
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to handle number of columns
            for (j = 1; j <= i; j++) {
                // printing value of num in each iteration.
                System.out.print(num + " ");
                // increasing the value of num.
                num++;
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 

6. Zero-One Triangle Pattern

Java
// Java Program to print pattern
// Zero-One triangle
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        //outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            //inner loop to handle number of columns
            for (j = 1; j <= i; j++) {
                // if the sum of (i+j) is even then print 1
                if ((i + j) % 2 == 0) {
                    System.out.print(1 + " ");
                }
                // otherwise print 0
                else {
                    System.out.print(0 + " ");
                }
            }

            //printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 
0 1 0 1 0 1 

7. Palindrome Triangle Pattern

Java
// Java Program to print pattern
// Palindrome triangle
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;

        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to print the spaces
            for (j = 1; j <= 2 * (n - i); j++) {
                System.out.print(" ");
            }

            // inner loop to print the first part
            for (j = i; j >= 1; j--) {
                System.out.print(j + " ");
            }

            // inner loop to print the second part
            for (j = 2; j <= i; j++) {
                System.out.print(j + " ");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
          1 
        2 1 2 
      3 2 1 2 3 
    4 3 2 1 2 3 4 
  5 4 3 2 1 2 3 4 5 
6 5 4 3 2 1 2 3 4 5 6 

8. Rhombus Pattern

Java
// Java Program to print
// Rhombus pattern
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }

            // inner loop to print stars
            for (j = 1; j <= n; j++) {
                System.out.print("*");
            }
            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
     ******
    ******
   ******
  ******
 ******
******

9. Diamond Star Pattern

Java
// Java Program to print
// Diamond Star Pattern
import java.util.*;

public class GeeksForGeeks {

    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
        // outer loop to handle upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print stars
            for (j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // outer loop to handle lower part
        for (i = n-1; i >= 1; i--) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print stars
            for (j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

10. Butterfly Star Pattern

Java
// Java Program to print
// Butterfly Pattern
import java.util.*;

// Java code for printing pattern
public class GeeksForGeeks {

    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;

        // outer loop to handle upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // inner loop to print spaces
            int spaces = 2 * (n - i);
            for (j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }

            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }

            System.out.println();
        }

        // outer loop to handle lower part
        for (i = n; i >= 1; i--) {
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // inner loop to print spaces
            int spaces = 2 * (n - i);
            for (j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }

            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }

            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
*          *
**        **
***      ***
****    ****
*****  *****
************
************
*****  *****
****    ****
***      ***
**        **
*          *

11. Square Fill Pattern

Java
// Java Program to print
// Square fill pattern
import java.util.*;

public class GeeksForGeeks {

    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;

        // outer loop to handle rows
        for (i = 0; i <= n; i++) {

            // inner loop to handle columns
            for (j = 0; j <= n; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
*******
*******
*******
*******
*******
*******
*******

12. Right Half Pyramid Pattern

Java
// Java Program to print
// Pyramid pattern
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;

        // outer loop to handle rows
        for (i = 1; i <= n; i++) {

            // inner loop to handle columns
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
*
**
***
****
*****
******

13. Reverse Right Half Pyramid Pattern

Java
// Java Program to print pattern
// Reverse Right Half Pyramid
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;

        // outer loop to handle rows
        for (i = n; i >= 1; i--) {

            // inner loop to handle columns
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
******
*****
****
***
**
*

14. Left Half Pyramid Pattern

Java
// Java Program to print pattern
// Left Half Pyramid pattern
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;

        // outer loop to handle rows
        for (i = n; i >= 1; i--) {

            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }

            // inner loop to print stars.
            for (j = 0; j <= n - i; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
     *
    **
   ***
  ****
 *****
******

15. Reverse Left Half Pyramid Pattern

Java
// Java Program to print pattern
// Reverse Left Half Pyramid 
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;

        // calculating number of spaces
        int num = 2 * n - 2;

        // outer loop to handle rows
        for (i = n; i > 0; i--) {
            // inner loop to print spaces.
            for (j = 0; j < n - i; j++) {
                System.out.print(" ");
            }
            // Decrementing value of num after each loop
            num = num - 2;
            // inner loop to print stars.
            for (j = 0; j < i; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
******
 *****
  ****
   ***
    **
     *

16. Triangle Star Pattern

Java
// Java Program to print
// Triangular Pattern
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle rows
        for (i = 0; i < n; i++) {
            // inner loop to print spaces.
            for (j = n - i; j > 1; j--) {
                System.out.print(" ");
            }

            // inner loop to print stars.
            for (j = 0; j <= i; j++) {
                System.out.print("* ");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 

17. Reverse number Triangle Pattern

Java
// Java Program to print pattern
// Reverse number triangle
import java.util.*;

public class GeeksForGeeks {

    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle rows
        for (i = 1; i <= n; i++) {

            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }

            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                System.out.print(j + " ");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
1 2 3 4 5 6 
 2 3 4 5 6 
  3 4 5 6 
   4 5 6 
    5 6 
     6 

18. Mirror Image Triangle Pattern

Java
// Java Program to print pattern
// Mirror Image of a triangle
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // Printing the upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                System.out.print(j + " ");
            }

            // printing new line for each row
            System.out.println();
        }

        // Printing the lower part
        for (i = n - 1; i >= 1; i--) {
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                System.out.print(j + " ");
            }
            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
1 2 3 4 5 6 
 2 3 4 5 6 
  3 4 5 6 
   4 5 6 
    5 6 
     6 
    5 6 
   4 5 6 
  3 4 5 6 
 2 3 4 5 6 
1 2 3 4 5 6 

19. Hollow Triangle Pattern

Java
// Java Program to print 
// Hollow triangle pattern
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j, k;

        // outer loop to handle rows
        for (i = 1; i <= n; i++) {
          
            // inner loop to print spaces.
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
          
            for (k = 1; k <= (2 * i - 1); k++) {
                // printing stars.
                if (k == 1 || i == n || k == (2 * i - 1)) {
                    System.out.print("*");
                }
                // printing spaces.
                else {
                    System.out.print(" ");
                }
            }

            System.out.println("");
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
     *
    * *
   *   *
  *     *
 *       *
***********

20. Hollow Reverse Triangle Pattern

Java
// Java Program to print pattern
// Reverse Hollow triangle
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j, k;

        // outer loop to handle rows
        for (i = n; i >= 1; i--) {

            // inner loop to print spaces.
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }

            for (k = 1; k <= (2 * i - 1); k++) {
                // printing stars.
                if (k == 1 || i == n || k == (2 * i - 1)) {
                    System.out.print("*");
                }
                // printing spaces.
                else {
                    System.out.print(" ");
                }
            }

            System.out.println("");
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
***********
 *       *
  *     *
   *   *
    * *
     *

21. Hollow Diamond Pyramid

Java
// Java Program to print Pattern
// Hollow Diamond Star
import java.util.*;

public class GeeksForGeeks {

    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
        // outer loop to handle upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print stars
            for (j = 1; j <= 2 * i - 1; j++) {
                if (j == 1 || j == 2*i-1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }

        // outer loop to handle lower part
        for (i = n-1; i >= 1; i--) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print stars
            for (j = 1; j <= 2 * i - 1; j++) {
                if (j == 1 || j == 2*i-1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
     *
    * *
   *   *
  *     *
 *       *
*         *
 *       *
  *     *
   *   *
    * *
     *

22. Hollow Hourglass Pattern

Java
// Java Program to print pattern
// Hollow Hourglass Pattern
import java.util.*;

public class GeeksForGeeks {
  
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
      
        // Printing the upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                if(j==i||j==n||i==1)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }

            // printing new line for each row
            System.out.println();
        }

        // Printing the lower part
        for (i = n - 1; i >= 1; i--) {
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                if(j==i||j==n||i==1)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
* * * * * * 
 *       * 
  *     * 
   *   * 
    * * 
     * 
    * * 
   *   * 
  *     * 
 *       * 
* * * * * * 

23. Pascal’s Triangle

Java
// Java Program to implement
// Pascal's Triangle
import java.util.*;

class GFG {

    // Pascal function
    public static void printPascal(int n)
    {
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= n - i; j++) {

                // for left spacing
                System.out.print(" ");
            }

            // used to represent x(i, k)
            int x = 1;
            for (int k = 1; k <= i; k++) {

                // The first value in a line is always 1
                System.out.print(x + " ");
                x = x * (i - k) / k;
            }
            System.out.println();
        }
    }

    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
        printPascal(n);
    }
}

Output
    1 
   1 1 
  1 2 1 
 1 3 3 1 

24. Right Pascal’s Triangle

Java
// Java Program to print
// Right Pascal’s Triangle
import java.util.*;

// Java code for printing pattern
public class Main {

    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;

        // outer loop to handle upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("* ");
            }

            System.out.println();
        }

        // outer loop to handle lower part
        for (i = n-1; i >= 1; i--) {
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 4;
        printPattern(n);
    }
}

Output
* 
* * 
* * * 
* * * * 
* * * 
* * 
* 

25. K Pattern

Java
// Java Program to print pattern
// Reverse Right Half Pyramid
import java.util.*;

public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;

        // outer loop to handle rows
        for (i = n; i >= 1; i--) {

            // inner loop to handle columns
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }

        // outer loop to handle rows
        for (i = 2; i <= n; i++) {

            // inner loop to handle columns
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }
    }

    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}

Output
******
*****
****
***
**
*
**
***
****
*****
******

Conclusion

Java pattern programs are a great way to learn and practice coding skills. They help you understand loops, nested loops, and how to think logically to solve problems. Whether you are a beginner or an experienced programmer, practicing pattern programs can improve your Java skills. So, keep coding, experimenting with different patterns, and enjoy the learning process

Java Pattern Programs – Frequently Asked Questions (FAQ)

What are Java Pattern Programs?

Java Pattern Programs are exercises that use nested loops and print statements to create patterns in Java, helping improve control flow and logical thinking.

Why are Java Pattern Programs important?

They develop problem-solving skills, logical thinking, and reinforce Java fundamentals like loops and conditional statements.

What are some common Java Pattern Programs?

Common patterns include half-pyramids, full pyramids, inverted pyramids, diamond shapes, and alphabet patterns.

How do you approach solving Java Pattern Programs?

Understand the pattern, break it into smaller steps, and design outer and inner loops with proper conditions for printing.

What are the benefits of practicing Java Pattern Programs?

They enhance problem-solving, reinforce control flow concepts, and help write cleaner, more efficient code.

How can I get started with Java Pattern Programs?

Start with simple patterns and gradually increase complexity. Practice using resources like GeeksforGeeks and coding challenges



Previous Article
Next Article

Similar Reads

Output of Java Programs | Set 55 (Java Collections Framework)
Pre-requisites: Java Collection Framework. 1. What is the Output of following Java Program? import java.util.*; class Demo { public static void main(String[] args) { ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(11); arr.add(2); arr.add(3); arr.add(5); arr.add(7); arr.remove(new Integer(7)); arr.remove(2); for (int i = 0; i
6 min read
Java Programs - Java Programming Examples
In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs. Java is one of the most popular programming languages today because of its simplicity. Java programming concepts such as co
12 min read
Java Exercises - Basic to Advanced Java Practice Programs with Solutions
Looking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills. As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers often find it difficult to find a platform for Jav
8 min read
Output of Java Programs | Set 54 (Vectors)
Prerequisite : Vectors in Java Basics 1. What is the Output Of the following Program Java Code import java.util.*; class demo1 { public static void main(String[] args) { Vector v = new Vector(20); System.out.println(v.capacity()); System.out.println(v.size()); } } Output: 20 0 Explanation: function - int capacity( ) Returns the capacity of the vect
6 min read
Output of Java programs | Autoboxing and Unboxing
Prerequisite - Autoboxing and unboxing in Java 1)What is the output of the following program? class Main { public static void main(String[] args) { Double x1, y1, z1; double x2, y2, z2; x1 = 10.0; y1 = 4.0; z1 = x1 * x1 + y1 * y1; x2 = 10.0; y2 = 4.0; z2 = x2 * x2 + y2 * y2; System.out.print(z1 + " "); System.out.println(z2); } } Options:
3 min read
Compile and Run Java Programs in Sublime Text in Linux
Sublime Text is a free minimalist coding editor developed by Sublime HQ for desktop use. This development and IT program enable you to solely focus on your code, leaving all the other types of eye-candy out. Procedure: Open terminal and the specific command are entered there in order to check for the software is there or not. Existence will be disp
2 min read
How to Write Robust Programs with the Help of Loops in Java?
Here we will discuss how we can write effective codes with the help of loops. It is a general perception that the approach using loops is treated as naive approach to solve a problem statement. But still, there is a huge scope of improvisation here. So basically, a loop statement allows us to execute a statement or group of statements multiple time
4 min read
Java OpenCV Programs - Basic to Advanced
Java is a popular programming language that can be used to create various types of applications, such as desktop, web, enterprise, and mobile. Java is also an object-oriented language, which means that it organizes data and behaviour into reusable units called classes and objects. Java is known for its portability, performance, security, and robust
2 min read
Java Threading Programs - Basic to Advanced
Java threading is the concept of using multiple threads to execute different tasks in a Java program. A thread is a lightweight sub-process that runs within a process and shares the same memory space and resources. Threads can improve the performance and responsiveness of a program by allowing parallel execution of multiple tasks. Java threading pr
3 min read
Java Programs Examples on Apache PDFBox
Apache PDFBox is an open-source Java library that allows you to work with PDF documents. You can use Apache PDFBox to create new PDF documents, manipulate existing ones, and extract content from them. Apache PDFBox also provides several command-line utilities for common tasks, such as splitting, merging, validating, and signing PDF files. Apache PD
3 min read
Few Tricky Programs in Java
Comments that execute : Till now, we were always taught "Comments do not Execute". Let us see today "The comments that execute" Following is the code snippet: public class Testing { public static void main(String[] args) { // the line below this gives an output // \u000d System.out.println("comment executed"); } } Output: comment executed
2 min read
Programs for printing pyramid patterns in Java
This article is aimed at giving a Java implementation for pattern printing. Simple pyramid pattern Java Code import java.io.*; // Java code to demonstrate star patterns public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printStars(int n) { int i, j; // outer loop to handle number of rows // n in this case fo
11 min read
Output of Java programs | Set 10 (Garbage Collection)
Prerequisite - Garbage Collection in Java Difficulty level : Intermediate In Java, object destruction is taken care by the Garbage Collector module and the objects which do not have any references to them are eligible for garbage collection. Below are some important output questions on Garbage collection. Predict the output of following Java Progra
4 min read
Output of Java Programs | Set 12
1) What is the output of the following program? Java Code public class Test implements Runnable { public void run() { System.out.printf("%d", 3); } public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(new Test()); thread.start(); System.out.printf("%d", 1); thread.join(); System.out.pri
5 min read
Output of Java programs | Set 13 (Collections)
Prerequisite - Collections in Java 1) What is the output of the following program? Java Code import java.util.*; public class priorityQueue { public static void main(String[] args) { PriorityQueue<Integer> queue = new PriorityQueue<>(); queue.add(11); queue.add(10); queue.add(22); queue.add(5); queue.add(12); queue.add(2); while (queue.
3 min read
Output of Java programs | Set 24 (Final Modifier)
Difficulty level : Easy Prerequisite : final keyword in java Predict the output of following Java Programs: What will be output of following program? class Test { final int MAXIMUM; final double PI; public Test(int max) { MAXIMUM = max; } public Test(double pi) { PI = pi; } public static void main(String[] args) { Test t1 = new Test(1500); Test t2
3 min read
Java Networking Programs - Basic to Advanced
Java allows developers to create applications that can communicate over networks, connecting devices and systems together. Whether you're learning about basic connections or diving into more advanced topics like client-server applications, Java provides the tools and libraries you need. This Java Networking programs will guide you through essential
3 min read
Java Apache POI Programs - Basic to Advanced
This Java Apache POI program guide provides a variety of programs on Java POI, that are frequently asked in the technical round in various Software Engineering/core JAVA Developer Interviews. Additionally, All practice programs come with a detailed description, Java code, and output. Apache POI is an open-source java library to create and manipulat
3 min read
Java String Programs
A String in Java is a sequence of characters that can be used to store and manipulate text data and It is basically an array of characters that are stored in a sequence of memory locations. All the strings in Java are immutable in nature, i.e. once the string is created we can't change it. This article provides a variety of programs on strings, tha
4 min read
Java File Handling Programs
Java is a programming language that can create applications that work with files. Files are containers that store data in different formats, such as text, images, videos, etc. Files can be created, read, updated, and deleted using Java. Java provides the File class from the java.io package to handle files. The File class represents a file or a dire
3 min read
Java Collection Programs - Basic to Advanced
As it cleared from its name itself "Collection" it is a pre-defined collective bunch of classes and Interfaces present in the "Collection Framework" in Java. Their Classes, Interfaces and Methods are frequently used in competitive programming. This article provides a variety of programs on Java Collections, that are frequently asked in the Technica
4 min read
Java Directories Programs: Basic to Advanced
Directories are an important part of the file system in Java. They allow you to organize your files into logical groups, and they can also be used to control access to files. In this article, we will discuss some of the Java programs that you can use to work with directories. We will cover how to create directories, delete directories, check if a d
3 min read
Java Array Programs
An array is a data structure consisting of a collection of elements (values or variables), of the same memory size, each identified by at least one array index or key. An array is a linear data structure that stores similar elements (i.e. elements of similar data type) that are stored in contiguous memory locations. This article provides a variety
4 min read
Java JDBC Programs - Basic to Advanced
This article provides a variety of programs on JDBC, that are frequently asked in the technical round in various Software Engineering/JAVA Backend Developer Interviews including various operations such as CREATE, INSERT, UPDATE, DELETE and SELECT on SQL Database etc. Additionally, all programs come with a detailed description, Java code, and output
3 min read
Output of Java Programs | Set 14 (Constructors)
Prerequisite - Java Constructors 1) What is the output of the following program? [GFGTABS] Java class Helper { private int data; private Helper() { data = 5; } } public class Test { public static void main(String[] args) { Helper help = new Helper(); System.out.println(help.data); } } [/GFGTABS]a) Compilation error b) 5 c) Runtime error d) None of
3 min read
Java Regex Programs - Basic to Advanced
In Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Regular Expressions in Java are provided under java.util.regex package. This consists of 3 classes and 1 interface. In Java Interviews, Regex questions are generally asked by Int
5 min read
Pattern pattern() method in Java with Examples
The pattern() method of the Pattern class in Java is used to get the regular expression which is compiled to create this pattern. We use a regular expression to create the pattern and this method used to get the same source expression. Syntax: public String pattern() Parameters: This method does not accepts anything as parameter. Return Value: This
2 min read
Java Program to Print Diamond Shape Star Pattern
In this article, we are going to learn how to print diamond shape star patterns in Java. Illustration: Input: number = 7 Output: * *** ***** ******* ********* *********** ************* *********** ********* ******* ***** *** * Methods: When it comes to pattern printing we do opt for standard ways of printing them via loops only. We will try out dif
6 min read
Java Program to Print a Square Pattern for given integer
Write a java program to print the given Square Pattern on taking an integer as input from commandline. Examples: Input : 3 Output :1 2 3. 7 8 9 4 5 6 Input :4 Output :1 2 3 4 9 10 11 12 13 14 15 16 5 6 7 8 Java Code // Java program to print a square pattern with command // line one argument import java.util.*; import java.lang.*; public class Squar
2 min read
Java Program to Print Alphabet Inverted Heart Pattern
Alphabet Inverted Heart Pattern consists of two parts. The upper part of the pattern is a triangle. The base of the pattern has two peaks and a gap between them. Hence, the desired pattern looks like as shown in the illustration. Illustration: A BBB CCCCC DDDDDDD EEEEEEEEE FFFFFFFFFFF GGGGGGGGGGGGG HHHHHHHHHHHHHHH IIIIIIIIIIIIIIIII JJJJJJJJJJJJJJJJ
4 min read
Practice Tags :