Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

increment and decrement concept in java

 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your answer read this :The Unary Operators

-hth
 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

doesnt able to understand


in code after printing i,it should be increment to 1 because i++ is post increment operator first increment then used ,then j should be 1


 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bhavneet kaur wrote:
doesnt able to understand


in code after printing i,it should be increment to 1 because i++ is post increment operator first increment then used ,then j should be 1




Don't expect always ready made answers, did you read that document ? if you have read that document then at last of the page same problem is explained but at least show some efforts to read a document.
 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i have seen that prepostdemo class example ,its been incremented with both ++i and i++ ,but with no explanation
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:



It is explained well, you can't see ?

Even without explanation you can see output differences here.
 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i will do it thanks
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is really good. To clear your doubts, for the time being just forget about your program and try to understand below concept of pre and post increment operators.

Consider below program


The comments are the answer. If you understand this concept, you will definitely understand your program.

Some pages related to above concepts, so read them
here and here and here


 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


ok, i got your point have read three articles
here i=0 ,because its post increment and i will increment after using system.out.println
when j=j+i ,now i should have increment to 1 but j=0 ???
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bhavneet kaur wrote:
here i=0 ,because its post increment and i will increment after using system.out.println
when j=j+i ,now i should have increment to 1 but j=0 ???


No, i think you haven't read the articles properly.

1) When you have this


i will remain 0 only. No matter even if you have any statement after this. So you said because its post increment and i will increment after using system.out.println is wrong. variable i will be 0

Therefore, j = j + i will result in 0 and not 1 since both "j" and "i" are 0.

Secondly, in the program which you posted now, there is no use of the static method f1, since you haven't used it.

Any more doubts, feel free to ask.
 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thank you so much ?
as i am preparing for certification and i havnt done collection in detail can you recommend me any website for collection?
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the best one is the official documentation i.e. here.

Some books are also there like this and of-course there's Google.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic