DEV Community

Priyanshu srivastava
Priyanshu srivastava

Posted on • Updated on

Java Program to print something without using semi-colon

In this program, we use a if statement and System.out.printf() because it returns object of PrintStream which is not null when something is passed to the printf().

public class Main {
    public static void main(String[] args) {
        if (System.out.printf("Hello World 👋") != null) {}
    }
}
Enter fullscreen mode Exit fullscreen mode

whereas, System.out.println() and System.out.print() return void which cannot be used for a comparison.

Top comments (5)

Collapse
 
cicirello profile image
Vincent A. Cicirello

Why?

Collapse
 
priyanshusrivastava profile image
Priyanshu srivastava

can you elaborate on the question plz?

Collapse
 
cicirello profile image
Vincent A. Cicirello

Sure. Why, as in, why would you want to deliberately use code smells, such as an empty block, etc, just to avoid a semicolon?

Thread Thread
 
priyanshusrivastava profile image
Priyanshu srivastava

It's nothing but a quiz question which was asked in my university, and I thought it would be nice for the community to know it, out there. 😁

Thread Thread
 
cicirello profile image
Vincent A. Cicirello

Wow, that's an unexpected (and unfair) question for a programming instructor to ask on a quiz, a question that can only be answered by using code smells.