File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertions Enabled? compilation vs runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions Enabled? compilation vs runtime" Watch "Assertions Enabled? compilation vs runtime" New topic

Assertions Enabled? compilation vs runtime

Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
Ok bit confused here, Practice Exam 3 question 50 of Whizlabs exam simulator

the below code is compiled with assertions enabled

public class AssertDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub

boolean assertEnabled = false;
//assert (assertEnabled==true);
assert assertEnabled = true;

System.out.println("Assertions are"+(assertEnabled?"enabled":"disabled"));

yet it supposedly is run without aserstions so somehow the assert assertEnabled = true;(assignment) doesn't get run...

so my question is, if you compile something with enabled assertions and run it without enabled assertions it ignores the assertions?

but if you compile it without assertions and run it without assertions enabled it appears that assertions are enabled? doesn't make sense? can anyone concur? or even explain?
marc weber

Joined: Aug 31, 2004
Posts: 11343

When assertions were introduced in 1.4, you needed to compile with the -ea flag. With version 1.5, this became the default, so you no longer need to compile with -ea. However, you still need to use the flag when running.

When I run this code with -ea, the output shows assertions are enabled. Without the -ea flag, the output shows assertions are disabled.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
yusuf Kaplan
Ranch Hand

Joined: Mar 11, 2011
Posts: 59

another question regarding assertions:
Within the exam, can we assum that assertions are enabled by default???
I agree. Here's the link:
subject: Assertions Enabled? compilation vs runtime
It's not a secret anymore!