Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

prefix v postfix increment : ++ X v X ++

 
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Khalid Mughal's Programmer's Guide to Certification in Section 3.1, he says that postfix operators associate left to right and that prefix operators associate right to left. Can anyone show me how that works ? I've tried many times and can't construct a situation where left v right associativty makes a difference with unary operators.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Its true that postfix operators are left associative, you can note that by the point "The postfix operator uses the current value of the expression first, and then adds 1 to j" i.e. if you have a statement g=i++;(consider g=1,i=1) System.out.println(g); then the value that is printed is 1 and not 2. This is because it has associativity from left to right and hence the value of 1 is assigned to g before incrementing it.
public class Inct
{
public static void main(String args[])
{
int g=1,i=1;
g=i++;
System.out.println(g);
}
}
try executing this code and you'll understand that left associativity is shown in assigning the value.
hope this helps you.
-gayathri
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no associativity involved in the statement :
g = i++;
Associativity is used only when there are operators of equal precedence which does not occur here. Here g receives the value of 1 when i is initialized to 1 because that's how the postfix operator is defined, not because of associativity.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!