This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun tekMiCiftMi(sayi: Int) : String { | |
| return when { | |
| sayi % 2 == 0 -> "Çifttir" | |
| sayi % 2 == 1 -> "Tektir" | |
| else -> { | |
| throw IllegalArgumentException() | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun isMonday(x: Any) = when(x) { | |
| is String -> x.startsWith("Mon") | |
| else -> false | |
| } | |
| fun main(args: Array<String>) { | |
| println(isMonday("Monday")) // true | |
| println(isMonday(42)) // false | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bu extension function bir sayının yüzdesini hesaplar | |
| fun Int.yuzde(yuzde: Int) = (this * yuzde) / 100 | |
| fun main(args: Array<String>) { | |
| println(100.yuzde(20)) // 20 | |
| println(200.yuzde(20)) // 40 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Celcius işareti ekleme fonksiyonu | |
| val Int.c: String | |
| get() = this.toString() + "ºC"; | |
| fun main(args: Array<String>) { | |
| println(100.c) // 100ºC | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun hesapla() { | |
| TODO("daha sonra yapılacak") | |
| } | |
| fun main(args: Array<String>) { | |
| println(hesapla()) | |
| } | |
| /* Çıktısı |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main(args: Array<String>) { | |
| // listOf() ile bir tamsayı listesi oluşturulur | |
| val sayilarList = listOf(2, 3, 4, 6, 23, 90) | |
| // -> operatörü ile predicate belirlenir. | |
| val hepsiPozitifMi = sayilarList.all({ sayi -> sayi > 0 }) | |
| println(hepsiPozitifMi) | |
| // predicate şartındaki değişken kısaca it keyword'ü ile belirlenebilir. | |
| val herhangiBiriOndanBuyukMu = sayilarList.any({ it > 10 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val tembelDeger: String by lazy { | |
| println("hesaplandı!") | |
| "Hello World" | |
| } | |
| fun main(args: Array<String>) { | |
| // İlk çağrımda Hello World ifadesi değere atanır ve hesaplandı! yazar | |
| println(tembelDeger) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| class Main { | |
| public static void main(String[] args) { | |
| List stars = Arrays.asList("Java", "Has", "Raw", "Types"); | |
| for(Object star: stars){ | |
| System.out.println((String)star); // cast gerekli | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| class Main { | |
| public static void main(String[] args) { | |
| Integer[] ints = {1, 2, 3}; | |
| Object[] objs = ints; // Integer tipi, Object'in alt türü olduğu için hata vermez. | |
| objs[0] = "asd"; // Hata: java.lang.ArrayStoreException: java.lang.String | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| class Main { | |
| public static void main(String[] args) { | |
| List<Integer> ints = new ArrayList<Integer>(); | |
| List<Object> objs = ints; // Derleme zamanı hatası: incompatible types: List<Integer> cannot be converted to List<Object> | |
| objs.add("abc"); // Normalde bu kullanım biçimi doğrudur. | |
| Integer s = ints.get(0); // Eğer generic'ler invariant olmasaydı bu hata runtime'da alınacaktı. | |
| // ClassCastException: Cannot cast String to Integer | |
| } |