Hello am new. I recently purchased paperback Kotlin Apprentice and I have few doubts. I’m a beginner who knows little java. So please be patient if my questions are stupid.
I tried running kotlin through command line. Why does a kotlin need to be compiled to a jar. If it’s running on jvm, is it not supposed to create .class? (I compile it without including jar, I saw two .class files which gave “main missing” error after trying to run .class files even though there is main function).
What’s wrong with below? I’m getting main method missing error when I run the code.
class sample {
fun main(){
println(“Its a sample”)
}
}
In java we have “;” that says statement end and compile knows its end of statement. How does compiler know its end of statement in kotlin?
I saw you mentioned in book that kotlin runs on jvm as well as it can be run without jvm(i.e kt source code directly converted to machine code without bytecode). If so, why do I see only generated .class files or .jar? where is machine code file? Please explain me if I got my fundamentals wrong.
In the Type Conversion section, for ex: var num:Double = 10.9
num.toInt() value is 10. Why is this always rounded off to number before decimal?
There is example on page 71 (chapter3 Operator with mixed types).
val a:Double = 19.5
val b:Int = 10
val c:Double = a*b.toDouble()
you mentioned kotlin automatically converts b to double “It turns out this is unnecessary” - why unnecessary? What conditions are being looked at to make it needed some cases but not needed in some cases?
I dont have enough targeted chapter problems to practice. Please provide source for more problems to solve which are chapters targeted.
Hi Roberto! So I didn’t work on these chapters so I can’t answer all your questions, but I’ll give a shot to what I can:
A jar is an executable file. Without the jar wrapper, the JVM doesn’t really know what to do with the class files that you’ve compiled - my understanding is because creating it creates whatever points to the correct file to have a main operation, but that could be an oversimplification. Here’s some info from Oracle on the jar format.
This one I don’t really know - my instinct is that main doesn’t need to be inside a particular class.
Basically, the compiler is advanced enough to figure out what the end of a statement is in Kotlin without the semicolon, so the semicolon becomes unnecessary. This is also the case in Swift. You can still use the semicolon if you want to, but most Kotlin code standards I’ve seen discourage use of the semi-colon in most cases.
So it is possible to compile things to LLVM bytecode, but that is something that you have to explicitly opt into. For short examples like the ones in the book, it is considerably easier to use a jar since you’re working on a machine that has the JVM installed.
If you take a look at the docs for Double.toInt(), you’ll see that it’s expressly called out as rounding down rather than rounding to the nearest integer.
Thank you. I just joined the chat. But I’m getting message that “You do not have permission to send messages in this channel”. Sorry I new to Discord too. Please let me know if I can post in the chat if I have any doubts.
One more quick question. Is Kotlin Object oriented or functional programming language? Also I see google answers that main function cannot be in class and it has to be outside class. This is not acceptable in Java. Because data and methods are tied to object. Object will have a reference and hence that reference can be used to the tied up objects fields/methods. But in kotlin, a “.kt” file just hosts data fields and functions? I just couldn’t get my head around this. I’m still in chapter 3 of reading. Is it too early for these questions and following may have answers?
In Raywenderlich’s Discord server, send a private message to RWBOT, with the following command !link_account
The bot will reply with a link to the Raywenderlich site. Click on that link and confirm the connection request.
You can find RWBOT in the chat user list on the right side (on the app, click on the people icon top right)