CodeGym /Courses /Java Multithreading /Practice with autoboxing Booleans

Practice with autoboxing Booleans

Java Multithreading
Level 9 , Lesson 7
Available
Practice with autoboxing Booleans - 1

"Hi, Amigo!"

16
Task
Java Multithreading, level 9, lesson 7
Locked
Weird bugs O_o
Fix 2 bugs: 1) an exception occurs 2) the program becomes unresponsive Make minimal changes.
9
Task
Java Multithreading, level 9, lesson 7
Locked
Autoboxing features (part 2)
Fix the bug causing a NullPointerException in the getValue() method. Read the supplementary article about the special features of autoboxing.
Comments (4)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Andrei Level 41
21 May 2021
For autoboxing part 2 here is a comment from Guadalupe from a question asked in the Help section: " The problem lies in that after the first boolean check, there is the number 100 in 'int' form. This forces the compiler to start the process of boxing so when the second boolean check is performed what is happening behind the scenes is that it calls the Integer constructor ( new Integer( ) )and passes the result of the second to as an argument. If you pass null to this constructor then a null pointer exception is thrown. So, the line: return first ? 100 : second ? 200 : null; processes like this: #1 it checks the valuation of 'first', which in this task is just a boolean but could be a boolean expression. It then moves to the result if true and result if false. #2 Because the result if true is a primitive, the auto-boxing feature kicks in and the constructor for Integer is called #3 if the result is true the 100 is passed to the constructor and all is good #4 if the result is false then it checks the second expression and passes either of the results to the Integer constructor #5 if true then 200 is passed in and all is good #6 if false then null is passed in and the exception is thrown You will find that if you have just a single expression, then null value can be assigned. So the key problem in the above code is when the auto-boxing feature occurs. " So think about what type of element you would need so that null is accepted.
Lisa L Level 47, Nuremberg, Germany
13 May 2022
the JLS has tables how conversion is done
Henrique Level 41, São Paulo, Brazil
18 August 2020
@CodeGym Where is this "supplementary article about the special features of autoboxing"????????????????????