/// Java public class ExampleTest { @Test public void testMethod1() throws Exception {}
@Test public void testMethod2() throws Exception {} }
/// Kotlin class ExampleTest { @Test fun testMethod1() {}
@Test fun testMethod2() {} }
/// C++ std::thread([this] { this->DoThreadWork(); });
/// Java new Thread(() -> doThreadWork());
/// Kotlin Thread { doThreadWork() }
/// Java int value = 10; Entry add(String name, String description)
/// Kotlin var value: Int = 10 fun add(name: String, description: String): Entry
/// Java int result; // An integer that is a variable called "result".
/// Kotlin var result: Int // A variable called "result" that is an integer.
/// Java ... = new SomeClass();
/// Kotlin ... = SomeClass()
/// Java Lists.newArrayList();
/// Java new Thread(...).start(); // Awkward but valid
/// Kotlin Thread(...).start()
/// Java private final Project project; // Cannot reassign after init private Module activeModule; // Can reassign after init
/// Kotlin private val project: Project // Cannot reassign after init private var activeModule: Module // Can reassign after init
/// Java public void log(final String message) { … }
/// Kotlin fun log(message: String) { … } // "message" is immutable
/// Java @Nullable Project project; @NotNull String title;
/// Kotlin val project: Project? val title: String
/// Kotlin // 'parse' could return null, but this test case always works val result = parse("123")!!// The following line is not necessary. !! already asserts. ❌ assertThat(result).isNotNull()
/// Kotlin val result = parse("...")!! result.doSomething() result.doSomethingElse()
/// Kotlin (auto-generated) val result = parse("...") ❌ result!!.doSomething() ❌ result!!.doSomethingElse()
/// Java SomeClass instance1 = new SomeClass(); SomeGeneric<List<String>> instance2 = new SomeGeneric<>();
/// Kotlin val instance1 = SomeClass() val instance2 = SomeGeneric<List<String>>()
/// Java BaseClass instance = new ChildClass(); // e.g. List = new ArrayList
/// Kotlin val instance: BaseClass = ChildClass()
/// Java public void readFile() throws IOException { … }
/// Kotlin fun readFile() { … }
/// Kotlin (auto-converted from Java) @Throws(Exception::class) fun testSomethingImportant() { … }
/// Kotlin fun testSomethingImportant() { … }
val sumFunc: (Int, Int) -> Int = { x, y -> x + y }
{ x, y -> x + y }
val intList = listOf(1, 2, 3, 4, 5, 6) val sum = intList.fold(0, { x, y -> x + y })
val sum = intList.fold(0) { x, y -> x + y }
intList.filter({ x -> x % 2 == 0 }).count()
intList.filter { x -> x % 2 == 0 }.count()
Thread({ doThreadWork() })
Thread { doThreadWork() }
/// Java Color first = new Color(255, 0, 255); Color second = new Color(255, 0, 255); assertThat(first.equals(second)).isTrue(); assertThat(first == second).isFalse();
/// Kotlin val first = Color(255, 0, 255) val second = Color(255, 0, 255) assertThat(first.equals(second)).isTrue() assertThat(first == second).isTrue() assertThat(first === second).isFalse()
/// Java if (day == DayOfWeek.MONDAY) { … }
/// Kotlin (auto-converted from Java) ❌ if (day === DayOfWeek.MONDAY) { … }
/// Kotlin if (day == DayOfWeek.MONDAY) { … }
/// Java private String myName; // or private String mName; // or private String _name; public String getName() { … } public void setName(String name) { … }
/// Kotlin class User { val id: String // represents field and getter var name: String // represents field, getter, and setter }
/// Kotlin (auto-converted from Java) class User { ❌ val myId: String ❌ var myName: String }
@override Widget build(BuildContext context) { return Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( width: _bigger ? 100 : 500, child: Image.asset('assets/star.png'), ), RaisedButton( onPressed: () => setState(() { _bigger = !_bigger; }), child: Icon(Icons.star), ), ], ); }
AnimatedContainer( width: _bigger ? 100 : 500, child: Image.asset('assets/star.png'), duration: Duration(seconds: 1), ),
AnimatedContainer( decoration: BoxDecoration( gradient: RadialGradient( colors: [Colors.purple, Colors.transparent], stops: [ _bigger ? 0.2 : 0.5, 1.0]) ), ),
AnimatedContainer( width: _bigger ? 100 : 500, child: Image.asset('assets/star.png'), duration: Duration(seconds: 5), ),
AnimatedContainer( width: _bigger ? 100 : 500, child: Image.asset('assets/star.png'), duration: Duration(seconds: 1), curve: Curves.easeInOutQuint, ),
class SineCurve extends Curve { final double count; SineCurve({this.count = 1}); @override double transformInternal(double t) { return sin(count * 2 * pi * t) * 0.5 + 0.5; } }