assert

Discussion in 'Java' started by Matthias Kaeppler, Jun 26, 2005.

  1. Hi,

    I want to use assertions in my Java 5 code. The javac manpage says I
    have to compile with `-source 1.4` to enable assert as a keyword, but
    this also leads to java 5 features like enums not working anymore.

    --
    Matthias Kaeppler
     
    Matthias Kaeppler, Jun 26, 2005
    #1
    1. Advertising

  2. On Sun, 26 Jun 2005 11:19:39 +0200, Matthias Kaeppler wrote:

    > Hi,
    >
    > I want to use assertions in my Java 5 code. The javac manpage says I
    > have to compile with `-source 1.4` to enable assert as a keyword, but
    > this also leads to java 5 features like enums not working anymore.


    assert works with and -source >= 1.4, so you should be fine with the
    default behaviour. This part of the documentation is likely just
    something someone forgot to update

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jun 26, 2005
    #2
    1. Advertising

  3. Matthias Kaeppler

    Uwe Seimet Guest

    Matthias Kaeppler wrote:

    > I want to use assertions in my Java 5 code. The javac manpage says I
    > have to compile with `-source 1.4` to enable assert as a keyword, but
    > this also leads to java 5 features like enums not working anymore.


    The Java 5 compiler accepts assertions without any additional keyword.
    In other words, just don't use -source 1.4 and you will get all Java 5
    features including assertions.

    --
    -----------------------------------------------------------------------
    Dr. Uwe Seimet http://www.seimet.de
     
    Uwe Seimet, Jun 26, 2005
    #3
  4. Uwe Seimet wrote:
    > Matthias Kaeppler wrote:
    >
    >
    >>I want to use assertions in my Java 5 code. The javac manpage says I
    >>have to compile with `-source 1.4` to enable assert as a keyword, but
    >>this also leads to java 5 features like enums not working anymore.

    >
    >
    > The Java 5 compiler accepts assertions without any additional keyword.
    > In other words, just don't use -source 1.4 and you will get all Java 5
    > features including assertions.
    >


    That's strange, because this does NOT lead to a failed assertion:

    // Assert.java
    public class Assert {
    public static void main(String[] args) {
    Assert a = null;
    assert(a != null);
    }
    }

    this compiles and runs cleanly!

    Are you sure I don't have to set a debug flag or such?

    --
    Matthias Kaeppler
     
    Matthias Kaeppler, Jun 27, 2005
    #4
  5. Matthias Kaeppler

    Hemal Pandya Guest

    Matthias Kaeppler wrote:
    > Uwe Seimet wrote:
    > > Matthias Kaeppler wrote:
    > >

    [...]
    >
    > this compiles and runs cleanly!
    >
    > Are you sure I don't have to set a debug flag or such?


    Yes you do. Try 'java -?' and check out -ea/-da options. At lease in
    1.4 you need to enable assertaions at runtime.

    >
    > --
    > Matthias Kaeppler
     
    Hemal Pandya, Jun 27, 2005
    #5
  6. Matthias Kaeppler

    Roland Guest

    On 27-6-2005 10:17, Matthias Kaeppler wrote:
    > Uwe Seimet wrote:
    >
    >> Matthias Kaeppler wrote:
    >>
    >>
    >>> I want to use assertions in my Java 5 code. The javac manpage says I
    >>> have to compile with `-source 1.4` to enable assert as a keyword, but
    >>> this also leads to java 5 features like enums not working anymore.

    >>
    >>
    >>
    >> The Java 5 compiler accepts assertions without any additional keyword.
    >> In other words, just don't use -source 1.4 and you will get all Java 5
    >> features including assertions.
    >>

    >
    > That's strange, because this does NOT lead to a failed assertion:
    >
    > // Assert.java
    > public class Assert {
    > public static void main(String[] args) {
    > Assert a = null;
    > assert(a != null);
    > }
    > }
    >
    > this compiles and runs cleanly!
    >
    > Are you sure I don't have to set a debug flag or such?
    >

    Use the commandline option -enableassertions (or -ea for short):

    java -ea Assert

    See also
    <http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#standard>
    --
    Regards,

    Roland de Ruiter
    ` ___ ___
    `/__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Jun 27, 2005
    #6
  7. Matthias Kaeppler

    Tim Tyler Guest

    Roland <> wrote or quoted:

    > Use the commandline option -enableassertions (or -ea for short):
    >
    > java -ea Assert
    >
    > See also
    > <http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#standard>


    ....which appears to only work on newer VMs.

    If you compile the code for a new VM and try to run it on an old
    one, you don't just get no assertions, it crashes completely at runtime.

    Compile the code to target old VMs is not an option - since the
    assert keyword is not recognised.

    If you want to compile code with assertions that "correctly"
    asserts its conditions on older JVMs, is there anything that can
    be done?

    It seems the only approach is to write your own "customAssert(boolean)"
    method.

    I thought we were supposed to be getting *away* from doing that.

    Is there a compiler that can be configured to do something sensible
    in this case?
    --
    __________
    |im |yler http://timtyler.org/ Remove lock to reply.
     
    Tim Tyler, Jul 9, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Newby
    Replies:
    1
    Views:
    1,709
  2. Robert Brewer
    Replies:
    1
    Views:
    496
    bsmith
    Nov 7, 2004
  3. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,528
    Carl Banks
    Feb 23, 2005
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    923
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,692
    Richard Bos
    May 17, 2010
Loading...

Share This Page