File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes No increment at all?? 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 "No increment at all??" Watch "No increment at all??" New topic

No increment at all??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
What will happen when you attempt to compile and run the following code?
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
i = i++;
void fermin(int i){
1) Compile time error
2) Output of 2
3) Output of 1
4) Output of 0

Ans : 4)
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Sonir,
Before asking questions, you should use the search feature here at the ranch. This topic has been covered considerably and is old news. Here are for some links:
First Link
Second Link
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Read the Campfire Story Pass by Value Please

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Patrick Mugabe
Ranch Hand

Joined: Jan 08, 2002
Posts: 132
When you say i=i++;
The i on the left hand side is assigned the value of i on the right hand side BEFORE the increment.
However when you say i=++i; then i on the left hand side is assigned the value of i on the Right hand side AFTER the increment.
Thus if you go to your code where it says:
i = i++;
System.out.println(i); and change it to:
i = ++i;
System.out.println(i); then your output will be one(1).
I hope this will help. For further information read on post increment and pre increment.(You will normally find this section under for loops)
I agree. Here's the link:
subject: No increment at all??
It's not a secret anymore!