For variables, the declaration might be different Java and Kotlin. This is only cover for Kotlin but will update after I learn it with different language in different section. You can use Kotlin Playground to explore how to write code with Kotlin.
To implement it, you can use with two forward slashes (//). You put it above or after the variable, function, etc. You can also use multiple single line comments if you want (or you can use multi-line comments)
For example,
kotlin
funmain() {// This is variable for namevar name: String="John"println(name) // This is used to print the text}
funmain() {// This is variable for namevar name: String="John"println(name) // This is used to print the text}
To implement multi-line, you might need the specific space for the comments. You can start and close with forward slashes and asterisk (/* */) (order matters, open it with /* and close it with */)
For example,
kotlin
funmain() {/* * This is variable for name */var name: String="John"println(name)}
funmain() {/* * This is variable for name */var name: String="John"println(name)}
Variables
For variables, the declaration might be different Java and Kotlin. This is only cover for Kotlin but will update after I learn it with different language in different section. You can use Kotlin Playground to explore how to write code with Kotlin.
Getting Started
Do you familiar with this? Yes, when you start learn programming language, you might start with
Hello, World!
orHello World
.fun
: represents a functionmain
: it's the function name, usually used run the main application (same implementation inGo
as well)println
: it's used to print the information you want to log, it ends with add the new line.print
: you can also use print to show the log but it doesn't add new line, so the log will be on the same line if you also put the log after.Variables
For variable declaration, we can use
val
orvar
. What's the difference?val
: used for variable that the value will bestatic
(cannot be changed) or constant.var
: used for variable that can bemodified
/changed
.There are different ways to declare the variable.
Comments
The implementation for comments relatively same with other programming languages.
Single-Line Comments
To implement it, you can use with two forward slashes (
//
). You put itabove
orafter
the variable, function, etc. You can also use multiple single line comments if you want (or you can usemulti-line comments
)For example,
Multi-Line Comments
To implement
multi-line
, you might need the specific space for the comments. You can start and close with forward slashes and asterisk(/* */)
(order matters, open it with/*
and close it with*/
)For example,
Data Types
There are a lot data types in Kotlin (might not complete here).
Data types are divided into different groups:
Number Types
Number itself divided into 2 (two) groups,
Integer
andFloating Point
.Integer
: has 4 (four) different types (Byte
,Short
,Int
, andLong
), if you're not defined the types, might fallback toInt
Floating Point
: has 2 (two) different types (Float
andDouble
), if you're not defined the types, might fallback toDouble
Byte
Short
Int
Long
Float
Double
Boolean Type
The
Boolean
data types aretrue
andfalse
Character Type
The
Char
type can only store asingle
character and must surrounded bysingle
quotes. It could only take valueA-Z
anda-z
.For example
String Type
The
String
type can store sequence of characters like example in the beginning of the page