kotlin能和java混用吗

如题所述

可以的,Kotlin与Java是兼容的。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-09-02
上代码,其实就是在gradle中加入kotlin的插件就好了.

首先是project 的gradle文件
buildscript {
ext.kotlin_version = "1.0.1" //加上这个,至于为什么是1.0.1待会解释
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0-alpha4'//这个不用管

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
//加上这个⬆️
}
}

然后是module的gradle文件
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' //加上插件
...
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" //加上库

}

代码是在android中运行的

kotlin代码
data class Student(val name:String,val sex:Char,val age:Int,val course:Array<String>){

}

java调用代码
public void demo() {
Student[] students = new Student[]{
new Student("x1",'男',14,new String[]{"数学","语文"}),
new Student("x2",'女',15,new String[]{"英语","语文"}),
new Student("x3",'男',16,new String[]{"化学","语文"}),
new Student("x4",'女',17,new String[]{"物理","语文"}),
};
for (Student student : students) {
Log.v(TAG,student.toString());
}
}本回答被提问者采纳
第2个回答  2017-07-26
可以。
jvm语言,都编译成class字节码就行。