Can anyone explain to me in simple English language why we can't use assertion for:

1) Argument checking in public methods.

public class Test{
    public void abc(int a){
         assert a>3
    } 
}

2) Command line arguments.

public class Test{
    public static void main(String[] args)
         assert args.length==3
    } 
}

Your help is kindly appreciated.

Thank You.

Recommended Answers

All 2 Replies

public class Test{
    public static void main(String[] args)
    {
         assert args.length==3;
    } 
}

@solomon
Your code is perfectly right, problem is that you have to enable assertion
by either
java -ea Test arguments
Or
java -enableassertion Test arguments


Hope you cleared.

The answer is:

1) Assert doesn't throw an appropriate exception (i.e. IllegalArgumentException).
2) If assert is disabled then the argument isn't tested.

Does anyone disagree with the answer above?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.