Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Marcus Green exam #3 question 54

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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 answer is 4, i.e., i=0. But I still cannot understand why
{int i=0; i=i++;} will be 0. Can somebody explain why? Thank you.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karlon
Check out this thread for a good explaination http://www.javaranch.com/ubb/Forum24/HTML/009768.html

 
Karlon Wu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Dave. I'll get the exam after 3 days. It really helps me.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you should understand the difference between assignments and uninar incremental operator.
when you put
i=0;
i=i+10;
it assignes value to i a value 0+10=10
where as
i=i++
it assignes value
i=i and then increments,
ie i=0
when you pass as a parameter it passes only the copy of the value, hence the original value never changes. that is why you get 0 as output in this code.

Originally posted by Dave Vick:
Karlon
Check out this thread for a good explaination http://www.javaranch.com/ubb/Forum24/HTML/009768.html


 
easwaran ram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
i=i+10;
i+=++i;
inc.fermin(i);
System.out.println(i);
i+=++i;
System.out.println(i);
}
void fermin(int i){
i+=100;
}
}

Originally posted by Dave Vick:
Karlon
Check out this thread for a good explaination http://www.javaranch.com/ubb/Forum24/HTML/009768.html


 
get schwifty. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic