• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Having trouble understanding assertions:See this code

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following piece of code:
public class assertUse{
public void m1(int value)
{
assert 0<=value;
System.out.println("OK");
}
public static void main(String[] args){
assertUse aU=new assertUse();
System.out.println("assertUse.m1(1):");
aU.m1(1);
System.out.println("assertUse.m1(-1):");
aU.m1(-1);
}
}
Shouldn't it throw an erro when -1 is passed to m1(). When I compile it the program gives the following output:
assertUse.m1(1):
OK
assertUse.m1(-1):
OK
I am not sure why it is not throwing an asertioerror in case of -1. Can somebody help?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you enable assertions when you ran the code?
java -ea assertUse
By default they are disabled.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the JavaRanch newsletter. I think it was in September that we had two articles on using assertions.
 
Don't touch me. And dont' touch this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic