• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

question from Marcus Green's Mock Exam 3

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 54)
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;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
1) Compile time error
2) Output of 2
3) Output of 1
4) Output of 0
the given answer is 4. I'm confused. I know that if the statement is System.out.println(i++); then the output will be the current i, say 0. But is there any difference between the statement System.out.println(i++); &
i = i++;
System.out.println(i); ?
Can anybody give me a clear expanation?Thank you
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an excellent question. There are two things being tested here, both pass by value and operators. The value passed to fermin goes nowhere and disappears. The value is passed to fermin which increments its own i variable local to the method and does nothing to the i in Inc. The second thing is that i = i++; is really
i = 0(++);
Postfix operators happen after the expression i++(1) is never added to i. If you change the code to ++i, then you get a result of 1 because the prefix operator is added first!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an excellent explanation on how the + operator works, please check out:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
Ray Chang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I know the code below will give an output 0.
int i=0;
System.out.println(i++);
But I guess it's different when the code is like below:
int i=0;
i=i++;
System.out.println(i);
In my understanding, immediately after the statement i=i++; is excuted, i is 0. But then i is incremented after that and is 1. then the System.out.println(i); excuted and the output is 1. If I was wrong, when will i be incremented at this kind of statements?Thanks again
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ray Chang:
Thank you. I know the code below will give an output 0.
int i=0;
System.out.println(i++);
But I guess it's different when the code is like below:
int i=0;
i=i++;
System.out.println(i);
In my understanding, immediately after the statement i=i++; is excuted, i is 0. But then i is incremented after that and is 1. then the System.out.println(i); excuted and the output is 1. If I was wrong, when will i be incremented at this kind of statements?Thanks again


Actually, there is no difference:

What happens in
int j=0;
j = j++;
is that the assignment goes last. The return value of j++ is 0, but it is not assigned yet. j gets incremented to 1, then the assignment kicks in, assigning the return value 0 to j.
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anthony Villanueva:
then the assignment kicks in, assigning the return value 0 to j.


Hi Anthony,
I do understand that i gets 0 knowing that i = 0(1) but i was just wondering about the lasted value of i which is 1. I compiled the program myself and the value 1 of i seems to be flashed.
i really appreciated more insights on this.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this section from the java language specification may help reveal what happens:

PostIncrementExpression:
PostfixExpression ++

A postfix expression followed by a ++ operator is a postfix increment expression.
The result of the postfix increment expression is not a variable, but a value.
At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes
abruptly for the same reason and no incrementation occurs.
The value of the postfix increment expression is the value of the variable before the new value is stored.



therefore, I believe that since i++ doesn't complete until after the assigment expression ( "i = i++; " completes, you get an abnormal termination to the increment operation.
just a guess ....
reply
    Bookmark Topic Watch Topic
  • New Topic