×

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

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.