+ 2

Explain how it is?

Need explanation of java code, how it works? https://code.sololearn.com/cdzcAKTDsREl/?ref=app

29th Jun 2018, 3:15 PM
Kumaresan L
Kumaresan L - avatar
2 Respuestas
+ 2
let's explain it line by line: class CourseCube{ // Simply create a class public static void main(String as[]) { int X=10; // initial value of x = 10 int Y=X++ + ++X; // post increment of x + pre increment of x. x changes value from 10 to 12 as : when x++ is written x gets incremented for next time it's called and then when we write ++x x gets incremented because of previous increment and once again due to post increment so x becomes 10 + 1 + 1 = 12. Furthermore y will be 10 + 12 = 22 System.out.println("X = "+X); // prints the value of x i.e. 12 System.out.println("Y = "+Y); // prints value of y i.e. 22 }
29th Jun 2018, 3:55 PM
RZK 022
RZK 022 - avatar
+ 2
Hi kumaresan , Question is: int X=10; int Y=X++ + ++X; Result is: X=12 Y=22 Why??? 🔼According to: prefix and postfix in java ( https://www.sololearn.com/learn/Java/2141/ ) : 1⃣ At first step: X++ The value of "X" (x=10) assigned to "Y" and now "Y" is 10, Then, the value of "X" incremented to 11 and now, "X" is 11. 2⃣ Second step: ++X At this point, at first, the value of "X" incremented to 12 and Final value of "X" is 12 and then 12 added to "Y" (Y=10),so: X=12 Y=10+12=22 Ok? Goodluck 😉 🙋
29th Jun 2018, 4:05 PM
Root
Root - avatar