Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Assertion Question

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question is from Master Exam. Why does the code prints "done" when line 17 resolves to false? Please explain your answer. Thanks a lot!


import java.lang.*;

public class AssertTest{
public static int y;
public static int foo(int x){
System.out.print("foo ");
return y=x;
}

public static int bar(int z){
System.out.print("bar ");
return y=z;
}
public static void main(String[] args){
int t=20;
assert t < 4:bar(7); //line 17
assert t > 1:foo(8);
System.out.println("done");
}
}

//The program print "done"

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you run this program with option -ea (enable assertions),
it throws AssertionError exception at line 17.
If you run it with disabled assertions (without -ea or -enableassertion), it prints 'Done'.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Tayitu Betule,

I am studying for the SCJP exam, and I am not an expert.

To reply to your post I think the code prints out "done" because the assertions are ignored,
there is nothing that tell us that the assertions are enabled, thus at run time these lines of code are not executed.

To enable assertions it needs to be specified with a flag in the command line:
java -enableassertions AssertTest
or with the short flag, which is equivalent
java -ea AssertTest

Regards,

Giorgio
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tayitu,

JavaRanch message posting has a feature called "Code" that will display your code in the following manner:


For the assertion problem, Giorgio and Ireneusz are right about the -ea flag. If you are using Eclipse IDE, then go to "Run Configurations..." and set "-ea" as the VM argument under the Arguments tab. Then run AssertTest as a Java application and you will get:

bar Exception in thread "main" java.lang.AssertionError: 7
at AssertTest.main(AssertTest.java:17)
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Please use code tags while posting the question, rather then quoting it.
It makes the question hard to read.

Hope this helps,
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to take Larry's IDE advice even farther (further?):

Don't use an IDE to study for the exam!!!

The entire Sun SCJP exam was coded with text editors, and tested from the command line! A lot of times IDEs do things 'behind the scenes', without telling you - this can often lead to misleading results for SCJP candidates.

hth,

Bert

p.s. fwiw, we didn't use an IDE to write the code for either K&B or HF Java - we used only text editors and the command line.
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Tayitu

Try this



javac AssertTest.java //for compiling your code

java -ea AssertTest //for running your code




definetly this may solve your problem
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys. It did work when running it with -ea.

 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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