Java Programming - Assertions - Discussion

2. 

public class Test 
{ 
    public void foo() 
    {
        assert false; /* Line 5 */
        assert false; /* Line 6 */
    } 
    public void bar()
    {
        while(true)
        {
            assert false; /* Line 12 */
        } 
        assert false;  /* Line 14 */
    } 
}
What causes compilation to fail?

[A]. Line 5
[B]. Line 6
[C]. Line 12
[D]. Line 14

Answer: Option D

Explanation:

Option D is correct. Compilation fails because of an unreachable statement at line 14. It is a compile-time error if a statement cannot be executed because it is unreachable. The question is now, why is line 20 unreachable? If it is because of the assert then surely line 6 would also be unreachable. The answer must be something other than assert.

Examine the following:

A while statement can complete normally if and only if at least one of the following is true:

- The while statement is reachable and the condition expression is not a constant expression with value true.

-There is a reachable break statement that exits the while statement.

The while statement at line 11 is infinite and there is no break statement therefore line 14 is unreachable. You can test this with the following code:

public class Test80 
{ 
    public void foo() 
    {
        assert false; 
        assert false; 
    } 
    public void bar()
    {
        while(true)
        {
            assert false; 
            break; 
        } 
        assert false;  
    } 
}


Das said: (Nov 9, 2011)  
Now if we declare boolean assert in starting, where will be the error. ? In which line. ?

T V Madhu Sudhana Reddy said: (Nov 20, 2011)  
If we declare boolean assert=true/false in starting and if we are trying to compile from 1.5 version onwards then we will get compile time error.

Complietime error:assert is an keyword from 1.5 version so dont use assert as an identifier.

Vasavi said: (Jun 19, 2013)  
Why it didn't show error in line 6. Why it show at 14. Which method is executed first?

Magi said: (Nov 12, 2013)  
What is assert, what is the use assert in java and give proper explanation on assert?

Kavyashree J said: (Jan 30, 2014)  
What is the difference b/w compilation error and run time error?

How to identify whether it is a compilation error or run time error. Explain me in easy way. Now only I started to learn java.

Jyotiprasad said: (Apr 12, 2014)  
Assert are useful to test our assumptions during development without writing the exception handler for the exception.

Before jdk 1.4 it has been treated as an identifier and onwards from 1.4 it has been first introduced as a keyword.

assert can be write in two ways:

Really simple:

assert(y>x) or assert true or assert false.
Which must result in boolean value.

Simple:

assert(y>x):"y is greater than x".
In first exp is same but second exp can be anything that results in a value.

Yogesh M. said: (Apr 24, 2014)  
Compile time error occurs when your syntax is correct but the logic is wrong while runtime error occurs when your syntax is wrong.

Tushar said: (May 12, 2014)  
Not just comment please told me more about assertion.

Harold said: (Aug 18, 2015)  
The compiler can detect an unreachable statement, but cannot detect a dead loop.

Job Smith said: (Aug 22, 2016)  
I have compiled the code without any compile time error. The only thing it was barking about was missing the main method. I have check the following code:

public class Test80
{
public void foo()
{
assert false;
assert false;
}
public void bar()
{
while(true)
{
assert false;
break;
}
assert false;
}
public static void main(String[] args){}
}

Thus the code is successfully compiled.

Sumit said: (Oct 5, 2016)  
What is the solution, and what does the assert?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.