Null
Null
A special value indicating the absence of a value. A programming concept that causes NullPointerException and is known as Tony Hoare's "Billion Dollar Mistake."
Null
A special value indicating the absence of a value. A programming concept that causes NullPointerException and is known as Tony Hoare's "Billion Dollar Mistake."
Null is a special value in programming that represents "absence of value." It is used to express that a variable does not reference any object, has not yet been initialized, or is intentionally empty. In databases, it is used to express an "unknown value" or an "inapplicable value," and it has a clearly different meaning from 0 or an empty string ("").
The null reference was introduced by computer scientist Tony Hoare in 1965 while he was designing the ALGOL W language. At the QCon conference in 2009, he called it the "Billion Dollar Mistake." This means that the numerous bugs, security vulnerabilities, and system crashes caused by null references over the past 40 years have resulted in costs of at least one billion dollars.
In JavaScript, there are two values that represent "absence of value": null and undefined. null indicates a case where the developer intentionally assigned "no value," while undefined indicates a state where a variable has been declared but no value has been assigned yet. These two values are equal in a == comparison but different in a === comparison.
Modern programming languages offer various approaches to Null Safety. Representative examples include Java 8+'s Optional, Kotlin's Nullable Types (?), Swift's Optional, and Rust's Option. They specify the possibility of null at compile time and enforce safe ways of handling null, thereby preventing runtime NullPointerException.
// 1. μ ν΅μ μΈ null μ²΄ν¬ public String getUserName(User user) { if (user != null) { if (user.getName() != null) { return user.getName(); } } return "Anonymous"; } // 2. Optional μ¬μ© (Java 8+) public String getUserNameSafe(User user) { return Optional.ofNullable(user) .map(User::getName) .orElse("Anonymous"); } // 3. Objects.requireNonNull - λ°©μ΄μ νλ‘κ·Έλλ° public void processUser(User user) { Objects.requireNonNull(user, "User must not be null"); // userκ° nullμ΄λ©΄ NullPointerException λ°μ }
// null vs undefined λΉκ΅ let a; // undefined (μ μΈλ§ ν¨) let b = null; // null (μλμ μΌλ‘ λΉμ) console.log(a == b); // true (λμ¨ν λΉκ΅) console.log(a === b); // false (μ격ν λΉκ΅) // Optional Chaining (?.) - ES2020 const userName = user?.profile?.name ?? "Anonymous"; // Nullish Coalescing (??) - null/undefinedλ§ μ²λ¦¬ const count = inputCount ?? 0; // 0μ΄λ ""λ κ·Έλλ‘ μ¬μ© const count2 = inputCount || 0; // 0μ΄λ ""λ 0μΌλ‘ λ체
// Kotlinμ Nullable Types var name: String = "John" // null λΆκ° var nullableName: String? = null // null κ°λ₯ // Safe call operator (?.) val length = nullableName?.length // nullμ΄λ©΄ null λ°ν // Elvis operator (?:) val len = nullableName?.length ?: 0 // nullμ΄λ©΄ 0 // Not-null assertion (!!) - κΆμ₯νμ§ μμ val forceLength = nullableName!!.length // NPE λ°μ κ°λ₯ // letμ μ΄μ©ν null μ²΄ν¬ nullableName?.let { println("Name length: ${it.length}") }
user.getAddress().getCity() part of this code looks risky. A NullPointerException can occur in situations where user or address may be null. How should we fix it?Optional by using Optional.ofNullable(user).map(User::getAddress).map(Address::getCity).orElse("Unknown"). Or we could also make it fail fast at the start of the method with Objects.requireNonNull.String?, and the compiler enforces it. Rust has no null itself and expresses the presence or absence of a value with Option<T>. Java introduced Optional, but cannot enforce it because of compatibility with existing code.requireNonNull. Third, fields are initialized in the constructor to minimize null states.Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
Optional.get() directly.NULL = NULL is false. Use the IS NULL, COALESCE, and NULLIF functions appropriately.