This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes fyi: pre/post increment with exception thrown on the line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "fyi: pre/post increment with exception thrown on the line" Watch "fyi: pre/post increment with exception thrown on the line" New topic
Author

fyi: pre/post increment with exception thrown on the line

Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
i just ran in to a situation where i am converting a string to a number and then inserting the number in to an array. i thought this code sample might be helpful for newbies - i sure learned something.

anything pre/post increment/decrement on the line before the exception is thrown is on the stack and will be executed (k++,++n1), but anything after the exception is not (++n2). also try n1++, etc, just for fun. hope this helps someone avoid logic errors :-)


use the [CODE] tags - it makes it much easier for people to help you.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Of course, simply not trying to do too many things at once and on a single line would greatly improve the readability of the code as well as reduce the likelihood of any logic errors.
So, rather than trying to do something like,
array[ i++ ] = ++j ;
just break it apart and say what you mean, one thing at a time
++j ;
array[ i ] = j ;
++i ;


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148

array[ i++ ] = ++j ;
just break it apart and say what you mean, one thing at a time
++j ;
array[ i ] = j ;
++i ;

If I'm not mistaken the bytecode(if not then the assembly) generated for both of these is going to be the same, so it doesn't improve performance to do it the "short" way.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: fyi: pre/post increment with exception thrown on the line
 
Similar Threads
please explain the output
Explain this FFT Radix 2 DIT java code line by line
and yet another recursive problem
trouble with operator
unreachable code in 'finally'