Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Assertion Question

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Assertion Question

    public class NewClass2 {
     
        public static void main(String[] args) {
     
            int x = 100;
            int y = 20;
     
            assert y > x;
        }
    }

    i want to confirm my understanding, with the basic usage of assertion, as of now, im at the start of the book chapter im reading about assertions

    question:

    does assertion means in layman's term "make sure"
    or in the above code, is it similar to or correct if i would say "make sure y is less than x, when y is set lower than x"?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Assertion Question

    Very similar to your 'make sure' definition. The assertion checks whether something is true...if it is not, the assertion exception is thrown (assuming assertions are enabled). This can get to be a valuable tool in working with larger applications and algorithms, as assertions define something the programmer knows must be true, thus assertion failures define a fundamental flaw in the algorithm/runtime.

  3. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (August 16th, 2011)