3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【初心者向け】JavaとKotlinの違い

Last updated at Posted at 2019-10-05

Kotlinとは

  • Kotlinは、JetBrains社が作っているJVM上で動作する言語。
  • Kotlinは、Javaと完全に相互運用が可能である。あるクラスをJavaで書いて、それを継承したクラスを Kotlinで書いて、さらに継承したクラスをJavaで書くことができる。
  • Javaで書かれていたプロジェクトに、追加開発分だけ Kotlin で書くことでき、導入しやすい。

変数宣言

  • Java

         String str = "aaa";
    
  • Kotlin

         var str: String = "aaa"
    

final な変数を宣言するには val 、変更可能な変数を宣言するには var を使います。

if文

  • Java

       String foo = bar < 10 ? "abc" : "xyz";
    
  • Kotlin

       var foo = if (bar < 10) "abc" else "xyz"
    

if や else ブロック内の結果を直接代入することができます。

for文

  • Java

           for (int num : numbers) {
              System.out.println(num);
           }
           for (int i = 0; i< 5; i++) {
              System.out.println(i); 
           } 
    
  • Kotlin

         for (num in numbers) {
            println(num)
         }
         for (i in 0 until 5) {
            println(i)
         }
    

kotlinは inを使う。

コンストラクタの呼び出し

  • Java

      Foo foo = new Foo();
    
  • Kotlin

      var foo = Foo()
    

kotlinでは、newは不要である。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?