• 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 Problem

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ObjectTest {

public static void assertVoid(int x) {
System.out.print("assert 1 ");
}

public static int assertInt(int x) {
System.out.print("assert 2 ");
return x;
}

public static void main(String [] args ) {
int t = 0;
assert t > 1 : assertInt(1);//line 1
System.out.println("complete");
}
}

1 :In line1 t>1 is false:

So will it execute assertInt(1)???

2:If in line1 t>1 is true:

So will it execute assertInt(1)???


How will I get output as "assert 2 complete"?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will be able to answer these questions after you have read the standard documentation Programing with Assertions
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running your class with the following

java -ea ObjectTest

will cause an AssertionError

You have to run your class with Assertions enabled.
 
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic