Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8055891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:32:20+00:00 2026-06-05T08:32:20+00:00

I know assertions can be enabled/disabled at runtime for debugging and production, respectively. However

  • 0

I know assertions can be enabled/disabled at runtime for debugging and production, respectively. However I found that assertions also increase the size of the generated binary (about 100-200 bytes in the example below).

In C and C++, we can do this at compile time by having #define NDEBUG before #include <assert.h>.

Is there any way for the Java compiler to automatically do this? I’d like to leave them in the source code for debugging purposes later on. But I also don’t want the resultant binary to be any larger than necessary (we have a size limit as design requirement).

C code:

//#define NDEBUG
#include <assert.h>

int main(void) {
    assert(0); // +200 bytes without NDEBUG, 0 with NDEBUG
    return 0;
}

Java code:

public class Test {
    public static void main(String[] args) {
        assert(System.nanoTime()==0); // increases binary size by about 200 bytes
    }
}

In response to bn.’s answer:

public class Test2 {
    public static final boolean assertions = false;

    public static void main(String[] args) {
        if(assertions) {
            assert(System.nanoTime()==0);
        }
    }
}

EDIT: In fact, it seems to me that this enabling/disabling is a more useful compile-time feature than run-time. I mean, how many end users will enable them? As far as a programmer is concerned during the debug process, he/she will likely be recompiling code anyways.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:32:22+00:00Added an answer on June 5, 2026 at 8:32 am

    Personally I would not do the following because of the complexity added to the source code but javac generated the exact same intermediate code for main in the following two fragments:

    conditional asserts

    class C {
        public final static boolean assertions = false;
    
        public static void main(String[] args) {
            if(assertions) {
                assert(System.nanoTime()==0);
            }
        }
    }
    

    no asserts

    class C {
        public static void main(String[] args) {
        }
    }
    

    compiled code

      public static void main(java.lang.String[]);
        Code:
           0: return        
        LineNumberTable:
          line 3: 0
    

    EDIT

    In fact, it seems to me that this enabling/disabling is a more useful
    compile-time feature than run-time. I mean, how many end users will
    enable them?

    Its not end users that enable them, it is the customer support that tells the end user to enable them. I do wish though they were enabled, not disabled, by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that it's a subject that can raise a lot of debate, but
How portable is this conversion. Can I be sure that both assertions pass? int
I know that one of the differences between classes and structs is that struct
How can i know upper bound of requests per second my webapp can handle
I need to know if a redefined function in Eiffel can define its own
[EDIT] Okay, so that makes sense, thank you sharptooth and CashCow. You can't delete
I just found out that boost::array::static_size isn't part of tr1::array, or at least it's
example of using assertions during debugging: char* append(char* pStr, const char* pAddStr) { //
I have seen some nice Java compiler hacks wherein you can replace assertions by
Hello another question concerning debugging : Automatically generating test cases when i know the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.