+ 1

What is the conclusion of this code?

class A{ Public static void main(String [] array){ int val1=1; int val2=1; int result1=val1+++val1; int result2=++val2+val2; System.out.println(result1==result2); } }

13th Jan 2018, 9:49 AM
Burcu
7 Answers
+ 28
i know its val1+++val1 , i am telling that it will be written as val1++ + val1 , not val1 + ++val1 , it would not be possible to explain that using 1 variable only ... as it will give same output if we think in any way with 1 variable "val1" //yes , its not a challenge question , but in most challenge it is used â˜ș
13th Jan 2018, 10:22 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 27
see this â˜ș👍 //& yes, make P of Public small https://www.sololearn.com/Discuss/995934/?ref=app
13th Jan 2018, 10:06 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 27
val1+++val2 is val1 + ++val2 or val1++ + val2 //might need to see operator precedence so it will be val1++ + val2 so 3 and 4
13th Jan 2018, 10:16 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
What val1+++val1 does is it increments just the val1 by 1 and adds it to val1 (as it being a postfix increment, the value of first val1 is still taken as 1 and the result1 leads to 3). Result2 instead, leads to 4 (which causes the comparison to return false).
13th Jan 2018, 10:13 AM
Dev
Dev - avatar
+ 7
@Gaurav: Unexpectedly, his code is a little different. It's val1+++val1; I hope it's not a challenge question :D
13th Jan 2018, 10:20 AM
Dev
Dev - avatar
+ 6
@Burcu: Actually, you must spell the access modifier "public" with a lowercased p. Only then you get the output as false as stated by @Muhammad Khairul Amirin Bin Yaacob
13th Jan 2018, 10:01 AM
Dev
Dev - avatar
+ 5
False -- I ran it in Code Playground
13th Jan 2018, 9:52 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar