Kotlin program to retrieve the values of Pair using properties

Learn how to create a Pair and access/retrieve its values using the properties?
Submitted by IncludeHelp, on April 02, 2022

In the previous example, we have discussed how to retrieve the Pair values using the variable name? There is another way which is using properties, with the help of some predefined properties we can easily get the Pair values.

Properties:

In Kotlin Pair, we can use the properties named "first" and "second" to retrieve the values of Pair. The "first" property stores the first value of the Pair, while the "second" property stores the second value of the Pair.

Syntax:

pair_name.first
pair_name.second

Consider the below examples –

Example 1:

fun main() {
	// creating a new instance of the Pair
	var numbers = Pair(10, 20)

	// Printing the values
	println(numbers.first)
	println(numbers.second)
}

Output:

10
20

Example 2:

fun main() {
	// creating a new instance of the Pair
	var student = Pair("Alvin Alexander", 35)

	// Printing the values
	println(student.first)
	println(student.second)
}

Output:

Alvin Alexander
35

Kotlin Pair Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.