kotline Basic type (Datatype)
/*
fun main()
{
print("hellow first main function")
}
*/
fun main(args: Array<String>) {
/*
data type in kotlin
*/
//---number data type--
var b: Byte = 111
var s: Short = 11111
var i: Int = 1111111111
var f: Float = 1.00f
var d: Double = 1.00
var l: Long = 1111111111111111111
var charactervalue: Char = 'c'
var Stringvalue: String = "stringvalue"
var flag: Boolean = true
println("byte---8-bit : " + b)
println("short---16-bit : " + s)
println("int---32-bit : " + i)
println("float---32-bit : " + f)
println("double---64-bit : " + d)
println("long---64-bit : " + l)
println("char---: " + charactervalue)
println("Strin---: " + Stringvalue)
println("boolean---: " + flag)
var name: String
name = "ruhul"
}
/*
fun main()
{
print("hellow first main function")
}
*/
fun main(args: Array<String>) {
/*
data type in kotlin
*/
//---number data type--
var b: Byte = 111
var s: Short = 11111
var i: Int = 1111111111
var f: Float = 1.00f
var d: Double = 1.00
var l: Long = 1111111111111111111
var charactervalue: Char = 'c'
var Stringvalue: String = "stringvalue"
var flag: Boolean = true
println("byte---8-bit : " + b)
println("short---16-bit : " + s)
println("int---32-bit : " + i)
println("float---32-bit : " + f)
println("double---64-bit : " + d)
println("long---64-bit : " + l)
println("char---: " + charactervalue)
println("Strin---: " + Stringvalue)
println("boolean---: " + flag)
var name: String
name = "ruhul"
}
var val deference in kotlin
fun main() {
var Android_Programing_language: String = "java"
println("android official programing language: " + Android_Programing_language)
Android_Programing_language = "Kotline"
println("2019 google announced android official programing language: " + Android_Programing_language)
val Android_MultiPlatform_language: String = "Dart"
println("multiple device run : " + Android_MultiPlatform_language)
/*
Android_MultiPlatform_language=""
/*
note: var--------------valu changable
note: val--------------valu not changable (final)
*/
*/
}
fun main() {
var Android_Programing_language: String = "java"
println("android official programing language: " + Android_Programing_language)
Android_Programing_language = "Kotline"
println("2019 google announced android official programing language: " + Android_Programing_language)
val Android_MultiPlatform_language: String = "Dart"
println("multiple device run : " + Android_MultiPlatform_language)
/*
Android_MultiPlatform_language=""
/*
note: var--------------valu changable
note: val--------------valu not changable (final)
*/
*/
}
Kotlin Type Conversion
method list
- toByte()
- toShort()
- toInt()
- toLong()
- toFloat()
- toDouble()
- toChar()
var value1: Long = 200
val value2: Int =value1.toInt()
println(value2)
}
fun main(args : Array<String>) {
var value1 = 100
val value2: Long =value1.toLong()
println(value2)
}
Kotlin Standard Input/Output
import java.util.Scanner
fun main(args: Array<String>) {
val read = Scanner(System.`in`)
println("Enter your age")
var age = read.nextInt()
println("Your input age is "+age)
}
fun main(args: Array<String>) {
val read = Scanner(System.`in`)
println("Enter your age")
var age = read.nextInt()
println("Your input age is "+age)
}
Kotlin Object Oriented Programing clear code
kotlin class object function
-----------------------------------------------------------------------------------------
fun main() {
val s1=Student("ruhul",510,6)
s1.display()
val s2=Student("amin",510,6)
s2.display()
}
class Student(var name:String,var studentid:Int,var semester:Int) {
val depertment:String="Computer"
fun display(){
println("name: "+name)
println("studentid: "+studentid)
println("semester: "+semester)
println("depertment: "+depertment)
println()
}
}
Constructor: Primary and Secondary Contractor
fun main() {
val subject= Studentclass("ruhul",10)
subject.id=11
println(subject.id)
}
class Studentclass(var name:String)
{
var id:Int=0
constructor(name:String,id:Int): this(name) {
this.id=id
}
init {
println(id)
}
}
Inheritance in Kotlin
example: 1--------------Single Inheritance
fun main() {
val student = Student_info()
println("department: "+student.depertment)
}
class Student_info(): Depertment(){
}
open class Depertment{
var depertment:String = "Computer"
}
example: 2--------------Single Inheritance
fun main() {
val ruhul = Student_info("Ruhul",3510,2132)
ruhul.display()
val amin = Student_info("amin",3010,2133)
amin.display()
}
class Student_info(var name:String,var roll:Int,var reg:Int): Depertment()
{
fun display()
{
println("name: "+name)
println("roll: "+roll)
println("regestration: "+reg)
basic_info()
println()
}
}
open class Depertment{
var department:String = "Computer"
var subject:String = "CSE"
var section:String = "A"
var year:Int=2021
open fun basic_info (){
println("Department : "+department)
println("subject : "+subject)
println("section : "+section)
println("year : "+year)
}
}
example: 3-------------Multiple Inheritance
fun main() {
val ruhul = Student_info("Ruhul",3510,2132)
ruhul.display()
val amin = Student_info("amin",3010,2133)
amin.display()
}
class Student_info(var name:String,var roll:Int,var reg:Int): Depertment(),Collage
{
override var collagename: String="tpi"
fun display()
{
println("Collage name: "+collagename)
println("name: "+name)
println("roll: "+roll)
println("regestration: "+reg)
basic_info()
println()
}
}
interface Collage{
var collagename:String
}
open class Depertment{
var department:String = "Computer"
var subject:String = "CSE"
var section:String = "A"
var year:Int=2021
open fun basic_info (){
println("Department : "+department)
println("subject : "+subject)
println("section : "+section)
println("year : "+year)
}
}
-----------------------------------------------------------------------------------------
Abstraction in Kotlin :
example: 1
fun main() {
val add = Product()
add.add(10,20)
add.sub(10,20)
add.mul(10,20)
add.div(10,20)
}
class Product:Calculate()
{
override fun add(a: Int, b: Int) {
println("add result: "+(a+b))
}
override fun sub(a: Int, b: Int) {
println("sub result: "+(b-a))
}
override fun mul(a: Int, b: Int) {
println("mul result: "+(a*b))
}
override fun div(a: Int, b: Int) {
println("div result: "+(a/b))
}
}
abstract class Calculate{
abstract fun add(a:Int , b:Int)
abstract fun sub(a:Int , b:Int)
abstract fun mul(a:Int , b:Int)
abstract fun div(a:Int , b:Int)
}
example: 2
fun main() {
val room=BedRoom()
room.lightstatus(true)
}
open class Light{
open fun lightstatus(flag: Boolean)
{
println("light status: "+flag)
}
}
abstract class Home:Light(){
override abstract fun lightstatus(flag: Boolean)
}
class BedRoom : Home(){
override fun lightstatus(flag: Boolean) {
if (flag)
{
println("light has open ")
}
else{
println("light has closed ")
}
}
}
-----------------------------------------------------------------------------------------
val add = Product()
add.add(10,20)
add.sub(10,20)
add.mul(10,20)
add.div(10,20)
}
class Product:Calculate()
{
override fun add(a: Int, b: Int) {
println("add result: "+(a+b))
}
override fun sub(a: Int, b: Int) {
println("sub result: "+(b-a))
}
override fun mul(a: Int, b: Int) {
println("mul result: "+(a*b))
}
override fun div(a: Int, b: Int) {
println("div result: "+(a/b))
}
}
abstract class Calculate{
abstract fun add(a:Int , b:Int)
abstract fun sub(a:Int , b:Int)
abstract fun mul(a:Int , b:Int)
abstract fun div(a:Int , b:Int)
}
fun main() {
val room=BedRoom()
room.lightstatus(true)
}
open class Light{
open fun lightstatus(flag: Boolean)
{
println("light status: "+flag)
}
}
abstract class Home:Light(){
override abstract fun lightstatus(flag: Boolean)
}
class BedRoom : Home(){
override fun lightstatus(flag: Boolean) {
if (flag)
{
println("light has open ")
}
else{
println("light has closed ")
}
}
}-----------------------------------------------------------------------------------------
Interface in Kotlin
example: 1 (Basic interface )
fun main() {
val calculate=Calculator()
calculate.add(10,10)
calculate.sub(10,10)
calculate.mul(10,10)
calculate.div(20,10)
}
public interface Calculate{
abstract fun add(a:Int , b:Int)
abstract fun sub(a:Int , b:Int)
abstract fun mul(a:Int , b:Int)
abstract fun div(a:Int , b:Int)
}
class Calculator:Calculate{
override fun add(a: Int, b: Int) {
println("result add: "+(a+b))
}
override fun sub(a: Int, b: Int) {
println("result sub: "+(a-b))
}
override fun mul(a: Int, b: Int) {
println("result mul: "+(a*b))
}
override fun div(a: Int, b: Int) {
println("result: "+(a/b))
}
}
example: 2 (Basic interface )
In This Section, We have to Create Multiple Interfaces inside the same methods to create the normal method and it can override outside the other class at the same time compiler got an error. so how can I fix the problems?
super<Calculate>.result()
super<Product_Calculate>.result()
full code :
fun main() {
val calculate=Calculator()
calculate.add(10,10)
calculate.sub(10,10)
calculate.mul(10,10)
calculate.div(20,10)
calculate.result()
}
public interface Product_Calculate{
abstract fun P_add(a:Int , b:Int)
open fun result()
{
println("this is product result:")
}
}
public interface Calculate{
abstract fun add(a:Int , b:Int)
abstract fun sub(a:Int , b:Int)
abstract fun mul(a:Int , b:Int)
abstract fun div(a:Int , b:Int)
open fun result()
{
println("this is calculate result:")
}
}
class Calculator:Calculate,Product_Calculate{
override fun add(a: Int, b: Int) {
println("result add: "+(a+b))
}
override fun sub(a: Int, b: Int) {
println("result sub: "+(a-b))
}
override fun mul(a: Int, b: Int) {
println("result mul: "+(a*b))
}
override fun div(a: Int, b: Int) {
println("result: "+(a/b))
}
override fun result() {
super<Calculate>.result()
super<Product_Calculate>.result()
}
override fun P_add(a: Int, b: Int) {
println("product total result: "+(a+b))
}
}Data Class In Kotlin:
Data class is used to hold data/state and contains standard functionality. A data keyword is used to declare a class as a data class.Data class internally contains the following functions:
- equals(): Boolean
- hashCode(): Int
- toString(): String
- component() functions corresponding to the properties
- copy()
Example: 1 "data" keyword uses simple code
fun main (args:Array<String>)
{
/* data class is used to hold/state contains Standard functionality */
val persion = Persion("a",10,"student")
println(persion)
}
data class Persion(var name:String , var id:Int , var ocupation:String)
output:
Persion(name=a, id=10, ocupation=student)
- equals(): Boolean
fun main (args:Array<String>)
{
/* data class is used to hold/state contains Standard functionality */
val persion1 = Persion("a",10,"student")
val persion2 = Persion("a",10,"student")
println(persion1.toString())
if (persion1.equals(persion2))
{
println("true")
}else{
print("not same")
}
}
data class Persion(var name:String , var id:Int , var ocupation:String)
- hashCode(): Int
fun main (args:Array<String>)
{
/* data class is used to hold/state contains Standard functionality */
val persion1 = Persion("a",10,"student")
val persion2 = Persion("a",10,"student")
println("persion1 hascod: ${persion1.hashCode()}")
println("persion2 hascod: ${persion2.hashCode()}")
if (persion1.hashCode() == persion2.hashCode()) {
println("object same")
}
else{
println("object not same")
}
}
data class Persion(var name:String , var id:Int , var ocupation:String)
- component() functions corresponding to the properties
notice: sorry guess it will fix and update quickly coming soon
- copy()
fun main (args:Array<String>)
{
/* data class is used to hold/state contains Standard functionality */
val persion1 = Persion("a",10,"student")
val persion2 = persion1.copy()
println(persion1.toString())
println(persion2.toString())
}
data class Persion(var name:String , var id:Int , var ocupation:String)
Kotlin Sealed Class with when
sealed class Notes{
data class Addnotes(var addtexttext:String) :Notes(){}
data class deletenotes(var deletetext:String) :Notes(){}
}
fun adddata():Notes{
return Notes.Addnotes("this is simple notes taking app")
}
fun main(args: Array<String>) {
val addnotes = adddata()
when(addnotes)
{
is Notes.Addnotes ->{
println(addnotes)
}
is Notes.deletenotes ->{
println(addnotes)
}
}
}
Kotlin Extension Functions function
class Grade{
fun isPassed(mark: Int): Boolean{
var a: Int
return mark>40
}
}
fun Grade.isExcellent(marks: Int): Boolean{
return marks > 90
}
fun main(args: Array<String>){
val student = Grade()
val passingStatus = student.isPassed(55)
println("student passing status is $passingStatus")
val excellentStatus = student.isExcellent(95)
println("student excellent status is $excellentStatus")
}
Kotlin Generics Type
Generics is a powerful fether.generics type declare it can allow any type data
Advantages of generic –
- Type casting is evitable- No need to typecast the object.
- Type safety- Generic allows only single type of object at a time.
- Compile time safety- Generics code is checked at compile time
class Vehicle <T> (var info:T){
init { println("vehical information : "+info) }
}
fun main(args : Array<String>) {
var name:Vehicle<String> =Vehicle("Car")
var licence:Vehicle<Int> =Vehicle(48522)
var brad_name:Vehicle<String> =Vehicle("Bangla car")
var warenty_years:Vehicle<Double> =Vehicle(1.5)
}
Generics type Model Class:
class Data<T> (var information: T)
{
init { println("information : "+information) }
}
fun main() {
var info:Data<Model> = Data(Model())
}
data class Model(var name: String?="ruhul",var age: Int?=21,var stedy: String? = "BSC in Cse") {}
Comments
Post a Comment