Skip to content

Commit 9072dd9

Browse files
committed
feat(Package): add Test class with null-safe getName demo
What - Added `Test` class in package `Package`. - Contains a `main` method that: - Calls `getName(2)` to simulate name retrieval. - Checks for `null` before using the result. - If non-null, prints `name.toUpperCase()` to stdout. - `getName(int id)` currently returns `null` placeholder (stub). Why - Demonstrates a **null-check pattern** before dereferencing method return values. - Provides a skeleton for later implementing real logic (e.g., fetching a name from a database, map, or API). - Serves as a small example of defensive coding against `NullPointerException`. How - Defined `main` method with program entry point. - Invoked `getName(2)` and stored in `String name`. - Added `if (name != null)` guard before calling `name.toUpperCase()`. - Stubbed `getName(int id)` to always return `null`. Logic - **Inputs:** integer `id` (in example, `2`). - **Outputs:** no output produced since `getName` returns `null`. - **Flow:** 1. Call `getName(2)` → returns `null`. 2. Assign result to `name`. 3. Run `if (name != null)`. Condition fails (false). 4. Program terminates silently without printing anything. - **Edge cases handled:** - Prevents `NullPointerException` when `name` is `null`. - Safe if `getName` later evolves to return dynamic values (including nulls). - **Complexity / performance:** trivial; O(1) operations. - **Concurrency / thread-safety:** - No shared mutable state. - Safe for multi-threaded invocation. - **Error handling:** - Explicitly avoids runtime error (`NullPointerException`). - No exceptions thrown in current stub. Real-life applications - Template for testing null-safety in methods that fetch data from external systems. - Example in tutorials for teaching how to avoid dereferencing nulls. - Basis for implementing business logic where missing values are valid (and must be checked before use). Notes - Current implementation is a stub. Real implementations should replace `return null` with actual logic. - Alternatives: - Return `Optional<String>` from `getName` to force callers to handle absence explicitly. - Use `Objects.requireNonNull` if null is unexpected in future designs. - `toUpperCase()` is locale-sensitive; in production, consider `toUpperCase(Locale.ROOT)` for deterministic results. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e044125 commit 9072dd9

File tree

1 file changed

+14
-0
lines changed
  • Java 8 Crash Course/Lambda Expression/Optional/src/Package

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Package;
2+
3+
public class Test {
4+
public static void main(String[] args) {
5+
String name = getName(2);
6+
if(name != null) {
7+
System.out.println(name.toUpperCase());
8+
}
9+
}
10+
11+
private static String getName(int id) {
12+
return null;
13+
}
14+
}

0 commit comments

Comments
 (0)