This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions Enabled? compilation vs runtime

 
Robert Elbourn
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
yusuf Kaplan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

another question regarding assertions:
Within the exam, can we assum that assertions are enabled by default???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic