Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions

 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a book who tells if i do an assertion and the assertion is NOT true, the program stop executing and an error (AssertionError) is throw ..

is this correct ?

because when i tryed in NetBeans ..

int a = 8;
assert(a==2); NOTHING HAPPEN

how this work ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Podonga,
Do you have assertions enabled? If assertions are not enabled, they don't do anything.
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think they are not enabled

do you know how to enable in netbeans ? i need to practice ... and compiling using javac -ea every time is tedious !
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't compile using the -ea flag, you run it using the -ea flag. The assertions are compiled into the code regardless of the flags.

Isn't there a way to tell NetBeans to run your code with assertions enabled?

As a side node, you do know that your code should work just the same with and without assertions enabled? You can't assume that somebody else will also have assertions enabled when he/she runs your code.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to our IDEs forum where people know more about NetBeans.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you forgot the actual moving part
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
I think you forgot the actual moving part

Sorry. The phone rang and then I forgot about it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic