본문 바로가기
Android

[Kotlin] Android Java 와 Kotlin 몇가지 문법 비교

by bryan.oh 2023. 7. 24.
반응형

Java

Integer.parseInt(someValue)

 

Kotlin

someValue.toInt()

 


 

Java

if ( "100".equals(someValue) ) {  }

Kotlin

if( someValue == "100" ) { }

 


 

Java

Object someValue = new String("hello");
if ( someValue instanceof String ) {
    // OK
}

Kotlin

var someValue: Any = "hello"
if ( someValue is String ){
    // OK
}

 


Java

class MainActivity : AppCompatActivity() {

Kotlin

class MainActivity : AppCompatActivity() {

 


Java

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.onClickListener(){
    @override
    public void onClick(View view){
        Toast.makeText(getApplicationContext(), "Hello~ Bryan~!", Toast.LENGTH_LONG).show();
    }
});

Kotlin

binding.button.setOnClickListener {
    Toast.makeText(applicationContext, "Hello~ Bryan~!", Toast.LENGTH_LONG).show()
}

 


Java

public void show(String message) { ... }

Kotlin

fun show(message: String) { ... }

 


 

Java

public int add(int a, int b) {
    return a + b;
}

Kotlin

fun add(a:Int, b:Int):Int {
    return a + b
}
fun add(a:Int, b:Int):Int = a + b
fun add(a:Int, b:Int) = a + b
val add = { a:Int, b:Int -> a + b }

 

 

 

728x90
반응형

댓글