aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""assert i%2==0 : i--;"question required" Watch ""assert i%2==0 : i--;"question required" New topic
Author

"assert i%2==0 : i--;"question required

VINCE CARTER
Greenhorn

Joined: Sep 26, 2004
Posts: 13
What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled.)

1.public class AssertTest
2.{
3.public static void main(String args[])
4.{
5.for(int i=0;i <10;i++)
6.{
7.try
8.{
9.assert i%2==0 : i--;
10.System.out.println("Even number : " + i);
11.}
12.catch(AssertionError ae)
13.{
14.System.out.println("Odd number : " + ++i);
15.}
16.}
17.}
18.}
Answer:It will print odd and even numbers from 0 to 9 correctly (0 even and 1 odd).
Why ?
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200

1. public class AssertTest
2. {
3. public static void main(String args[])
4. {
5. for(int i=0;i <10;i++)
6. {
7. try
8. {
9. assert i%2==0 : i--;
10. System.out.println("Even number : " + i);
11. }
12. catch(AssertionError ae)
13. {
14. System.out.println("Odd number : " + ++i);
15. }
16. }
17. }
18. }


Iteration 1:
i = 0
line 9: 0%2 == 0 - true - no assertion error thrown.

Iteration 2:
i=1
line 9: 1%2 == 0 - FALSE - assertion error thrown


SCJP 1.4 SCWCD 1.4
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
I am adding something to the last reply........

Iteration 1:
i = 0
line 9: 0%2 == 0 - true - no assertion error thrown.

Iteration 2:
i=1
line 9: 1%2 == 0 - FALSE - assertion error thrown
line 12: Assertion Error has been caught and ++i has been printed...means i=1.

Ater the normal loop operation starts again......
Iteration 3:
i=2
line 9: 2%2 == 0 - true - no assertion error thrown.

Iteration 4:
i=3
line 12: Assertion Error has been caught and ++i has been printed...means i=3.

This way you can find the other iterations also..

very nice question.....

Kaps


Kapil Munjal
SCJP 1.4, SCWCD 1.4
amitkumar kamboj
Greenhorn

Joined: Oct 06, 2004
Posts: 5
hi there

actually logically your program is wrong
because the class Error and its subclass in java is not mean to catch them they represent the error in your code

and it is poor programming practice to catch the Runtime Exceptions and Error's

you should only catch the check Exception i.e. include IOException
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Yes, Amit you are right that its not a good programming practice. But if you study Assertions, you will find that we can catch AssertionError if thrown in the code. So, if Sun Exam developer thinks that the examinee should know this fact then they are going to test your knowledge.

So, you should atleast know this concept.

Kaps
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
poor programming practice to catch the Runtime Exceptions and Error's


Agreed but there are exceptions to this rule. The problem is NumberFormatException is a RuntimeException, and this exception is often thrown when parsing user input (Integer.parseInt(args[0]) for example). So a RuntimeException is not ALWAYS caused by a programming error as such. If you do not catch this exception, and display a meaningful error message, the clueless user is going to be confused by a possibly complex stack trace.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Tom Screen
Greenhorn

Joined: Oct 09, 2004
Posts: 11
Hi,

I decided it was time for me to register just to add a reply to this thread! I was wondering where you came across this piece of code. If you are studying for your SCJP exam then you should really be aware that this code flaunts the 'good practice' guidelines of assertions - that is they should not produce a side effect. In this case your assertion will decrement the i variable if the condition of the assertion evaluates to false.

Hope this helps too!


Tom Screen<br />SCJP 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Tom said:
I was wondering where you came across this piece of code


Unfortunately, VINCE CARTER (and his namesake PETER CARTER) has not responded to our requests to quote the source of his code when he posts

I'm pondering on the option to close his questions if he continues.

Anyway, welcome to Javaranch Tom! Keep on postin'
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by amitkumar kamboj:
it is poor programming practice to catch the Runtime Exceptions


Why???


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
VINCE CARTER
Greenhorn

Joined: Sep 26, 2004
Posts: 13
Even number : 0
Odd number : 1
Even number : 2
Odd number : 3
Even number : 4
Odd number : 5
Even number : 6
Odd number : 7
Even number : 8
Odd number : 9

Thanks!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "assert i%2==0 : i--;"question required