|
a[i++]++ a[i++]=a[i++] + 1; As stated, there's no decrement above, so is this exactly right? For starters, the index variable "i" is incremented twice in the second statement, once in the first. So, regardless of which contents of "a" get changed, there's one difference right off the bat. Some of this kind of thing can be done with experiments easily enough nowadays. Try this out: public class test { public static final void main (String args[]) { int a[] = new int[4]; int i; for (i=0; i<4; i++) a[i]=i; i=0; a[i++]++; System.out.println("a[0] is : "+a[0]+ " a[1] is : "+a[1]+ " a[2] is : "+a[2]+ " a[3] is : "+a[3]+ " and i is :" + i); for (i=0; i<4; i++) a[i]=i; i=0; a[i++]= a[i++]+1; System.out.println("a[0] is : "+a[0]+ " a[1] is : "+a[1]+ " a[2] is : "+a[2]+ " a[3] is : "+a[3]+ " and i is :" + i); } } For output, I get (IBM Windows 95 1.1.7 JDK): a[0] is : 1 a[1] is : 1 a[2] is : 2 a[3] is : 3 and i is :1 a[0] is : 2 a[1] is : 1 a[2] is : 2 a[3] is : 3 and i is :2 If you want to figure out more, fire up a decent IDE against this and watch how the variables change as you let your debugger step through the program. I wouldn't be 100% sure about this little example being portable, by the way. (It's the a[i++]= a[i++] + 1 that worries me in terms of which locations in "a" get fetched, all dependent on when the increments of i take effect, especially the one on the right hand side of the assignment). In C, I don't believe that second example would be portable. Two post increments of the same variable on the same statement are allowed to optimized any number of ways as I recall the standard. Perhaps Java restricts this so it is portable, but I'd have to look it up to be sure; partly this is a "javac" question and partly a question of what JIT and other Java compilers (e.g. AS/400 Transformer) are allowed to do. This is the sort of thing that keeps language gear heads up late at night, but real programmers quickly learn to avoid that kind of thing to start with. But, it's a nice teaching example. Larry W. Loen - Senior Java and AS/400 Performance Analyst Dept HP4, Rochester MN -- We must do a few things well, rather than everything late. 8-553-3535 (507) 253-3535 +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.