Java Program to Print a Semicolon Without Using Semicolon
In this tutorial, we are going to learn about Java Program to Print a Semicolon Without Using Semicolon. The task is to print a semicolon without using any semicolons in the code. A possible way of handling this problem is to use ASCII values in the program. However, as Java does not provide macros, various approaches, such as conditional statements or loops are required. One method is the printf(String, Object) method from the PrintStream class, which returns a non-null PrintStream object and allows for the desired output without explicitly mentioning a semicolon.
Example 1:
The example program below demonstrates how to print the semicolon character ';' without using the semicolon directly in the code. We are using an if conditional statementto print a semicolon without using a semicolon in the code and u003B, which is the unicode representation of the semicolon symbol (;). Within the if block we execute the printf() method, which returns null. So, the if block will be executed and no other statements are needed inside the if block.
FileName: SemiColonWithoutSemicolonInCode.java
public class SemiColonWithoutSemicolonInCode { // main method public static void main(String[] args) { // using if statement to print a semicolon without using semicolon in the code // the ASCII value for the semicolon is 59, represented here as Unicode u003B if (System.out.printf(" Printing a semicolon without using semicolon in the code: " + " " + "u003B") == null) { // the printf() method returns null, so the if block will be executed } } }
Output:
Printing a semicolon without using semicolon in the code: ;
Example 2:
The example below is similar to previous example. Here we areconverting the ASCII value 59 to its character representation and including it in the output. And we use the printf method to print a message and the append method for adding the semicolon character in the output. The if statements use these return values to determine whether the operations were successful. The printf function returns null after printing, and the append method returns null after appending.
FileName: SemiColonWithoutSemicolonInCode.java
public class SemiColonWithoutSemicolonInCode { // main method public static void main(String[] args) { //printing a semicolon character without calling System.out.print() directly. if(System.out.printf(" Printing a semicolon without using semi colon in the code : ") == null) { } //converting the ASCII value 59 to its character representation and including it in the output. if(System.out.append(String.valueOf((char)59)) == null) { // the if block executes if append() returns null. } } }
Output:
Printing a semicolon without using semi colon in the code : ;
Example 3:
The example below uses while loop to printa semicolon without using semicolon anywhere in the program. Within the while we use printf() method to print the semicolon using u003B which is the unicode representation of semicolon symbol (;).
FileName: SemiColonWithoutSemicolonInCode.java
public class SemiColonWithoutSemicolonInCode { // main method public static void main(String[] args) { // using while loop to print a semicolon without using semicolon(;) while (System.out.printf(" Printing a semicolon without using semicolon in the code : ") == null) { } while (System.out.printf("%c", 'u003B') == null) { } // the ASCII value for semicolon is 59, represented here as Unicode u003B } }
Output:
Printing a semicolon without using semicolon in the code : ;