Kotlin notes (ii): Kotlin vs. Java __java

Source: Internet
Author: User
Tags ranges

In this article we will learn the basic syntax and common operation Basic types of Kotlin by analogy with Java syntax

Java Kotlin bit width
Double Double 64
Float Float 32
Long Long 64
Int Int 32
Short Short 16
Byte Byte 8

Note: Char is not a basic data type in Kotlin, is a standalone data type string string representing Java

String name = "Java";
String sex = "man";
Kotlin

A string can contain a template expression that evaluates to a code fragment and connects its results to a string. A template expression starts with a $ and contains another simple name

Val name = "Kotlin"
val sex = "Man"
val link = "name is $name, sex is: $sex"
Print OperationsJava
System.out.print ("Java");
System.out.println ("Java");
Kotlin
Print ("Kotlin")
println ("Kotlin")
Line Wrap OperationJava
String Text = "First row \ n second row \ n third row \ n";
Kotlin

There are two types of literal literals: one that can be separated by a delimiter. As with Java, it is not possible to include a separator but can contain other characters. Wrapped by three quotes ("")

Val Text1 = "First row \ n second row \ n";

val Text = ""
| First line
| second Line
| third line ""
". Trimmargin ()   //Remove space
Constants and variable definitionsJava
Final String s = "Java";
String name = "Java";
int num = 5;
Kotlin

VAR declaration variable
Val declares a constant

val s = "Java"
var name = "Kotlin"
var num = 5
declaring static constants, methodsJava
Static final String name = "Name";
static final int SEX = 0;

static void Test () {}
Kotlin

When declaring a static constant or method, you declare it by using the following method

Companion object {
    internal val NAME = "name"
    internal val SEX = 0

    Fun Test () {}
}
conditional Expression If statementJava
if (A = = = b) {}
if (a>=0 && a <=10) {}
if (a>0 && a <10) {}
Kotlin
if (a== b) {}
if (a in 0..10) {}        //also can be in Java notation if (a>=0 && a <=10) {}
if (a in 1 until)      You can also follow the Java notation if (a>0 && a <10) {}

Reference ranges using ternary operator Java

int num = a > B? A:B;
Kotlin
Val num = if (a > B) a else B
Case ExpressionJava
String s;
Switch (a) {case
    0:
        s =  "a = 0";
        break;
    Case 1:
        s = "a = 1";
        break;
    Default:
        s = "Default";
        break;
Kotlin
val s = When (a) {
    0-> "a = 0"
    1-> "a = 1"
    else-> "default"
}
For Loop Iteration OperationJava
for (int i = 0; I <= i++) {} for

(int i = 0; i < i++) {} for

(int i = i < 0; i--) {} for (

int i = 0; I <= 10; i+=2) {}

for (int i = i < 0; i-=2) {}

to (String s:liststring) {}

for (map.entry<string, string> Entry:map.entrySet ()) {}
Kotlin

The operation used below uses the Kotlin ranges to use in the specified range until does not contain the Downto reverse step to specify the number of steps to jump

For (I-in 0.) {} for

(i-0 until) {} for

(i-Downto 0) {} for

(I-in 0.

Wnto 0 Step 2) {}

for (item in liststring) {}

for ((key, value) in map) {}

Reference ranges using collections to manipulate Java

list<string> liststring = Arrays.aslist ("A", "B", "C");
Liststring.add ("D");

map<string,string> map = new hashmap<> ();
Map.put ("A", "1");
Map.put ("B", "2");
Map.put ("C", "3");
Kotlin
var liststring = Listof ("A", "B", "C")
Liststring.add ("D")

var map = mapof ("A" to "1",
               "B" to "2",
               "C" to "3 ")
traversing collections and filtering actionsJava
for (String s:liststring) {
     System.out.println (s);
}

Filter for
(String s:liststring) {
    if (s.equals ("a")) {
        System.out.println (s)
    }
}
Kotlin
Liststring.foreach {
    println (it)
}

//filter
liststring.filter{it.equals ("a")}
        . foreach{
            println (IT)
        }
declaring Methods

Java with no return value method

void Test () {}

void Test (int a, String b) {}
Kotlin
Fun Test () {}

Fun Test (A:int, b:string) {}

Java with return value method

int Test () {return 0;}

int Test (int a, int b) {
    if (a > B) {return
        A;
    }
    return b;
}
Kotlin
Fun Test (): Int {return 0}

fun Test (A:int, b:int): int{
    if (a > B) {
    } return
    b
}
Construction MethodJava
Class test{Public
    test () {} public
    test (int a, int b) {}
}
Kotlin
Class Test {
    constructor () {}
    constructor (A:int, B:int) {}
}

or write that.

Class Test Constructor () {   //constructor can omit
    constructor (A:int, B:int): this () {}
}}
generate get and set methodsJava
Class test{
    String A;
    int b;

    Public Test (String A, int b) {
        this.a = A;
        this.b = b;
    }

    Public String Geta () {return
        A;
    }

    public void SetA (String a) {
        this.a = A;
    }

    public int Getb () {return
        B;
    }

    public void Setb (int b) {
        this.b = b;
    }

}
Kotlin

Just one line of code

Data Class Test (Val a:string, Val b:int) {}

After reading the above article, we can find that Kotlin syntax is more intuitive and convenient than Java, but it still takes some time to adapt to the Kotlin grammar habits.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.