• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do the pre and post operators matter outside of assignments?

 
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:
The increment operators ++i and i++ or likewise the decrement operators, have identical effects except in assignments
and that is the only place they actually distinguish themselves (or find their use).
Is my understanding correct?

 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
They do both have the effect of incrementing the value of the variable "i". The difference is when you look at the result of the incrementation. For one of the operators the result is the value of "i" before incrementing, and for the other the result is the value of "i" after incrementing. No doubt you know which.

So yes, the only concern is when you use the result of the operator. You mentioned assignments, which is true, but be careful, it's not only = which causes an assignment. Using a value as a method parameter also assigns that value to something, for example. Example: Math.max(++i, 42). There may be other situations.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:You mentioned assignments, which is true, but be careful, it's not only = which causes an assignment. Using a value as a method parameter also assigns that value to something,


Perhaps they should be referred to as assignment operators!
 
Saloon Keeper
Posts: 15608
366
  • Number of slices to send:
    Optional 'thank-you' note:
i++ and ++i are expressions.

The difference between them will become evident whenever you use them as part of a larger expression. Not just explicit assignments or assignment to method arguments, but anywhere an expression is allowed.

Does something different than


Retrieves a different element than


There are many similar examples.
 
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:The increment operators . . . have identical effects . . . Is my understanding correct?

Afraid not. Since you are sitting a cert. exam, you will need to know the differences because cert. exams use i++ and ++i, etc., in larger expressions where the differences are there to confuse you. There are only a few instances where use of those operators as part of a larger expression doesn't count as bad programming. Or good programming for cert. exams!You will find those examples in Effective Java by Joshua Bloch, but I haven't got the time now to find the page number (sorry).
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Stephan van Hulst wrote:[tt] whenever you use them as part of a larger expression.


Isn't it (a special case of) an assignment to a temporary variable in the expression?
@moderators I don't know how this thread ended up in the JDBC forum - can you please move it? thanks.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:. . . Isn't it a special case of an assignment to a temporary variable in the expression?

No. That is part of the definition of the ++ and -- operators. It's all in the JLS.

. . . don't know how this thread ended up in the JDBC forum . . .

Shall move you. It will stay in the JDBC forum as a link for one week.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:No. That is part of the definition of the ++ and -- operators. It's all in the JLS.



from Stefan's example


is really



an assignment to a temporary
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:. . . is equivalent to . . .

Not 100% sure, but I think you will find that is incorrect. The JLS sometimes tells us that a temporary variable is used, e.g. in the for‑each loop and try‑with‑resources, but as far as I can remember, there are no temporary variables for increment and decrement. Try javap −c and see what happens.
 
Stephan van Hulst
Saloon Keeper
Posts: 15608
366
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:from Stefan's example [...] is really [...] an assignment to a temporary


No, not really.

Every expression evaluates to some value. For i++, that value is the current value of i before incrementing it, and for ++i it is the value of i after incrementing it. There are no temporary variables involved.

Your logic comes down to saying that
is really

That just isn't the case.
 
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
Here's some sample code to study, with no assignment statements.  Can you explain the results?
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:Perhaps they should be referred to as assignment operators!


Well, they are a lot like assignment operators.  "++i" is effectively the same as "(i += 1)" - except I added parenthesis because ++ is much higher in precedence, so if you use it within an expression it may make a difference.  It would be confusing to start calling ++ an assignment operator, and then discuss order of precedence, because those rules use the existing definition of "assignment operator".  Better to just use the terms as they have already been defined.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:. . . Perhaps they should be referred to as assignment operators!

As Mike has said, no they shouldn't.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:Here's some sample code to study, with no assignment statements.  Can you explain the results?


nice question! thanks...



i 1
i++ * i++  2
i 3

j 1
j++ * ++j  3
j 3

k 1
++k * ++k  6
k 3


 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

This morning, I wrote:. . . I haven't got the time now to find the page number (sorry).

In the 2nd edition, pages 25‑26. In the 3rd edition, pages 27‑28.

Mike Simmons wrote:. . .  Can you explain the results?

Well, can you?
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Stephan van Hulst wrote:i++ and ++i are expressions. . . . .

Aren't they also called expression statements? Nearly, but not quite←JLS link.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
If they're used as statements, where the entire statement is "i++;" for example, they are expression statements.  If they're used as subexpressions in a larger expression, then they are not.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
For Anil: so do you agree there is a difference between pre and post operators, outside of any assignment?
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:For Anil: so do you agree there is a difference between pre and post operators, outside of any assignment?


For the same kind of operator, there is no difference between pre and post operations - outside of an assignment.
 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:For the same kind of operator, there is no difference between pre and post operations - outside of an assignment.



You seem to be asserting that "6 + (++a)" is not different than "6 + (a++)". There's no assignment here.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:

Anil Philip wrote:For the same kind of operator, there is no difference between pre and post operations - outside of an assignment.



You seem to be asserting that "6 + (++a)" is not different than "6 + (a++)". There's no assignment here.



I meant that

++i;
and
i++;

result in the same value.
 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:I meant that

++i;
and
i++;

result in the same value.


That's an unambiguous statement, then. Which is a false statement.

It's true that the value of the variable i is the same after either of those operations, but you made a statement about the values of those expressions. Not the same.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
So Anil, between

and

Is there any difference in the result?  Are there any assignments in those statements?
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:but you made a statement about the values of those expressions. Not the same.


I did not say expressions, I said operators.
 
Greenhorn
Posts: 3
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Your understanding is mostly correct, but there's a bit more to it when considering the broader implications of pre and post increment/decrement operators (i.e., ++i/i++ and --i/i--) in programming.

The primary difference between these operators is not just in assignments, but in how they behave in expressions and how the value is used:

Pre-Increment/Decrement (++i or --i):
The variable i is incremented or decremented before it is used in the rest of the expression. This means the updated value of i is available immediately for any further computation within the same expression.
Post-Increment/Decrement (i++ or i--):
The variable i is used in its current state first, and only then is it incremented or decremented. This means the expression evaluates using the original value, and the change takes effect after the expression's value is computed.

Here are some scenarios beyond simple assignments where the distinction matters:

In a Function Call:
When passing a variable as an argument to a function, f(++i) will pass the incremented value, whereas f(i++) will pass the original value and increment i afterward.
In Loops:
Although often interchangeable in loops like for, the choice between pre and post can affect readability or performance minutely in some languages due to the timing of the increment/decrement.
In Complex Expressions: I
n expressions that mix several operations, choosing pre or post increment can affect the order of operations and thus the final result.
In Concurrent or Multi-threaded Code:
The choice between pre and post can have implications in terms of when a variable is updated, potentially affecting synchronization or the visibility of the variable's state across different threads.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:

Paul Clapham wrote:but you made a statement about the values of those expressions. Not the same.


I did not say expressions, I said operators.



correction: operations
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Raphaelle Senger wrote:The primary difference between these operators is not just in assignments, but in how they behave in expressions and how the value is used:


thank you.
 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:I did not say expressions, I said operators.


Let's review where you said "operators" and what you said, then. You'll have to copy and paste what you said, though, because you've said a lot of things including those two words.


 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:

Raphaelle Senger wrote:The primary difference between these operators is not just in assignments, but in how they behave in expressions and how the value is used:


thank you.


Why thank you?  He's right, of course.  But you're telling us that there is no difference, and he's telling you there's a difference.  Did you suddenly understand?  Are you changing your answer?
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:So Anil, between

and

Is there any difference in the result?  Are there any assignments in those statements?



There could be a temporary variable(s) involved and assignments to those variables happening, but I do not know enough.
But I do understand those expressions.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:

Mike Simmons wrote:For Anil: so do you agree there is a difference between pre and post operators, outside of any assignment?


For the same kind of operator, there is no difference between pre and post operations - outside of an assignment.


@Paul here it is.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:
Why thank you?  He's right, of course.  But you're telling us that there is no difference, and he's telling you there's a difference.  Did you suddenly understand?  Are you changing your answer?


No, I was just thanking him because this is his first post.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
I don't usually quote the whole post, but it seems appropriate here for maximum clarity:

Anil Philip wrote:

Mike Simmons wrote:So Anil, between

and

Is there any difference in the result?  Are there any assignments in those statements?



There could be a temporary variable(s) involved and assignments to those variables happening, but I do not know enough.
But I do understand those expressions.


Okaaayy.  

But again, is there any difference in the result of those two statements?

And, would you agree that these statements are "outside of any assignment", to use the phrase you used earlier?
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
[This second post is a mispost, please delete - I was trying to update the preceding post, and added a second post instead.]
 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
I don't wish to be not nice, but this thread is, let's say, not useful. It's true that the expressions ++x and x++ have different values, given some variable x. And many students have had difficulty with that. But really that's all you need to know. Those two expressions have different values, and the rule for what differs is fairly simple. So when you use those values in subsequent code, you have to understand the difference.

However there is no point in expanding upon the idea of "use". You might use those values by assigning them to a variable, or by using them as a method parameter, or by doing arithmetic with them, or in some other way I haven't thought of. But it makes no difference at all, the values of those expressions are the same no matter what. So going on about assignments, for example, is not useful.

I find it's easier to look for simple explanations for bits of computer languages. Writers of computer languages generally use simple concepts, unlike writers of fiction. So going on about nonexistent complications is not useful.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
The point I was making is: Anil, you've been shown an example that (a) does not involve any assignments, but (b) clearly there is a difference in output, between the pre-increment and post-increment versions of the code.  So how can you believe that these are equivalent, outside of an assignment operator?

I suspect that one of these statements we're making means something different to me than it does to you.  So I was trying to unearth where the disconnect is, between my understanding and yours.

If we're going to start imagining invisible assignments in random locations, well, I guess that would make "outside of any assignment" kind of meaningless.  It's probably best if we limit the discussion to things that are actually in the code that we discuss.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:. . . There could be a temporary variable(s) . . .

We have already established that there most probably isn't such a temporary variable.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:

Anil Philip wrote:. . . There could be a temporary variable(s) . . .

We have already established that there most probably isn't such a temporary variable.


I meant that when the compiler translates it into assembler (or interprets it), then it likely might use them.
So there is an assignment happening at the lower levels.
But at the higher levels, it looks like one step - copy the present value and increment afterwards.
 
Paul Clapham
Marshal
Posts: 28258
95
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
I think that your technique of trying to imagine how the Java runtime is actually implementing "x++" is getting in your way. What it does is to increment x and then return the original value of x as the result of the expression. There's no need to imagine the machinations under the covers as it does that, you only need to remember that "x++" returns the original value of x. That's all. When you start imagining hidden variables and assignments you just add extra conceptual load to your understanding which is unnecessary.

For another example consider the expression "(3 + 6) * (2 + 3)". To compute this, the runtime computes 3 + 6, then it computes 2 + 3, then it multiplies the results of those two calculations. No doubt it has to store those two calculated values somewhere before it multiplies them. But there's no need to imagine hidden variables and assignments to describe where they are stored -- because it's totally unnecessary to care how that works. I'm sure you never gave that example a second thought, though. Likewise there's no need to obsess over how "x++" works.

(As I understand it, those temporary results are stored in a stack in real life, and I expect "x++" results in something being stored in that stack too, but you don't need to care.)
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:. . . when the compiler translates it into assembler . . .

It doesn't use assembler, but Java bytecode. That is similar to assembler. Have you looked at the bytecode?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |