Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Comments

Hello, I'm Anna and in this lesson we will be learning about comments in Kotlin. So let's see what are comments, and how do we write comments in Kotlin. Comments are the line of code that the compiler simply ignores while executing the codes. Here I had said that main function Is the first function to be executed when our Kotlin app runs. So let us mention this in our comment. If you want to write the comment in a single line use the // followed by space. Space is usually important because of the coding standards. So let's write the command for this main function. Let's save it write is the first function to be executed when the app runs. What if you want to write multiple lines of code? You can write like this, but this is not the conventional way. If you want your function header to have multiple lines which usually is the syntax for it is /**/. Press enter and the star will be automatically printed for you. And then enter, enter, enter and you're get this for writing your comments. To write the multiple line of comment we have this syntax, /**/. If you want to write a single line comment you can mention it here. The compiler comes to execute printIn("Hello World"). But when it finds the comment, it simply ignores it. So no matter what, your comment will not affect the execution of the code at any cost. You can write the single line comment here. Let us run our code and see what happens. We only have hello world printed, but if you write a code after the comment thinking that it will be executed, you're wrong. The compiler will treat this entire statement which follows the forward slashes, it will be treated as comment. Let's see what happens here. You see, we get nothing in the output because this line is completely ignored by the compiler thinking that it is a comment. So, if you want your statement to be executed, write in a fresh line or write the comment after it. So this is all about comments. In the next video we will be learning about variables and data type in Kotlin. I'll also tell you about Val and Val keyword and what are the differences between the two. Until then, happy programming, keep smiling and have a good day.

Back to the top