Is Java assert broken?

后端 未结 12 800
走了就别回头了
走了就别回头了 2020-12-29 23:33

While poking around the questions, I recently discovered the assert keyword in Java. At first, I was excited. Something useful I didn\'t already know! A more

12条回答
  •  星月不相逢
    2020-12-30 00:15

    Java's assertions aren't really made for argument validation - it's specifically stated that assertions are not to be used instead of dear old IllegalArgumentException (and neither is that how they are used in C-ish languages). They are more there for internal validation, to let you make an assumption about the code which isn't obvious from looking at it.

    As for turning them off, you do that in C(++), too, just that if someone's got an assert-less build, they have no way to turn it on. In Java, you just restart the app with the appropriate VM parameters.

提交回复
热议问题