Wednesday, October 8, 2025
HomeLanguagesJavaPrint Hello World Without using a Semicolon in Java

Print Hello World Without using a Semicolon in Java

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

Methods: 

  1. Using if-else statements
  2. Using append() method of StringBuilder class
  3. Using equals method of String class 

Method 1: Using if statement

Java




// Java program to Print Hello World Without Semicolon
// Using if statement
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Using if statement to
        // print hello world
        if (System.out.printf("Hello World") == null) {
        }
    }
}


Output

Hello World

Method 2: Using append() method of StringBuilder class

Java




// Java Program to Print Hello World Without Semicolon
// Using append() method of String class
 
// Importing required classes
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Using append() method to print statement
        if (System.out.append("Hello World") == null) {
        }
    }
}


Output

Hello World

Method 3: Using equals method of String class

Java




// Java Program to Print Hello World Without Semicolon
// Using equals() method of String class
 
// Importing required classes
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Using equals() method to print statement
        if (System.out.append("Hello World").equals(null)) {
        }
    }
}


Output

Hello World
RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS