kotlin与java在Android开发中的简单对比(正在更新中),快手android面试经验

2022/1/29 17:04:52

本文主要是介绍kotlin与java在Android开发中的简单对比(正在更新中),快手android面试经验,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  • 2.2.1 Kotlin调用Java

  • 2.2.2 java调用kotlin

一、基础

===================================================================

| | java | kotlin |

| — | — | — |

| 继承/实现 | extend implements | |

| 强转 | Integer.parse(String) | String.toInt/as Int |

| 找控件 | findViewById(R.id.btn_login) | 直接用btn_login就行,就是view本身 |

| TextView | tv.setText(“”) | tv.text=“” |

| EditText | ed.setText("") | ed.text = Editable.Factory.getInstance().newEditable("") |

二、集合

===================================================================

1.集合与可空性

前边我们知道如果一个变量可以为空,我们在定义的时候就在该变量前边加个?,来表示该变量的值可为null。同样集合也可以事先在定义的时候知道是否持有null元素。对于一个集合,我们也应该考虑到,这个集合是否可以存储null值,对应于有包含可空值的集合和包含非空值的集合。

举例 val result=ArrayList<Int?>() ArrayList<Int?>是能持有Int?类型值的列表,换句话说就是,可以持有Int或者是null。

这里需要注意的是,变量自己类型的可空性和用作类型参数的类型的可控性是有区别的。 即包含可空Int列表和包含Int的可空列表

包含可空的int列表 :(指的是列表中单个值是可空的),列表本身始终不为null,但是列表中的每个值都可以为null,

包含int的可空列表:(指的是整个列表是可空的) 可能包含空引用,而不是列表实例,但列表中的元素保证是非空的

所以要小心决定是集合的元素还是集合本身可空。

2.只读集合和可变集合

Kotlin的集合设计与Java不同的是,它把访问集合数据的接口和修改集合数据的接口分开了。这种区别存在于最基础的使用集合的接口之中,Kotlin.collections.Collection。使用这个接口,可以遍历集合中的元素、获取集合的大小、判断集合中是否包含某个元素,以及执行其他从该集合中读取数据的操作。但是这个接口没有任何添加或移除元素的方法。

使用Kotlin.collections.MutableCollections接口可以修改集合中的数据。它继承了普通的Kotlin.collections.Collection接口,还提供了方法来添加和移除元素、清空集合等。

| 集合类型 | 只读 | 可变 |

| — | — | — |

| List | listOf | mutableListOf

arrayListOf |

| Set | setOf | mutableListOf

hashSetOf

linkedSetOf

sortedSetOf |

| Map | mapOf | mutableMapOf

hashMapOf

linkedMapOf

sortedMapOf |

        1.1.1 常量与变量


java代码:

String mName = “Java string name”;

final String mName = “Java string name”;

kotlin代码:

var mName = “kotlin string name”

val mName = “kotlin string name”

        1.1.2 null声明


java代码:

String mName;

mName = null;

kotlin代码:

var mName : String?

mName = null

        1.1.2 换行


java代码:

String text = “First Line\n” +

“Second Line\n” +

“Third Line”;

kotlin代码:

val text = “”"

|First Line

|Second Line

|Third Line

“”".trimMargin()

        1.1.3 三元表达式(三目运算符)


java代码:

String text = x > 5 ? “x > 5” : “x <= 5”;

kotlin代码:

val text = if (x > 5)

“x > 5”

else “x <= 5”

        1.1.4 多重条件


java代码:

if (score >= 0 && score <= 300) { }

kotlin代码:

if (score in 0…300) { }

        1.1.5 拼接字符串

------------------------------------------

java代码:

String strA = “a”;

String strB = “b”;

Log.e(“TAG”, “print:” + strA + strB);

kotlin代码:

val strA = “a”

val strB = “b”

Log.e(“TAG”, “print: s t r A strA strAstrB”)

        1.1.8 更方便的集合操作


java代码:

final List listOfNumber = Arrays.asList(1, 2, 3, 4);

final Map<Integer, String> keyValue = new HashMap<Integer, String>();

map.put(1, “Amit”);

map.put(2, “Ali”);

map.put(3, “Mindorks”);

// Java 9

final List listOfNumber = List.of(1, 2, 3, 4);

final Map<Integer, String> keyValue = Map.of(1, “Amit”,

2, “Ali”,

3, “Mindorks”);

kotlin代码:

val listOfNumber = listOf(1, 2, 3, 4)

val keyValue = mapOf(1 to “Amit”,

2 to “Ali”,

3 to “Mindorks”)

        1.1.9 方法定义


java代码:

void doSomething() {

// todo here

}

void doSomething(int… numbers) {

// todo here

}

kotlin代码:

fun doSomething() {

// logic here

}

fun doSomething(vararg numbers: Int) {

// logic here

}

        1.2.0 类型判断和转换(隐式)


java代码:

if (object instanceof Car) {

Car car = (Car) object;

}

kotlin代码:

if (object is Car) {

var car = object // 智能转换

}

  1.3 点击事件


java代码:

btnLogin.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

}

});

kotlin代码:

btn_login.setOnClickListener(View.OnClickListener { })

  1.4 精简空判断


        1.4.1 防空判断


java代码:

if(a != null && a.b != null && a.b.c != null) {

String user = a.b.c.d;

}

kotlin代码:

val user = a?.b?.c?.d

        1.4.2 空判断


java代码:

if (str != null) {

int length = str.length();

}

kotlin代码:

str?.let {

val length = str.length

}

// 更简单的写法

val length = str?.length

// 为null赋予默认值

val length = str?.length?:0

                1.5.4.1 带返回值的方法


java代码:

int getScore() {

// logic here

return score;

}

kotlin代码:

fun getScore(): Int {

// logic here

return score

}

// 单表达式函数

fun getScore(): Int = score

                1.5.4.1 无结束符号


java代码:

int getScore(int value) {

// logic here

return 2 * value;

}

kotlin代码:

fun getScore(value: Int): Int {

// logic here

return 2 * value

}

// 单表达式函数

fun getScore(value: Int): Int = 2 * value

       1.5.5 单表达式函数


java代码:

public double cube(double x) {

return x * x * x;

}

kotlin代码:

fun cube(x: Double) : Double = x * x * x

       1.5.6 可变参数数量(vararg)


java代码:

public int sum(int… numbers) { }

kotlin代码:

fun sum(vararg x: Int) { }

       1.5.7 Main函数


java代码:

public class MyClass {

public static void main(String[] args){

}

}

kotlin代码:

fun main(args: Array) {

}

       1.5.8 参数命名


java代码:

public static void main(String[]args){

openFile(“file.txt”, true);

}

public static File openFile(String filename, boolean readOnly) { }

kotlin代码:

fun main(args: Array) {

openFile(“file.txt”, readOnly = true)

}

fun openFile(filename: String, readOnly: Boolean) : File { }

       1.5.9 方法重载(可选参数)


java代码:


java代码:

public class MyClass {

public static void main(String[] args){

}

}

kotlin代码:

fun main(args: Array) {

}

       1.5.8 参数命名


java代码:

public static void main(String[]args){

openFile(“file.txt”, true);

}

public static File openFile(String filename, boolean readOnly) { }

kotlin代码:

fun main(args: Array) {

openFile(“file.txt”, readOnly = true)

}

fun openFile(filename: String, readOnly: Boolean) : File { }

       1.5.9 方法重载(可选参数)


java代码:



这篇关于kotlin与java在Android开发中的简单对比(正在更新中),快手android面试经验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程