aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes increment and decrement concept in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "increment and decrement concept in java" Watch "increment and decrement concept in java" New topic
Author

increment and decrement concept in java

bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

For your answer read this :The Unary Operators

-hth


OCA7
bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32

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

Joined: Aug 06, 2012
Posts: 333

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

Joined: Apr 08, 2012
Posts: 32


i have seen that prepostdemo class example ,its been incremented with both ++i and i++ ,but with no explanation
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

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

Joined: Apr 08, 2012
Posts: 32

i will do it thanks
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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

Joined: Apr 08, 2012
Posts: 32


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

Joined: Feb 03, 2011
Posts: 247
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

Joined: Apr 08, 2012
Posts: 32

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

Joined: Feb 03, 2011
Posts: 247
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.
 
Don't get me started about those stupid light bulbs.
 
subject: increment and decrement concept in java
 
Similar Threads
Mock Exam doubt increment expresion
Why the result is 1,0
What is the follow of this code?
Prime Factor
Why is this printing 1,0 instead of 1,1