Questions about using `assert` in Java
Daohan Qu
quadhier at outlook.com
Mon Jul 17 13:34:22 UTC 2023
Hi Alan,
> You will find places in the JDK code, esp. in performance critical code,
> where assertions are commented out. The reason is that asserts, even if
> disabled, increase the method size and can impact inlining by the
> compiler at run-time. So while useful when debugging some issue in such
> code, they are commended out to avoid increasing the method size.
Thanks a bunch! I didn't realize this before!
> Asserts are very useful during development or when testing, e.g. the JDK
> tests run with -esa and can periodically help catch issues when testing
> a change.
Indeed. But I am a little bit curious: unlike C/C++, which enables assert
by default (You could define NDEBUG to disable it during compilation),
Java disables assert by default. I wonder why the language designers
made such a decision. Is it for backward compatibility? IMHO, enabling it by
default might potentially increase its usage among developers.
Regards,
Daohan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20230717/a36d1f0b/attachment.htm>
More information about the core-libs-dev
mailing list