• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Assertions Enabled? compilation vs runtime

 
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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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.
 
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???
 
I found some pretty shells, some sea glass and this lovely tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic