Open In App

Print Hello World Without using a Semicolon in Java

Last Updated : 23 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

Java Program to Print a Semicolon Without Using Semicolon
The given task is to print a ';' without using ';' anywhere in the code. The simplest idea one can think of to approach this interesting question is using ASCII value somehow in the program. But as Java does not support macros, unlike C/C++, therefore to eliminate ';' at the end of every line in code is to use, if-statement or while loop with print
1 min read
Java Hello World Program
Java is one of the most popular and widely used programming languages and platforms. Java is fast, reliable, and secure. Java is used in every nook and corner from desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet. In this article, we will learn how to write a simple Java Program. Steps to Implem
5 min read
Hello World Example using Stuts2
Apache Struts 2 is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller architecture. In this article, we will see how we can create a simple Hello World application in Struts 2. We are going to use Eclipse IDE so that all
4 min read
Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
Spring Boot - Hello World
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
4 min read
Role of SemiColon in various Programming Languages
Semicolon is a punctuation mark (;) indicating a pause, typically between two main clauses, that is more pronounced than that indicated by a comma. In programming, Semicolon symbol plays a vital role. It is used to show the termination of instruction in various programming languages as well, like C, C++, Java, JavaScript and Python. In this article
5 min read
How to Print an Array in Java Without using Loop?
Given an array arr in Java, the task is to print the contents of this array without using any loop. First let's see the loop method. Loop method: The first thing that comes to mind is to write a for loop from i = 0 to n, and print each element by arr[i].Pseudo Code: for(int i = 0; i < Array.length; i++) System.out.println(Array[i]); Concept: We
2 min read
Java Program to Print any Statement without Using the Main Method
As we know that the static block executes before the main method, hence we can place the statement that we want to execute in the static block, But in case of JDK7, and the above versions of the JDK, the code would not execute as compiler firstly looks for the main method before any other thing. Also, it depends on the IDE being used to run the pro
2 min read
How to Print List Items Without Brackets in Java?
In Java, if you want to print the elements of a list without brackets [ ]. you need to manually iterate through the list and print each element separately. The brackets are automatically added when you directly print the entire list. In this article, we will discuss how to print list items without brackets using Java. Methods to Print List Items wi
2 min read
Apache Kafka - Real World Project with Twitter using Java
Apache Kafka is a publish-subscribe messaging system. A messaging system let you send messages between processes, applications, and servers. Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. To know more about this, please refer to the article - What is Apache Kafka and How Does it Work? In th
5 min read
Article Tags :
Practice Tags :