2.1 programming style
1. Hump nomenclature, without underscores.
For example, public class DataBaseUser
2. The first letter of the class should be capitalized
class People
3. Method and property names should be lowercase
fun hello(){
}
2.2 Kotlin constants and variables
Constant: val
Variables: var
2.2.1 constant
val constrain1=3.3 val constrain3:Int=40 fun main(args:Array<String>){ println(constrain1) println(constrain3) println("constrain1 The value is"+constrain1) println("constrain3 The value is"+constrain3) }
2.2.2 definition and use of variables
var var1:String="Kotlin" var var3:Int=10 var var4=5 set(value){ if(value<=5) field=value } fun main(args:Array<String>){ println("var1 Without reassignment"+var1) println("var1 The value is"+var1) var1=var1+5.3 println("var1 The value is"+var1) var3=var3+5 println("var3 The value is"+var3) var4=var4+5 println("var4 The value is"+var4) }
2.2.3 variable scope
class vals(val a:Int=5){ fun geta():Int = 5 } fun varfun():Int{ var b=10 return b } var c=15 fun main(args:Array<String>){ //println(a) println(vals().a) //println(b) println(varfun()) println(c) }
2.3 data type
Basic data type: Boolean, Number,Char
Reference data type: Null, Object, Array
2.3.1 Boolean
var boo: Boolean=true fun main(args:Array<String>){ if(boo==true){ println("boolean To be true") }else println("boolean For false") }
2.3.2 number
var a1:Int=19 var te1:Int=0x88888 var maxint:Int=Int.MAX_VALUE var minint:Int=Int.MIN_VALUE var long:Long=128L var maxlong:Long=Long.MAX_VALUE var minlong:Long=Long.MIN_VALUE var double:Double=2.88 var maxdouble:Double=Double.MAX_VALUE var mindouble:Double= Double.MIN_VALUE var float:Float=2.0f var maxfloat:Float=Float.MAX_VALUE var minfloat:Float=Float.MIN_VALUE var maxShort:Short=Short.MAX_VALUE var minShort:Short=Short.MIN_VALUE var maxbyte:Byte=Byte.MAX_VALUE var minbyte:Byte=Byte.MIN_VALUE fun main(args:Array<String>){ println(a1) println(te1) println(maxint) println(minint) println(long) println(maxlong) println(minlong) println(double) println(maxdouble) println(mindouble) println(float) println(maxfloat) println(minfloat) println(maxShort) println(minShort) println(maxbyte) println(minbyte) }
2.3.3 string type (Char)
fun main(args:Array<String>){ val letterchar:Char='a' val nubchar:Char='0' val UnicodeChar:Char='\u7231' val yi:Char='\n' println("Escape character ${yi}Letter, char type ${letterchar},number char type ${nubchar},Unicode Code char Type: ${UnicodeChar}") }
2.3.4 string type
2.3.5 Tuple type
fun main(args:Array<String>){ val (status,msg)=Pair(404,"Not Found") val (server,script,database)=Triple("Tomcat","PHP","MySQL") println("One way:Visit ${server}+${script}+${database}Environmental Science,Get the feedback code ${status},Express ${msg}") val http=Pair(404,"Not found") val Development=Triple("Tomcat","PHP","MySQL") println("Mode two:Visit ${Development.first}+${Development.second}+${Development.third}Environment, feedback to get status code ${http.first},Express ${http.second}") }
2.3.6 nullable type
fun main(args:Array<String>){ var address:String?=null //Null type println(address?.length) }
2.3.7 object type
class course(var name:String){ init{ println("The course name you chose is ${name}") } } fun main(args:Array<String>){ val studentobj=course("Kotlin") }
2.3.8 array type
val arrayOfInt:IntArray= intArrayOf(1,2,3,4,5) val arrayOfChar:CharArray=charArrayOf('H','e','l','l','o') val arrayOfString: Array<String> = arrayOf("I","love","Kotlin") fun main(args:Array<String>){ println(arrayOfInt[2]) for(String in arrayOfString){ println(String) } }
2.3.9 check and conversion of array type
fun main(args:Array<String>){ val a=10 if(a is Int){ println(a) } if(a!is Int){ println(a) } val b: String?=a as? String println(b) val c=a.toDouble() println(c) }