Java ve Kotlin Syntax Farklılıkları ve Örnekleri

Java ve Kotlin Syntax Farklılıkları ve Örnekleri

Java ve Kotlin Syntax Farklılıkları ve Örnekleri

Kotlin ve Java Syntax benzerlikleri ve farklılıkları

Java ve Kotlin Syntax Farklılıkları ve Örnekleri

Java ve Kotlin Syntax Farklılıkları ve Örnekleri

Örnek 1: Class ve Method kullanımı

Java

public class MainActivity extends AppCompatActivity{

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

}

Kotlin

class MainActivity : AppCompatActivity{

override fun onCreate(savedInstanceState: Bundle?){

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

}

}

Örnek 2: Android Nesneleri kullanımı

Java

public class MainActivity extends AppCompatActivity{

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

FloatingActionButton myfab = (FloatingActionButton) findViewById(R.id.myfab);

myfab.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View view){

Snackbar.make(view, "This is a snackbar",Snackbar.LENGTH_LONG).setAction("Action",null).show();

}

}

}

}

Kotlin

class MainActivity : AppCompatActivity{

override fun onCreate(savedInstanceState: Bundle?){

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

val toolbar = findViewById(R.id.toolbar) as Toolbar

setSupportActionBar(toolbar)

val myfab = findViewById(R.id.myfab) as FloatingActionButton

myfab.setOnClickListener { view -> Snackbar.make(view,"This is a snackbar", Snackbar.LENGTH_LONG).setAction("Action",null).show()

}

}

Örnek 3: Button Click Event

Java

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button next = (Button) findViewById(R.id.btn01);

next.setOnClickListener(new View.OnClickListener(){

public boid onClick(View view){

Intent myIntent = new Intent(view.getContext(),KotlinActivity.class);

startActivityForResult(myIntent,0);

}

}

}

Kotlin

override fun onCreate(savedInstanceState: Bundle?){

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_kt)

val next = findViewById(R.id.button02) as Button

next.setOnClickListener{

val intent: Intent = Intent()

setResult(RESULT_OK, intent)

finish()

}

}

Örnek 4: Method kullanımı

Java

public class MainActivity extends AppCompatActivity{

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public static void main(String[] args){

System.out.println("Hello world");

}

@Override

public String toString(){

return super.toString();

}

}

Kotlin

class MainActivity : AppCompatActivity(){

override fun onCreate(savedInstanceState: Bundle?){

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

}

fun main(args: Array<String>){

println("Hello World")

}

override fun toString(): String{

return super.toString()

}

}


İlgili kelimeler;
android java kotlin jvm
İlgili kategori;
Kotlin Dersleri
Makaleyi paylaşın; Google+FacebookTwitterTumblrPinterestLinkedIn

Benzer makaleler mi eklemeliyim ? Bana LinkedIn ve Github adreslerimden üzerinden ulaşabilirsiniz.