Empty Statements in Java

0
26
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

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++){
    ;
}
}
 
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here