Ad space (banner)
โ˜•Java Lessons
Lesson 47 / 68

Functions That Return Multiple Values (record)

This lesson covers returning multiple values from a single function in Java using record, so you understand a function isn't limited to a single return value. It's for anyone searching "Java record multiple return values."

record is a relatively recent Java feature for bundling multiple values together. It's like "handing over one box holding both a minimum-value card and a maximum-value card."

The example defines a type for bundling data in one line, record MinMax(int min, int max) {}, and has the minMax() method return this MinMax type โ€” returning the minimum and maximum together.

A common early mistake is not knowing a record automatically generates getters and an equals method for you. Doing the same thing with a regular class would take a lot of boilerplate code, but a record does it in one line.

In real projects, this is genuinely useful for things other languages would handle with a tuple or pair โ€” Java lets you write it in a type-safe way with record.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)