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:
- Using if-else statements
- Using append() method of StringBuilder class
- Using equals method of String class
Method 1: Using if statement
Java
class GFG {
public static void main(String args[])
{
if (System.out.printf( "Hello World" ) == null ) {
}
}
}
|
Method 2: Using append() method of StringBuilder class
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
if (System.out.append( "Hello World" ) == null ) {
}
}
}
|
Method 3: Using equals method of String class
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
if (System.out.append( "Hello World" ).equals( null )) {
}
}
}
|
Last Updated :
23 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...