随着Kotlin越来越火,最近开始学习Kotlin,感觉Java和区别还是很大的,这里列举一下Kotlin和Java的一些区别;
1.打印日志
Java
System.out.print("hello world");
System.out.println("hello world");
kotlin
print("hello world")
println("hello world")
2.定义变量
可变变量 String name = "xxxx";
不可变变量 final String name = "aaaa";
kotlin
可变变量 var name= "xxxx"
不可变变量val name = "aaaaa"
3.null声明
Java
String name;
name = null;
kotlin
var name: String?
name = null
4.空判断
Java
if(text !=null){
int length = text.length();
}
kotlin
text?.let{
val length = text.length
}
或者
val length = text?.length
5.字符串拼接
Java
String firstName = "apple";
Stirng lastName = "enginner";
String message = "My name is:"+firstName + lastName;
kotlin
val firstName= "apple"
val lastName="enginner"
val message = "My name is:$firstName $lastName"
6.三元表达式
Java
String text = x > 5 ? "x > 5" : "x <= 5";
kotlin
val text = if(x > 5)
" x > 5"
else
"x <= 5"
7.多重表达式
Java
if(score >= 0 && score <= 300){}
Kotlin
if(score in 0..300){}
8.更灵活的case语句
Java
int score = 100;
String grade;
switch(score){
case 1:
...
case 100:
grade="100";
break;
default:
break;
}
kotlin
var score = 100;
var grade = when(score){
9,10 -> "excellent"
in 6..8 -> "Good"
4,5->"ok"
in 1..3->"fail"
else -> "fail"
}
9.for循环
Java
for (int i = 1; i <= 10 ; i++) { }
for (int i = 1; i < 10 ; i++) { }
for (int i = 10; i >= 0 ; i--) { }
for (int i = 1; i <= 10 ; i+=2) { }
for (int i = 10; i >= 0 ; i-=2) { }
for (String item : collection) { }
for (Map.Entry<String, String> entry: map.entrySet()) { }
Kotlin
for(i in 1..10){}
for(i in 1 until 10){}
for(i in 10 downTo 0){}
for(i in 1 ..10 step 2){}
for(item in collection){}
for((key,value) in map){}
10.集合操作
Java
final List<Integer> listOfNumber = Arrays.asList(1,2,3,4);
final Map<Integer,String> keyvalue = new HashMap<Integer,String>();
map.put(1,"Android");
map.put(2,"Ali");
kotlin
val listOfNumber = listOf(1,2,3,4)
val keyvalue = mapof(1 to "Android",
2 to "Ali",
3 to "Mindorks")
11.遍历
Java
for (String s: stringList) {
System.out.println(s);
}
stringList.forEach(s -> System.out.println(s));
kotlin
fun A(stringList: List<String>) {
stringList.forEach { s -> println(s) }
}
12.方法定义
Java
void doSomeThing(){}
void doSomeThing(int a,int b){}
kotlin
fun doSomeThing(){}
fun doSomeThing(a : I}nt,b : Int){
14.constructor构造器
Java
public class A{
private A(){}
public static int getScore(int value){
return 2*value;
}
}
kotlin
class A private constructor(){
companion object{
fun getScore(int: Int):Int{
return 2*int
}
}
}
13.Get Set构造器
Java
public class Teacher {
private String name;
private int age;
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
kotlin
data class Student(val name:String,val age : Int)
fun main() {
var stu = Student("XiaoMin",24)
println(stu.name)
println(stu.age)
}
15.操作符
Java
final int and = a&b;
final int or = a | b;
final int xor = a ^b;
final int right = a >>2;
final int left = a << 2;
final int unsingedRightShift = a >> 2;
kotlin
val and = a and b
val or = a or b
val xor = a xor b
val rightShift = a shr 2
val leftShift = a shl 2
val unsignedRightShift = a ushr 2