| Author |
What is the output
|
Sneha Kapoor
Ranch Hand
Joined: Jun 08, 2009
Posts: 49
|
|
why the i output as 0..when it will print as 1
|
 |
Joshua Ebarvia
Ranch Hand
Joined: Sep 25, 2007
Posts: 70
|
|
Hi, I'm not an expert here but I'll try to answer your question.
#
# 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);
# System.out.println(i);
# }
# void fermin(int i){
# i++;
# }
# }
When this is compiled and run, the output is
It is just as expected, though you have passed variable i from the main method to method fermin() and used the i++, the ++ operator just took effect inside the method fermin. After the method is done, the effect doesn't change your variable i in the main method. when you reach the code i = i++, the variable i still has the value of 0 since i++ leads to 0. Then when you print the i using the println, effect of the ++ was not considered since you have assigned it to i itself.
|
Lovin' java
|
 |
Venkata Kumar
Ranch Hand
Joined: Apr 16, 2008
Posts: 110
|
|
Hi Sneha,
In java the parameters to method arguments are passed by value
The parameter i is local to method fermin and incrementing this value won't affect the variable i which is declared in main program.
Please see the kink http://www.coderanch.com/t/408774/Java-General-beginner/java/could-anyone-explain-this-please
|
SCJP 5.0, SCWCD 5, preparing for SCDJWS
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Please when you post a question, then remember to Quote Your Sources...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Sneha Kapoor
Ranch Hand
Joined: Jun 08, 2009
Posts: 49
|
|
the variable i still has the value of 0 since i++ leads to 0.
how come i = i++ leads to 0
|
 |
Joshua Ebarvia
Ranch Hand
Joined: Sep 25, 2007
Posts: 70
|
|
int i =0; // initial value of i in main()
inc.fermin(i); //passes i to fermin() but doesn't have an effect to i in main()
i = i++; // since i = 0 and you have the post increment operator ++, it is like saying increment i on the next operation, but not this time
System.out.println(i) // still leads to zero because of i = i++
|
 |
Sneha Kapoor
Ranch Hand
Joined: Jun 08, 2009
Posts: 49
|
|
thanks got it so if i do below things so i will get i = 1
i=i+1;
System.out.println(i);
|
 |
Joshua Ebarvia
Ranch Hand
Joined: Sep 25, 2007
Posts: 70
|
|
Sneha Kapoor wrote:thanks got it so if i do below things so i will get i = 1
i=i+1;
System.out.println(i);
yes...l. you can also try
|
 |
Bob Wheeler
Ranch Hand
Joined: Apr 24, 2009
Posts: 317
|
|
Sneha Kapoor wrote:thanks got it so if i do below things so i will get i = 1
i=i+1;
System.out.println(i);
Or
cheers
Bob
|
SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
|
 |
aabir sanyal
Greenhorn
Joined: Jun 22, 2009
Posts: 21
|
|
I am unable to get this, then what is the effect of i=i++;
Really confusing
|
 |
Bob Wheeler
Ranch Hand
Joined: Apr 24, 2009
Posts: 317
|
|
aabir sanyal wrote:I am unable to get this, then what is the effect of i=i++;
Really confusing
check this out
|
 |
aabir sanyal
Greenhorn
Joined: Jun 22, 2009
Posts: 21
|
|
Bob Wheeler wrote:
aabir sanyal wrote:I am unable to get this, then what is the effect of i=i++;
Really confusing
check this out
Thanks Bob
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: What is the output
|
|
|