What does the Java assert keyword do, and when should it be used?

前端 未结 19 1509
眼角桃花
眼角桃花 2020-11-22 15:58

What are some real life examples to understand the key role of assertions?

相关标签:
19条回答
  • 2020-11-22 16:35

    Basically, "assert true" will pass and "assert false" will fail. Let's looks at how this will work:

    public static void main(String[] args)
    {
        String s1 = "Hello";
        assert checkInteger(s1);
    }
    
    private static boolean checkInteger(String s)
    {
        try {
            Integer.parseInt(s);
            return true;
        }
        catch(Exception e)
        {
            return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题