Skip to content
Related Articles

Related Articles

Java Program to Print a Semicolon Without Using Semicolon

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 02 Nov, 2020
Improve Article
Save Article

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 printf(String, Object) method of PrintStream in the condition statement. The main logic here is as printf() returns PrintStream object which is not null so on comparison it with null (literal in Java) we will get our desired output.

Method 1: Using if statement

Java




// Java program to print ; without using ;
// using if statement
  
class PrintSemicolonExample {
    public static void main(String[] args)
    {
        // The ASCII value for semicolon is 59
        if (System.out.printf("%C", 59) == null) {
        }
    }
}

Output

;

Method 2: Using while loop

Java




// Java program to print ; without using ;
// using while loop
  
class PrintSemicolonExample {
    public static void main(String[] args)
    {
        while ((System.out.printf("%C", 59) == null)) {
        }
    }
}

Output

;
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!