assert

M

Matthias Kaeppler

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.
 
S

Stefan Schulz

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
 
U

Uwe Seimet

Matthias said:
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.
 
M

Matthias Kaeppler

Uwe said:
Matthias Kaeppler wrote:




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?
 
H

Hemal Pandya

Matthias said:
Uwe said:
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.
 
R

Roland

Uwe said:
Matthias Kaeppler wrote:





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_/ /__/
/ \ /_/ / \
 
T

Tim Tyler

Roland said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top