SoloLearn quiz mistake! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

SoloLearn quiz mistake!

as i was rating people quiz submission in java i encountered a problem that involves byte b = 127 System.out.println(b+1) the correct output to this is -128...well that's what SoloLearn has of course any coder that knows a little, knows that can't possible be true the above statements is equivalent to System.out.println(127 + 1); which outputs 128 not -128 they got mixed up with the += operator, which indeed will wrap the byte back to the smallest assignable value as it maximum positive value is 127

6th Dec 2018, 6:54 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
5 Answers
+ 22
//Have a look at following code : public class Program{ public static void main(String[] args){ System.out.print(1+1.2); //● implicit typecasting 👍 (int to double) // ● same happened in example by vlada [no role of byte I guess as it got typecasted automatically to higher data type int though byte b = _ , at _ there must be number between -128 to 127 else we need explicit typecasting to byte there] } } ● final conclusion : 1) byte b=127; System.out.println(b+1); 2) byte b=(byte)(127+1); System.out.println(b); //both 1) & 2) are different , though output in 1) is just opposite of output in 2) [ very nice case 👌] Mind To Machine 🤖😎 He is APJ Abdul Kalam.
7th Dec 2018, 2:47 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 10
● Range of byte will be from -2^7 to 2^7 that is -128 to 127 , since 128 can't come in this range , in order to bring 128 in this range , substraction of 256 will be done , ie 128-256 = -128 //for anyone who want to know reason of that output in MSD code☺👍
6th Dec 2018, 8:06 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
Yes if that so you can report that question by going on reveiew correct answer and click the right top" i" and report in as wrong answer it will fixed soon then https://code.sololearn.com/c2QE019xx8b3/?ref=app
6th Dec 2018, 7:09 PM
MsJ
MsJ - avatar
+ 3
Mind To Machine 🤖😎 you are right. If the question is like code below, output is +128. https://code.sololearn.com/cNm1Im2b0pxz/?ref=app and in addition different behaviour when data type is int. very nice case :-)
6th Dec 2018, 10:30 PM
vlada
+ 1
Gaurav Agrawal who's that guy as your avatar?
6th Dec 2018, 8:51 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar