• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assertions Enabled? compilation vs runtime

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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