Results 1 to 5 of 5

Thread: Assertions

  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Assertions

    ok firstly i didn;t understand the concept of stupid assertions secondly when i tried to run this code of my own on assertions from whtever i understood of it, i dont get any assertion error. ain't am suppose to get error upon passing a negative number as argument???

    class Assert

    {


    public void check(int i)

    {

    assert(i<=0);

    System.out.println("its ok");

    }


    public static void main(String[] args)

    {

    Assert ast = new Assert();

    ast.check(-1);

    }

    }

    Thank you

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Assertions

    -1 <= 0 indeed so nothing fails. Try ast.check(42) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Assertions

    ok now that i remove less than symbol and add greater than symbol still i dont get any error!!! is it that i have to run the program by specifying java -ea Myfilename??

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Assertions

    Quote Originally Posted by java4amanda View Post
    ok now that i remove less than symbol and add greater than symbol still i dont get any error!!! is it that i have to run the program by specifying java -ea Myfilename??
    I don't know, I never use assertions. Try 'assert(true)' and 'assert(false)' and see if anything happens at all. Enabling assertions can never harm.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Assertions

    Quote Originally Posted by JosAH View Post
    I don't know, I never use assertions. Try 'assert(true)' and 'assert(false)' and see if anything happens at all. Enabling assertions can never harm.

    kind regards,

    Jos

    yes but am preparing for scjp 1.5 and bumped into this very irritating topic.

    Thank any ways.

Similar Threads

  1. Assertions
    By jayragz in forum New To Java
    Replies: 1
    Last Post: 05-30-2011, 12:14 PM
  2. Assertions
    By LovJava in forum New To Java
    Replies: 3
    Last Post: 01-07-2010, 10:13 AM
  3. Assertions on Eclipse
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 05-19-2008, 04:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •