Updated March 30, 2023
Introduction to Kotlin println
In kotlin println is one of the default statements that can be used to print the statement automatically one by one or nextLine of the code that will be appended using the newline at the end of the output print the variable using’ $’ symbol followed by the var and val type variable inside the double-quoted using a string literal it accepts all the datatype arguments it will be printed output on the console screen, and standard output function calls both internally and externally supported and passed to the data as the parameters which is similar to the java language.
Syntax
The kotlin language uses many default keywords, variables, and functions to implement the mobile-based applications, with some pre-defined keywords, including the functions. Like that, println() is the default function for handling and print the statements, which the coder declares.
fun main(args: Array<String>)
{
val variablename= values
println("coder statements and ${variablename}"")
var var2= values
println("coder statements and $var2")
}
The above codes are the basic syntax for utilising the println() method and its statements used by the kotlin language. Then, based on the user scenario, we can print the outputs on the console screen.
How does println work in Kotlin?
- Using the println() method, the coder prints the statements and the outputs the programmer enters, and it depends on the application point of view. The programmer entered all the codes like keywords, variables, functions, and classes that have both primary and secondary classes. Some nested classes concept implements the parent class with the child class, and then it’s called by the specific packages. For each method, both default and customized methods used the println() statements.
- The println() method has similar to the print() method but some difference is there like print() method prints the string inside of the quotes and println() method print the strings inside the quotes which is similar like print() method but the mouse cursor which automatically moves to the next line of the console screen. We can use the expressions and catch the exceptions and errors instance whenever it requires like the try-catch statement is used for handling the exceptions block, but the catch and finally blocks to capture and print the exceptions using the print and println() method. And also, when we want to print the variable inside the println() method, we can use the dollar($) symbol followed by either var and val names along with inside of the double-quoted string literals.
Examples of Kotlin println
Given below are the examples of Kotlin println:
Example #1
Code:
import java.util.Scanner
class examp1{
fun demo()
{
val strinp="Welcome To My Domain its the first example that related to the kotlin println() statement"
var mlist = mutableListOf<String>()
mlist.add("Please add the first input")
mlist.add("Please add the second input")
mlist.add("Please add the third input")
mlist.add("Please add the fourth input")
mlist.add("Please add the fifth input")
var finp = mutableListOf<String>("1","One")
var finp1 = mutableListOf<String>("2","Two")
var finp2 = mutableListOf<String>("3","Three")
println("Please follow the below loop iteration")
for(vlist in mlist){
println(vlist)
}
println("Thank you users your mutablelist datas are entered successfully")
println(mlist[1])
mlist.add(1,"June")
println("We can modify the first mutable list value as mlist.add(1,\"June\")")
for(vlist in mlist){
println(vlist)
}
mlist.add("July")
println("Again we can add one more list values mlist.add(\"July\")")
for(vlist in mlist){
println(vlist)
}
mlist.addAll(2,finp1)
println("We can add all the list values into the single list: mlist.addAll(1,finp1)")
for(vlist in mlist){
println(vlist)
}
mlist.addAll(finp)
println("We can add all the values and make it to the single list: mlist.addAll(finp)")
for(vlist in mlist){
println(vlist)
}
mlist.remove("July")
println("We can remove the specified values: mlist.remove(\"July\")")
for(vlist in mlist){
println(vlist)
}
}
}
fun main(args : Array<String>) {
val num = 41.83
println("Your input num is:")
println("$num")
println("num = $num")
println("${num + num}")
println(41.83)
val sc = Scanner(System.`in`)
print("Please enter your input number: ")
var il:Int = sc.nextInt()
println("You entered input is: $il")
var ob=examp1()
ob.demo()
}
Output:
In the above example, we used collection concepts like mutablelist class for to add and remove the elements in the list, and the result is printed on the console.
Example #2
Code:
import java.util.Date
import java.text.SimpleDateFormat
class Exam2() {
var id: Int = 0
var sname: String = ""
var city: String = ""
fun demo2(){
val sinp="41,Sivaraman, Chennai"
println(sinp)
}
}
enum class Second(var sec: String) {
demo("Welcome To My Domain its the second example that related to the kotlin println()"){
override fun sample() {
println("Thank you users have a nice day")
}
},
demo1("We can override the sample method"){
override fun sample() {
println("Current month is june")
}
},
demo2("Again we override the sample method"){
override fun sample() {
println("Next month is july")
}
};
abstract fun sample()
fun demo1(svalues: String): String{
return "Have a Nice day users"
}
}
fun main(args: Array<String>) {
val inp1 = Exam2()
var sinp=inp1.demo2()
println(SimpleDateFormat("yyyy-MM-ddX").parse("2021-06-22+00"))
println("Welcome To My Domain its the second example that related to the kotlin println() method, $sinp")
println("Hello users")
println(41)
println(1234567L)
println(0b01001111)
println(41.32)
println(41.762F)
println('a')
println(true)
println()
var st = "Hello users thank you for spenting ur time with us"
println(st)
println(st)
}
Output:
In the second example, we used the date format package utilised in the kotlin codes with some operations like integer, string, float, double and boolean datatype values printed on the output console.
Example #3
Code:
import java.util.Scanner
fun main(args: Array<String>) {
try {
val inpread = Scanner(System.`in`)
println("Welcome To My Domain its the third example that related to the kotlin println()")
println("Please enter your inputs")
var id = inpread.nextInt()
println("Your input id is "+id)
val lamb: (Int) -> Unit= {st: Int -> println(st) }
demo(41,83,lamb)
val inpdata = 12 / 4
println(inpdata)
}
catch (e: NullPointerException) {
println(e)
} finally {
println("finally block always executed whenever try is executing")
}
println("Have in Nice Day users please try again")
}
fun demo(in1: Int, in2: Int,lamb: (Int) -> Unit){
val result = in1 + in2
println(result)
}
Output:
In the final example, we used the try-catch block for handling the exceptions and performed the arithmetic operations using the lambda expressions.
Conclusion
In kotlin language, we used many default methods, classes, and keywords to implement the application. So all the programming logic will be implemented using some codes like classes and methods eventhough some additional operations are performed, results are printed on the output console by using the print() and println() methods.
Recommended Articles
This is a guide to Kotlin println. Here we discuss the introduction, syntax, and working of println in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –