Skip to content
Related Articles

Related Articles

Print Hello World Without using a Semicolon in Java
  • Difficulty Level : Medium
  • Last Updated : 02 Sep, 2020
GeeksforGeeks - Summer Carnival Banner

Every statement in Java must end with a semicolon as per the basics. However, unlike other languages, almost all statements in Java can be treated as expressions. However, there are a few scenarios when we can write a running program without semicolon. If we place the statement inside an if/for statement with a blank pair of parentheses, we don’t have to end it with a semicolon. Also, calling a function that returns void will not work here as void functions are not expressions.

1. Using if statement:

Java




// Java program to print Hello World without
// semicolon using if statement
class GFG {
    public static void main(String args[])
    {
        if (System.out.printf("Helllo World") == null) {
        }
    }
}
Output
Helllo World

2. Using for loop:

Java




// Java program to print Hello World without
// semicolon using for loop
class GFG {
    public static void main(String args[]) {
        for(int i = 0; i < 1; System.out.println("Hello World")) {
            i++;
        }
    }
}
Output



Hello World

3. Using StringBuilder.append() method:

Java




// Java program to print Hello World without semicolon
// using append method of String class
class GFG {
  
    public static void main(String[] args)
    {
        if (System.out.append("Hello World") == null) {
        }
    }
}
Output
Hello World

4. Using String.equals() method

Java




// Java program to print Hello World without semicolon
// using equals method of string class
class GFG {
    public static void main(String args[])
    {
        if (System.out.append("Hello World")
                .equals(null)) {
        }
    }
}
Output
Hello World

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :