File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assertions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "assertions" Watch "assertions" New topic
Author

assertions

vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
Given code as

class A {
void m1(int i) {
int j = i % 3;
switch (j) {
case 0: System.out.print("0"); break;
case 1: System.out.print("1"); break;
default:
assert j == 2;
System.out.print(j);
}}
public static void main (String[] args) {
A a = new A();
for (int i=5; i >= -1; i--) {a.m1(i);}
}}

output is
With assertions enabled it prints 210210 followed by an AssertionError message.
With assertions disabled it prints 210210-1

i am bit confused...i guess the output is 210000 as j will never be 2 after the first time it executed...please help me
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by vandu matcha:
...i guess the output is 210000 as j will never be 2 after the first time it executed...

% is Java's remainder operator, which yields "the remainder of its operands from an implied division." So 2%3 is 2, because 2 divided by 3 is 0 with a remainder of 2.

Does this answer your question? Or were you thinking that the assertion statement might affect this?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
vandu matcha
Ranch Hand

Joined: Dec 28, 2005
Posts: 57
thank u marc
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: assertions
 
Similar Threads
assertions doubt
Assertion
Mock Exam - Exception handling
Assertion querry
Assertion