003-003-009
Safe Null Check Using Short-Circuit Evaluation
Hard
Problem Description
Safe Null Check Using Short-Circuit Evaluation
In this problem, you will create a program that reads a string from standard input, assigns null to the variable if the input is "null", otherwise assigns the input string, then uses text != null && text.length() >= 3 to safely perform a null check and length check via short-circuit evaluation, and prints the result.
Input format: A single line string (null as input is treated as null)
Output format: Result: Valid or Result: Invalid
Learning Objectives:
- Understand how short-circuit evaluation works
- Use the
&&operator to skip right-side evaluation when the left side is false - Write null-safe code to prevent
NullPointerException
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up