Skip to content

Commit 0f2c917

Browse files
Update NULL Topic
1 parent b0daba1 commit 0f2c917

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
```Go
2-
// TODO
2+
// Go uses nil for pointers, slices, maps, channels, functions, and interfaces.
3+
4+
var pointer *int = nil
5+
var values []int = nil
6+
var users map[string]int = nil
7+
8+
if pointer == nil {
9+
// ...
10+
}
11+
12+
// Basic value types do not use nil.
13+
var value int = 0
314
```
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
```Java
2-
// TODO
2+
String variableName = null;
3+
4+
if (variableName == null) {
5+
// ...
6+
}
7+
8+
if (variableName != null) {
9+
System.out.println(variableName.length());
10+
}
11+
12+
// Optional can be used when absence is part of the API contract.
13+
Optional<String> optionalValue = Optional.empty();
14+
Optional<String> optionalValue = Optional.of("value");
315
```
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
```Rust
2-
// TODO
2+
// Rust does not have null.
3+
// Use Option<T> when a value can be absent.
4+
5+
let variable_name: Option<String> = None;
6+
let variable_name: Option<String> = Some(String::from("value"));
7+
8+
if let Some(value) = variable_name {
9+
println!("{}", value);
10+
}
11+
12+
match variable_name {
13+
Some(value) => println!("{}", value),
14+
None => println!("No value"),
15+
}
316
```

docs/programming-languages/null/_typescript.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
// Declaring nullable
1010
// ------------------------------------
1111

12-
// TODO
12+
let variableName: string | null = null;
13+
let variableName: string | undefined = undefined;
14+
let variableName: string | null | undefined = null;
15+
16+
function myFunction(value?: string): void {
17+
// value is string | undefined
18+
}
1319
```
1420

1521
```Typescript

0 commit comments

Comments
 (0)