kotlin java 执行效率为什么一定要初始化

你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
回复了问题 o 3 人关注 o 2 个回复 o 603 次浏览 o
o 来自相关话题
回复了问题 o 2 人关注 o 1 个回复 o 485 次浏览 o
o 来自相关话题
回复了问题 o 1 人关注 o 1 个回复 o 75 次浏览 o
o 来自相关话题
回复了问题 o 3 人关注 o 2 个回复 o 370 次浏览 o
o 来自相关话题
发表了文章 o 0 个评论 o 278 次浏览 o
o 来自相关话题
目前国内唯一一套 Kotlin 入门的视频教程,大家可以关注下,比较有趣。
目前国内唯一一套 ,大家可以关注下,比较有趣。
发表了文章 o 0 个评论 o 1000 次浏览 o
o 来自相关话题
1. 如何评价 Kotlin 语言? 来自知乎,可以看看广大道友是如何评价这个新生代的,不过好不好用还是要看自己
2. Kotlin 官网 英文版,里面的东西很全,但是不是很详细,用来入门是没什么问题了
3. Kotlin 官网 - 中文版 这个和上面一样,只不过是中文的,有需要的可以看一看
4. Kotlin 中文翻译项目
这个在本人爬坑的时候也参考了一部分,只不过好像项目停止更新了
姑且先这么多吧,以后友好的资源再拿来分享(PS : 难道只有我一个人在吐槽不能用MD吗?)
来自知乎,可以看看广大道友是如何评价这个新生代的,不过好不好用还是要看自己
英文版,里面的东西很全,但是不是很详细,用来入门是没什么问题了
这个和上面一样,只不过是中文的,有需要的可以看一看
这个在本人爬坑的时候也参考了一部分,只不过好像项目停止更新了
姑且先这么多吧,以后友好的资源再拿来分享(PS : 难道只有我一个人在吐槽不能用MD吗?)
发起了问题 o 1 人关注 o 0 个回复 o 509 次浏览 o
o 来自相关话题
发表了文章 o 1 个评论 o 1394 次浏览 o
o 来自相关话题
目前Kotlin的中文文档比较少,我在Github上找一份更新勤的项目:/huanglizhuo ... e.git。
web网站地址:
目前Kotlin的中文文档比较少,我在Github上找一份更新勤的项目:。
web网站地址:
发表了文章 o 0 个评论 o 687 次浏览 o
o 来自相关话题
项目地址:/BennyWang/KBinding
Android View Model binding framework write in kotlin, base on anko, simple but powerful.ContentsBindingMode
OneWay: Binding from model to viewTwoWay: Binding from model to view and view to modelOneWayToSource: Binding from view to modelOneTime: Binding from model to view, and auto release after first emit
Simple BindingverticalLayout {
editText {
bind { text(&name&, mode = TwoWay) }
bind { click(&hello&) }
class SimpleViewModel() : ViewModel() {
var name: String by bindProperty(&name&) { &Jason& }
val hello: Command by bindCommand(&hello&) { params, canExecute -&
toast(&Hello, ${name}!&)
}Multiple Binding//login button enabled only when name and password not empty
class ArrayToBooleanConverter : MultipleConverter&Boolean& {
override fun convert(params: Array&Any&): Boolean {
params.forEach {
if(it.toString().isEmpty()) return false
return true
verticalLayout {
editText {
bind { text(&name&, mode = TwoWay) }
editText {
bind { text(&password&, mode = TwoWay) }
bind { enabled(&name&, &password&, mode = OneWay, converter = ArrayToBooleanConverter()
bind { click(&login&) }
class LoginViewModel() : ViewModel() {
var name: String by bindProperty(&name&) { && }
var password: String by bindProperty(&password&) { &xxxxxx& }
val login: Command by bindCommand(&login&) { params, canExecute -&
//login processing
}View Model property depends on other properties//name and price property will be updated when new stock is set
class StockViewModel() : ViewModel() {
var stock: Stock? by bindProperty(&stock&)
val name: String? by bindProperty(&name&, &stock&) { stock!!.name }
val price: Float by
bindProperty(&price&, &stock&) { stock!!.price }
}Wait/Until//wait/until just like OneTime binding, but it need apply action, for example below, it wait for market from model, then decide how to display
relativeLayout {
wait { until(&market&, converter = viewOfMarket) { inflate(it, this@verticalLayout) }
}Extend Binding Property(Depend on RxBinding heavily)Event
fun View.click(path: String) : PropertyBinding = commandBinding(path, clicks(), enabled())Propertyfun View.enabled(vararg paths: String, mode: OneWay = BindingMode.OneWay, converter: OneWayConverter&Boolean& = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(enabled(), false, converter, *paths)
//this implements four binding mode for TextView, if just need OneWay mode, remove last three lines, some for other mode
fun TextView.text(vararg paths: String, mode: OneWay = BindingMode.OneWay, converter: OneWayConverter&out CharSequence& = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(text(), false, converter, *paths)
fun TextView.text(vararg paths: String, mode: OneTime, converter: OneWayConverter&out CharSequence& = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(text(), true, converter, *paths)
fun TextView.text(path: String, mode: OneWayToSource, converter: OneWayConverter&*& = EmptyOneWayConverter&String&()) : PropertyBinding = oneWayPropertyBinding(path, textChanges2(), convert)
fun TextView.text(path: String, mode: TwoWay, converter: TwoWayConverter&String, *& = EmptyTwoWayConverter&String, String&()) : PropertyBinding = twoWayPropertyBinding(path, textChanges2(), text(), converter)
Using with Gradledependencies {
compile 'com.benny.kbinding:library:0.1.0'
}Contribute
Now is just the beginning of KBinding, so everyone interested in this library, just fork it and pull requests to me. Let's make it a little better.
Discussion
项目地址:
Android View Model binding framework write in kotlin, base on anko, simple but powerful.ContentsBindingMode
OneWay: Binding from model to viewTwoWay: Binding from model to view and view to modelOneWayToSource: Binding from view to modelOneTime: Binding from model to view, and auto release after first emit
Simple BindingverticalLayout {
editText {
bind { text(&name&, mode = TwoWay) }
bind { click(&hello&) }
class SimpleViewModel() : ViewModel() {
var name: String by bindProperty(&name&) { &Jason& }
val hello: Command by bindCommand(&hello&) { params, canExecute -&
toast(&Hello, ${name}!&)
}Multiple Binding//login button enabled only when name and password not empty
class ArrayToBooleanConverter : MultipleConverter&Boolean& {
override fun convert(params: Array&Any&): Boolean {
params.forEach {
if(it.toString().isEmpty()) return false
return true
verticalLayout {
editText {
bind { text(&name&, mode = TwoWay) }
editText {
bind { text(&password&, mode = TwoWay) }
bind { enabled(&name&, &password&, mode = OneWay, converter = ArrayToBooleanConverter()
bind { click(&login&) }
class LoginViewModel() : ViewModel() {
var name: String by bindProperty(&name&) { && }
var password: String by bindProperty(&password&) { &xxxxxx& }
val login: Command by bindCommand(&login&) { params, canExecute -&
//login processing
}View Model property depends on other properties//name and price property will be updated when new stock is set
class StockViewModel() : ViewModel() {
var stock: Stock? by bindProperty(&stock&)
val name: String? by bindProperty(&name&, &stock&) { stock!!.name }
val price: Float by
bindProperty(&price&, &stock&) { stock!!.price }
}Wait/Until//wait/until just like OneTime binding, but it need apply action, for example below, it wait for market from model, then decide how to display
relativeLayout {
wait { until(&market&, converter = viewOfMarket) { inflate(it, this@verticalLayout) }
}Extend Binding Property(Depend on RxBinding heavily)Event
fun View.click(path: String) : PropertyBinding = commandBinding(path, clicks(), enabled())Propertyfun View.enabled(vararg paths: String, mode: OneWay = BindingMode.OneWay, converter: OneWayConverter&Boolean& = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(enabled(), false, converter, *paths)
//this implements four binding mode for TextView, if just need OneWay mode, remove last three lines, some for other mode
fun TextView.text(vararg paths: String, mode: OneWay = BindingMode.OneWay, converter: OneWayConverter&out CharSequence& = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(text(), false, converter, *paths)
fun TextView.text(vararg paths: String, mode: OneTime, converter: OneWayConverter&out CharSequence& = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(text(), true, converter, *paths)
fun TextView.text(path: String, mode: OneWayToSource, converter: OneWayConverter&*& = EmptyOneWayConverter&String&()) : PropertyBinding = oneWayPropertyBinding(path, textChanges2(), convert)
fun TextView.text(path: String, mode: TwoWay, converter: TwoWayConverter&String, *& = EmptyTwoWayConverter&String, String&()) : PropertyBinding = twoWayPropertyBinding(path, textChanges2(), text(), converter)
Using with Gradledependencies {
compile 'com.benny.kbinding:library:0.1.0'
}Contribute
Now is just the beginning of KBinding, so everyone interested in this library, just fork it and pull requests to me. Let's make it a little better.
Discussion
发表了文章 o 1 个评论 o 541 次浏览 o
o 来自相关话题
我们很高兴又呈现了Kotlin 1.0 的Beta版本。我们正在努力完成标准库和摆脱语言中过时的旧结构、以及修复Bug、性能提升、面向未来的检查。
全部的修改列表在此
查看已关闭的提问在此
一、库变更
我们努力让标准库在1.0之前进入最佳状态,这会涉及一些实验部分,所以出现了功能的增删,我们计划在1.0build(或者rc)版本中做最后的清理:移除所有的过时和传统部分。
这里我们只从所有修改中给出一个亮点:`contains()`和其他相似的扩展,现在集合接受元素的超类。// ns: String?
// cs: CharSequence
strs.contains(ns) // accepted now
strs.contains(cs) // accepted now
strs.contains(i) // ERROR (in fact, a deprecation warning, but will be an error soon)我们发现之前建议的`containsRow`方式低效。而选择采用`contains()`更宽容一些,同时又保持了最初的安全意图。注意集合的接口本身是完整的,而这一切是通过扩展函数完成。使用`Code Cleanup`迁移你的代码。
二、语言修改
一些语言修改的亮点,全部列表在此
很多之前是过时的现在已经变成错误,使用`Code Cleanup`来迁移。
1. When 表达式
这类代码已经证明是有问题的,所以我们决定让他过时。when {
foo.isValid(), foo.isReady() -& process(foo)
}很多人以为条件`foo.isValid(), foo.isReady()`代表foo是volid和read同时满足的,但是实际是这个逗号是表示`or`,解决方法十分简单:使用 `||`代替:when{
foo.isValid() || foo.isReady() -& process(foo)
}`Code Cleanup`将会迁移你的代码。
一个Bug被解决,他是防止我们以注解参数使用数组时为默认值:annotation class Entry(val value: String)
annotation class Example(
val entries: Array&Entry& = arrayOf(Entry(&a&), Entry(&b&)) // OK now
)3.Enum.values()
最近,我们修改了传统的Java的`Enum.values()`成为一个属性:`Enum.values`,但是现在我们回滚这个修改。因为这里有一个不愉快的细节用例:在枚举中可能会有一个叫values的常量,同时这里没有办法做到二取一。我们考虑不同的意见后,决定在这次清理中将`values`更改回一个方法。
所以,`values`属性是过时的,`values()`方法是不过时的。
4.可见性与作用域规则
你们清理并修复了可见性和作用域规则,所以:
pretected`成员在伴生对象是允许。从子类中调用伴生对象的非`@JvmStatic protected`成员会标记成错误(不支持)开放属性的`private`级别 setter 已经弃用。本地`sealed`类弃用(从来不是可用)覆盖setter不用削弱可见性内部类不再允许内嵌枚举中在lambda、对象常量、本地方法中使用没有初始化的变量是禁止的
三、 Android扩展
我们已经合并了`Kotlin plugin for IntelliJ IDEA`和`Kotlin Extensions For Android`插件。
同样,我们也增加了对Android的`product flavors`的支持,现在不同渠道的属性可以在不同的包中。productFlavors {
versionName &1.0-free&
versionName &1.0-pro&
}例如,如果我们在build.gradle文件中有两个渠道。
我们现在不仅在`main`代码堆中的布局,同时在渠道布局中也能使用合并的属性。// Import synthetic properties for the `activity_free.xml` layout in the `free` flavor
import kotlinx.android.synthetic.free.activity_free.versionMarker
class FreeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_free)
versionMarker.text = &Free version&
}注意,main代码集合堆中的布局现在都在`kotlinx.android.synthetic.main`包中,旧的命名约定已经过时。
四、IDE中有什么新的
1. Android的扩展插件已经合并到Kotlin插件中,不再需要单独安装。
2. 当新建一个Gradle项目时,我们已经添加一个Kotlin选项:
3. 调试:堆栈跟踪导航现在支持内联函数中的栈帧,同样步入内联函数时也有一些提高。
4. 增加了三种属性的快速修复
5. 引入变量(Ctrl + Alt + V / Cmd + Alt + V)现在支持多声明表达式。
6. 同样他允许选择lambda表达式和匿名函数的容器
7. Beta3给String模板片段带来引入变量、参数、属性、函数
8. 最后,一个试验性的特征已经添加——在IDE中添加对Kotlin脚本文件最基本的支持。
原文地址: /kotl ... -out/
我们很高兴又呈现了Kotlin 1.0 的Beta版本。我们正在努力完成标准库和摆脱语言中过时的旧结构、以及修复Bug、性能提升、面向未来的检查。
全部的修改列表
查看已关闭的提问
一、库变更
我们努力让标准库在1.0之前进入最佳状态,这会涉及一些实验部分,所以出现了功能的增删,我们计划在1.0build(或者rc)版本中做最后的清理:移除所有的过时和传统部分。
这里我们只从所有修改中给出一个亮点:`contains()`和其他相似的扩展,现在集合接受元素的超类。// ns: String?
// cs: CharSequence
strs.contains(ns) // accepted now
strs.contains(cs) // accepted now
strs.contains(i) // ERROR (in fact, a deprecation warning, but will be an error soon)我们发现之前建议的`containsRow`方式低效。而选择采用`contains()`更宽容一些,同时又保持了最初的安全意图。注意集合的接口本身是完整的,而这一切是通过扩展函数完成。使用`Code Cleanup`迁移你的代码。
二、语言修改
一些语言修改的亮点,全部列表
很多之前是过时的现在已经变成错误,使用`Code Cleanup`来迁移。
1. When 表达式
这类代码已经证明是有问题的,所以我们决定让他过时。when {
foo.isValid(), foo.isReady() -& process(foo)
}很多人以为条件`foo.isValid(), foo.isReady()`代表foo是volid和read同时满足的,但是实际是这个逗号是表示`or`,解决方法十分简单:使用 `||`代替:when{
foo.isValid() || foo.isReady() -& process(foo)
}`Code Cleanup`将会迁移你的代码。
一个Bug被解决,他是防止我们以注解参数使用数组时为默认值:annotation class Entry(val value: String)
annotation class Example(
val entries: Array&Entry& = arrayOf(Entry(&a&), Entry(&b&)) // OK now
)3.Enum.values()
最近,我们修改了传统的Java的`Enum.values()`成为一个属性:`Enum.values`,但是现在我们回滚这个修改。因为这里有一个不愉快的细节用例:在枚举中可能会有一个叫values的常量,同时这里没有办法做到二取一。我们考虑不同的意见后,决定在这次清理中将`values`更改回一个方法。
所以,`values`属性是过时的,`values()`方法是不过时的。
4.可见性与作用域规则
你们清理并修复了可见性和作用域规则,所以:
pretected`成员在伴生对象是允许。从子类中调用伴生对象的非`@JvmStatic protected`成员会标记成错误(不支持)开放属性的`private`级别 setter 已经弃用。本地`sealed`类弃用(从来不是可用)覆盖setter不用削弱可见性内部类不再允许内嵌枚举中在lambda、对象常量、本地方法中使用没有初始化的变量是禁止的
三、 Android扩展
我们已经合并了`Kotlin plugin for IntelliJ IDEA`和`Kotlin Extensions For Android`插件。
同样,我们也增加了对Android的`product flavors`的支持,现在不同渠道的属性可以在不同的包中。productFlavors {
versionName &1.0-free&
versionName &1.0-pro&
}例如,如果我们在build.gradle文件中有两个渠道。
我们现在不仅在`main`代码堆中的布局,同时在渠道布局中也能使用合并的属性。// Import synthetic properties for the `activity_free.xml` layout in the `free` flavor
import kotlinx.android.synthetic.free.activity_free.versionMarker
class FreeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_free)
versionMarker.text = &Free version&
}注意,main代码集合堆中的布局现在都在`kotlinx.android.synthetic.main`包中,旧的命名约定已经过时。
四、IDE中有什么新的
1. Android的扩展插件已经合并到Kotlin插件中,不再需要单独安装。
2. 当新建一个Gradle项目时,我们已经添加一个Kotlin选项:
3. 调试:堆栈跟踪导航现在支持内联函数中的栈帧,同样步入内联函数时也有一些提高。
4. 增加了三种属性的快速修复
5. 引入变量(Ctrl + Alt + V / Cmd + Alt + V)现在支持多声明表达式。
6. 同样他允许选择lambda表达式和匿名函数的容器
7. Beta3给String模板片段带来引入变量、参数、属性、函数
8. 最后,一个试验性的特征已经添加——在IDE中添加对Kotlin脚本文件最基本的支持。
原文地址:
0 个问题, 0 次赞同
0 个问题, 0 次赞同
0 个问题, 0 次赞同linux嵌入式方向(465)
作者:华清远见讲师
在上个月的博文中,我们介绍了如何使用Kotlin语言来进行Android开发,那么在本篇文章中,介绍一下Kotlin语言的简单语法。详细的语法可以访问其官方网站。
1、方法的使用
定义一个方法使用fun关键字,如下所示:
fun add(a: Int, b: Int): Int {
return a + b
方法add有两个Int型的参数,冒号后跟的是方法的返回值,一条代码语句的末尾不用加分号,当然加上分号也没有问题。
上面的add方法还可以简写成如下形式:
fun add(a: Int, b: Int) = a +
没有显式指定方法的返回值,会自动推断方法的返回值。
如果一个方法没有返回值,可以写成如下两种形式:
//没有返回值的方法,显式指定Unit为返回值
fun showAddResult(a: Int, b: Int): Unit {
println(a + b)
//没有返回值的方法,省略Unit的写法
fun showAddResult2(a: Int, b: Int) {
println(a + b)
2、常量和变量
使用val关键字声明一个常量(只读,不可修改),使用var关键字声明一个变量,下面是具体用法:
fun test() {
//使用val关键字声明一个常量(只读),声明常量时必须初始化
val a: Int = 1 //显式指定常量的类型
val b = 2 //自动推断类型
val c: Int //声明一个不初始化的常量,必须显式指定类型
// b = 3 //常量值不可修改,这句代码会报错
//a = 3 //不可以修改常量的值,此句代码会报错
//使用var关键字声明一个变量,变量的值可以修改
var year: Int = 2016 //显式指定变量的类型
var month = 5 //自动推断变量类型
var day: Int //声明一个不初始化的变量,必须显式指定类型
month = 6 //变量值可以被修改
Kotlin中的注释和Java中类似,如下所示:
fun comments() {
// 注释一行代码
// var s = &hello world&
/*注释一段代码
a++
和Java不同的是,Kotlin中的注释可以嵌套。
4、字符串模板
//字符串模板的用法
fun stringTempl(args: Array) {
if(args.size & 0)
println(&args[0] = ${args[0]}&)
//main方法是整个程序的入口
fun main(args: Array){
var arr = arrayOf(&hello&, &world&)
stringTempl(arr)
上面的代码执行后,在控制台打印如下内容:
5、条件表达式
常规的条件表达式可以是这么写的:
//常规写法的条件表达式,这里的方法返回值不能省略
fun max(a: Int, b: Int): Int {
Kotlin可以简写条件表达式,如下所示:
//简写的条件表达式
fun max2(a: Int, b: Int) = if(a & b) a else b
6、可空类型
fun nullableTest() {
//在变量类型后面加上问号,代表该变量是可空变量
var name: String? = &zhangsan&
name = null //可以将null赋值给name变量
var person: String = &tom&
// person = null //这句代码会报错,不可以将null赋值给一个不可空变量
方法返回值为可空的例子如下代码:
方法返回值为Int?,表示返回值可为空
当参数为空或者为&&时,则返回null,否则使用Java中的字符串转整型的方法
这里也体现了kotlin代码和Java代码无缝集成
fun parseInt(s: String): Int? {
if(s == null || s == &&)
return Integer.parseInt(s);
7、类型检查和自动类型转换
Kotlin中使用is运算符来检查数据类型和做类型转换,如下代码所示:
当方法参数为字符串类型时,就返回字符串的长度,否则返回空
fun getStringLength(n: Any): Int? {
if(n is String)
return n.length //这里会自动将n转化为字符串类型
return null
上面的代码还可以写成:
当方法参数为字符串类型时,就返回字符串的长度,否则返回空
fun getStringLength(n: Any): Int? {
if(n !is String)
return null
return n.length //这里会自动将n转化为字符串类型
8、for循环和while循环
//for循环的测试代码
fun testFor() {
var arr = arrayOf(1, 3, 4, 5, 6)
for(i in arr.indices) { //通过索引循环
println(arr[i])
for(num in arr) { //直接使用数组中的对象循环
println(num)
//while循环的测试代码
fun testWhile() {
var i = 0;
while(i & 10) {
print(& & + i)
i++
9、when表达式
when表达式就类似于Java中的switch表达式,如下代码所示:
fun main(args: Array) {
testCase(&hello world&)
fun testCase(obj: Any) {
when(obj) {
is String -& {
print(&this is string&)
is Int -& {
print(&this is integer&)
print(&unkown value&)
10、ranges的使用
(1)使用in操作符检查一个数是否在某个范围内
判断分数是否大于等于90,小于等于100
fun isGood(score: Int) {
if(score in 90..100) //ranges是闭区间
println(&very good&)
println(&not so good&)
(2)检查索引是否越界
检查index是否在数组arr的索引范围内
fun checkIndex(index: Int, arr: Array) {
if(index in 0..arr.lastIndex) //arr.lastIndex返回的是数组的最后一位的下标
println(&index in bounds&)
println(&index out of bounds&)
(3)遍历一个范围
for(i in 1..5) {
println(i)
也可以通过in运算符遍历一个集合,如下代码:
//in运算符遍历一个字符串数组
fun testStr(arr: Array) {
for(str in arr)
println(str)
至此,简单的介绍了一下Kotlin语言的基础语法供读者来稍作了解,如果喜欢这门新型的轻量化语言,可以自行深入学习,熟练的使用Kotlin语言可以使Android程序代码变得更高效简洁,并且其高兼容性也不存在项目对接等兼容性问题。
文章选自华清远见嵌入式培训
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:870470次
积分:16847
积分:16847
排名:第455名
原创:785篇
评论:212条
(2)(18)(25)(23)(12)(13)(2)(1)(2)(1)(1)(7)(5)(3)(1)(2)(1)(2)(3)(1)(5)(2)(13)(4)(3)(4)(2)(3)(1)(2)(1)(6)(4)(5)(3)(4)(3)(3)(2)(4)(2)(4)(4)(4)(5)(3)(6)(4)(5)(5)(12)(10)(6)(6)(13)(2)(2)(9)(13)(3)(12)(20)(16)(10)(8)(15)(15)(13)(15)(17)(18)(12)(14)(20)(27)(6)(19)(23)(18)(22)(35)(30)(23)(34)(35)

我要回帖

更多关于 kotlin java 执行效率 的文章

 

随机推荐