1. How to Get Output From Kotlin?

In this tutorial, we will discuss how to get the output of the Kotlin code. Actually, a standard Kotlin operation is performed to send the byte stream from the main memory to the output device. And by doing so, we can get results of any type of Kotlin data on the screen. Usually, we utilize Kotlin methods to get the code results on the screen. There are several ways to print the data using Kotlin language.

1.1. Methods To Get Kotlin Output

Below are the two Kotlin methods used to print data.

  1. println() Method
  2. print() Method

Tutorial Contents:

  1. Kotlin Output
  2. println() Method
  3. print() Method

2. println() Method

The Kotlin println() method is used widely to get or print the results. Consider the below example to comprehend this Kotlin method.

Example

fun main() {
println("Hello TutsInsider!")
}
Hello TutsInsider!

Moreover, we can include one to many methods within the main function as needed. Moreover, you can also print numbers using this println() Kotlin method.

Example

fun main() {
	println("My name is John.")
	println("I am 20 years old.")
	println("My bank account number is :")
	println(2144124512)
}
My name is John.
I am 20 years old.
My bank account number is :
2144124512

3. print() Method

The next Kotlin method to get the output is using the print() method. It works similarly to the println() method, but there is a slight difference in its results. Actually, it does not add a new line when a new output is generated, instead, it keeps the cursor on the previous line and prints the data. Consider the following instance to understand it completely.

Example

fun main() {
	print("My name is John.")
	print("I am 20 years old.")
	print("My bank account number is :")
	print(2144124512)
}
My name is John.I am 20 years old.My bank account number is :2144124512

Actually, the println() method uses System.out.println() and the print() method calls the System.out.print() method in Java. Also, the working principle of both methods is quite similar apart from this difference. Both Kotlin methods can print text, and numbers, perform calculations, or even print data stored inside the variables. Later in the tutorial series, we will be using these methods to write more complex code examples.

Give Us Your Feedback
OR
If You Need Any Help!
Contact Us