How come the value of y is 70 i dint get it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How come the value of y is 70 i dint get it?

public class Program { public static void main(String[] args) { int x = 34; int y = x++ + ++x; System.out.println(x); System.out.println(y); } } O/p X 36 y 70

29th Aug 2017, 4:35 PM
Neha Parmar
Neha Parmar - avatar
5 Answers
+ 4
here value of y by inclement becomes x++ = 34 ++x = 36 x++ + ++x = 34+36 = 70 . And in this way it becomes 70
29th Aug 2017, 4:39 PM
RZK 022
RZK 022 - avatar
+ 4
no x++ becomes 35 when it is incremented one and called after incrementing it first . Example :- x = 10; System.out.println(x++); System.out.println(x); The output will be ; 10 11 While in the case of prefix increment . It will be like ;- x = 10 ; System.out.println(++x); Here it will be 11;
29th Aug 2017, 4:49 PM
RZK 022
RZK 022 - avatar
+ 2
Thank you:)
29th Aug 2017, 4:51 PM
Neha Parmar
Neha Parmar - avatar
- 1
But while learning it's written that x++ becomes 35
29th Aug 2017, 4:42 PM
Neha Parmar
Neha Parmar - avatar
- 1
public class Program { public static void main(String[] args) { int x = 34; int y = x++; System.out.println(y); System.out.println(x); } } y=34 x=35 Check this op
29th Aug 2017, 4:45 PM
Neha Parmar
Neha Parmar - avatar