×

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about Java's empty statement.

Empty Statement

An empty statement, as its name implies, contains nothing but a semicolon (;).  It has numerous applications. Let's talk about some of them.

Empty Statement in a for Loop

for  ( int k1 = 0 ; k1 < 10 ; k1++ )  
{  
// Statements
}  
System.out.prinln ( k1 ) ;

As a result, we must refrain from making the variable k's scope local.

int k1 = 0 ;  
for( ; k1 < 10 ; k1++ )  
{  
// statements  
}  
System.out.prinln ( k1 ) ;   

The for loop's initialization block is empty in this case due to the absence of any content.

We can utilize empty statements to build a loop that never ends.

for ( ; ; )   
{  
// statements  
}  

The loop can end, nevertheless, if break statements are used inside the loop's body.

An empty statement can be used to construct a for-loop or while loop with no body. Start with the for-loop.

for ( int i1 = 0 ; int i1 < 10 ; i1++ ) ;  

The semicolon that follows the for loop indicates that the for loop's body is empty.

The for-loop mentioned above can also be expressed as:

for (int i1 = 0 ; i1 < 10 ; i1++ )  
{  
// statements  
}  

Or

for(int i1 = 0 ; i1 < 10 ; i1++)  
{  
;  
}  

The for loop body may contain several empty statements.

for(int i = 0 ; i < 10 ; i++)  
{  
; ;  ; ; ; ; ; ; ; ;  
}  

In this case, the for loop's body contains 10 empty statements.

Empty Statement in while Loop

The while loop can also construct empty statements, just like the for-loop.

int k1 = -1 ;  
while ( k1 < 0 ) ;

Since the body of our while loop is an empty expression, it is identical to

int k1 = -1 ;  
while ( k1 < 0 )  
{  
// body   
}  

The body of the loop can also contain numerous empty statements.

int k = -1 ;  
while ( k < 0 )  
{  
; ; ; ; ; ; ; ; ; ; // 10 empty statements  
}  

Note that it is meaningless to use several empty statements.

Empty Statement in if … else Statement

int k1 = -1 ;  
if ( k1 < 0) ; 

The if statement in the code excerpt above has no body since we immediately followed the if statement with a semicolon. We can reformat the code above snippet similarly to loops as:

int k1 = -1 ;  
if ( k1 < 0 )  
{  
}

Similarly, we can immediately follow another statement with a semicolon. Look at the code below.

int k1 = -1 ;  
if ( k1 > 0 )  
{  
// body 
}  
else ;  

We have also taken out the curly braces in this instance for the else part.

Some Common Mistakes

Using empty statements can lead to several frequent errors. Several of them are listed below.

  1. A compilation error results if we write the else statement right after the if statement and then add two semicolons.

Java Program1 for empty statement

public class EmptyStatement1  
{  
public static void main(String s[])   
{  
int k1 = -1;  
if ( k1 > 0 ) ; ; 
else System . out . println ( " Hi There !! " ) ;  
  
}  
}  

Output

Empty Statement in Java

Explanation

This is because we followed the if condition with two semicolons. The first semicolon denotes the if condition's void body and the second semicolon terminates the condition.  There is an empty statement between the if part and the else part. It is well known that one cannot insert a statement between the if and else sections, even if it is empty. Therefore, since part one requires curly brackets, the code will generate that compilation error if one attempts to insert two empty sentences.

Empty statement Java Program

public class EmptyStatement2  
{  
public static void main(String s[])   
{  
int k1 = -1;  
if(k1 > 0)  
{  
; ;  
}  
else System.out.println("Hi There !!");   
  
}  
}  

Output

Empty Statement in Java

Explanation  

The two empty sentences, in this case, make up the if block, followed by the otherwise statement. As a result, the compiler does not object.

Another typical error is to begin the curly braces in the if... else block or the loops following the semicolon.

Empty statement Java Program

public class  EmptyStatement3  
{  
public static void main ( String s [ ] )   
{  
int k1 = -1 ;  
if(k1 > 0) ;  
{  
System . out . println ( " Hi There !! " ) ;  
}  
  
}  
} 

Output

Empty Statement in Java

Explanation: The part of the if that follows the curly braces is not part of the if part. The semicolon that follows the if the portion is the reason for this. As a result, even though the print command is enclosed in curly brackets and the predicate (k > 0) is false, Hello is displayed.

Empty statement Java Program

public class   EmptyStatement4
{  
public static void main ( String s [ ] )   
{  
for( int j = 0 ; j < 10 ; j++ ) ;  
{  
System.out.println ( " Hi There !! " ) ;  
}  
}  
}

Output

Empty Statement in Java

Explanation: After the for-loop, the curly braces are not a part of the for-loop. As a result, the word "Hello" is only printed once.


Related Topics

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

String Handling Method in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Upcasting in Java

To know what is Upcasting in Java we should first equip ourselves about what is casting or type casting in Java. Casting The process of providing or replacing a reference variable of...

3 minutes read.