ADVERTISEMENT
ADVERTISEMENT

Kotlin example of creating triple using the constructor

Example to create triple using the constructor in Kotlin.
Submitted by IncludeHelp, on April 06, 2022

In Kotlin, the constructor is same as other programming languages, it is a special kind of member function that is invoked when an object is being created and it is primarily used for initializing the variables or properties.

Here is the syntax to create a new instance of the Triple:

Triple(first: A, second: B, third: C)

In the below examples, we will create a triple by using the constructor.

Example 1:

fun main() {
  // creating a new instance of the Triple
  val(a, b, c) = Triple(10, 20, 30)

  // Printing the values
  println(a)
  println(b)
  println(c)
}

Output:

10
20
30

Example 2:

fun main() {
	// creating a new instance of the Triple
	val (name, age, city) = Triple("Alvin", 35, "New York")

	// Printing the values
	println("Name: $name")
	println("Age: $age")
	println("City: $city")
}

Output:

Name: Alvin
Age: 35
City: New York

Kotlin Triple Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.