Empty Statements in Java

Empty Statements in Java
Empty Statements in Java

Empty Statements in Java:

An empty statement in java consists of only a semicolon(;) It tells JVM not to do anything.
It is particularly useful while implementing delays in the code.

Few Examples of Empty Statements:


if(x<0){
x=-x;
};
//it is legal in java but JVM will consider this an empty statement
 

another example:


if(done)
;//empty statement
else
System.out.println("Oh no");
// it is equivalent to
if(done)
{}//empty statement
else
System.out.println("Oh no");

 

Interesting one:


for(int i=0;i<10;i++);
System.out.println("Hello");
//it will print Hello only once.
 
As we have put the semicolon at the end of the for loop. JVM will consider this as the empty statement and it is equivalent to:

for(int i=0;i<10;i++){
; //empty statement
}
System.out.println("Hello");
 

for creating delay:


for(int i=0;i<1000;i++){
    for(int j=0;i<10000;i++){
    ;
}
}
 
Empty Statements in Java Empty Statements in Java Reviewed by Animesh Chatterjee on January 09, 2019 Rating: 5

No comments:

Powered by Blogger.