# Java
<!-- TODO: Expand this note -->
## OSGi: Import-Bundle vs Require-Bundle
When using OSGi, prefer `Require-Bundle` for explicit bundle dependencies.
See: [StackOverflow](https://stackoverflow.com/questions/11559980/what-is-the-difference-between-import-bundle-and-require-bundle)
## Documentation
### Return Statements
Small functions can use single-line return statements. See [SO](https://stackoverflow.com/a/25957713/4700312).
### @InheritDoc
Empty `@inheritDoc` tags are not useful — JDT already chases overrides automatically.
## Parallel Streams
Parallel streams have significant overhead and aren't necessarily faster. Only use when:
- Processing a massive number of items (or each item takes significant time)
- There's a performance problem to solve
- The process doesn't already run in a multi-threaded environment
See: [StackOverflow](https://stackoverflow.com/q/20375176/4700312)
## String Case Conversion
Always provide a `Locale` when calling `String.toLowerCase()` or `toUpperCase()`. Some languages have tricky rules — e.g., Turkish where `I` and `i` are not case-converted to one another.
See: [StackOverflow](https://stackoverflow.com/questions/10336730/which-locale-should-i-specify-when-i-call-stringtolowercase)
## Synchronization
Java provides two synchronization idioms: **synchronized methods** and **synchronized statements**.
See: [Oracle docs](https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html)
For thread-safe lazy initialization, see [[Double-Checked Locking]] and [[Initialization-on-demand Holder Idiom]].
## Currying
Currying transforms a multi-argument function into a sequence of single-argument functions:
```java
Function<String, Function<String, Letter>> curriedCreator
= salutation -> body -> new Letter(salutation, body);
```
See: [Baeldung](https://www.baeldung.com/java-currying)
## JVM Utilities
Available in the JDK `bin/` directory:
| Tool | Purpose | |
| -------------- | --------------------------- | --- |
| `jps` | List running Java processes | |
| `jstack <PID>` | Show thread stack traces | |
| `jfr` | Java Flight Recorder | |
### Java Flight Recorder (JFR)
JVM flags for diagnostics:
```text
-XX:+UnlockDiagnosticVMOptions
-XX:+DebugNonSafepoints
-XX:+FlightRecorder
-XX:FlightRecorderOptions=stackdepth=200
```
Start a recording:
```text
-XX:StartFlightRecording=duration=10m,delay=5m,filename=./flight_recording.jfr
```
Open recordings with **JDK Mission Control** (JMC).
---
See also: [[Double-Checked Locking]], [[Initialization-on-demand Holder Idiom]]