compared with
Current by Andrey Breslav
on Feb 04, 2013 13:22.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (20)

View Page History
h3. {{void}}\-returning methods

If a *Java* method returns *void*, it will return {{Unit}} when called from [Kotlin]. If, by any chance, someone uses that return value, it will be assigned at the call site by the [Kotlin] compiler, since the value itself is known in advance (being {{()}}). {{Unit.VALUE}}).

h3. Escaping for Java identifiers that are keywords in [Kotlin]
** {{List}} becomes {{List<*>}}, i.e. {{List<out Any?>}}

Besides, *Java*'s generics are not [retained at runtime|Generics#Reified generics], i.e. *Java* objects do not carry information about actual type arguments passed to their constructors, i.e. {{new ArrayList<Integer>()}} is indistinguishable from {{new ArrayList<Character>()}}. This makes it impossible to perform *instanceof*\-checks that take *Java* generics into account. [Kotlin] only allows *is*\-checks for [star-projected|Generics#Star-projections] *Java*'s generic types:
Like *Java*'s, [Kotlin]'s generics are not retained at runtime, i.e. objects do not carry information about actual type arguments passed to their constructors, i.e. {{ArrayList<Integer>()}} is indistinguishable from {{ArrayList<Character>()}}. This makes it impossible to perform *is*\-checks that take generics into account. [Kotlin] only allows *is*\-checks for [star-projected|Generics#Star-projections] generic types:
{jet}
if (a is <error desc="java.util.List is a Java class and its generic application to <Int> cannot be used in 'is'">java.util.List<Int></error>) // Error: cannot check if it is really a List of Ints
if (a is <error desc="List<Int> cannot be used in 'is', use List<*> instead">List<Int></error>) // Error: cannot check if it is really a List of Ints
// but
if (a is java.util.List<*>) // OK: no guarantees about the contents of the list
{jet}

h5. toString()

{{toString()}} is declared as an [extension function|Extension functions] that looks for an instance function named {{toString}} and calls it. If there's no {{toString}} it returns some default like {{typeinfo.typeinfo(this) {{this.javaClass.getName() + "@" + System.identityHashCode(this)}}.

From the programmer's perspective almost nothing changes compared to *Java*: all the existing *toString()* implementations work, and when you need a custom {{toString}} for your class, you simply put if there:
h5. getClass()

To retrieve the type information from an object, on uses the [{{typeinfo()}}|Runtime Type Information] function. {{getClass()}} is available for *Java* objects.
To retrieve the type information from an object, one uses the {{javaClass}} extension property.
{kotlin}
val fooClass = foo.javaClass
{kotlin}

Instead of *Java*'s {{Foo.class}}, use {{javaClass<Foo>()}}.
{kotlin}
val fooClass = javaClass<Foo>()
{kotlin}

h5. finalize()

h3. Package-level functions

All the functions and properties declared inside a [*package*|Packages] {{org.foo.bar}} are put into a *Java* class named {{org.foo.bar.namespace}}. {{org.foo.bar.BarPackage}}.

{jet}
package demo {
class Foo() {
}
fun bar() {
}
}
{jet}

// Java
new Foo();
demo.namespace.bar(); demo.DemoPackage.bar();
{code}

For the root package (the one that's called a "default package" in Java), a class named {{_DefaultPackage}} is created.

h3. Checked exceptions

// Java
try {
demo.namespace.foo(); demo.DemoPackage.foo();
}
catch (<error desc="Exception IOException is never thrown in the corresponding try block">IOException e</error>) { // error: foo() does not declare IOException in the throws list
try {
mayThrow(IOException.class);
demo.namespace.foo(); demo.DemoPackage.foo();
}
catch (IOException e) { // No problem
{jet}
void foo() throws IOException { // Java does not require us to throw an exception if we declare one
demo.namespace.foo(); demo.DemoPackage.foo();
}
{jet}
Now, you can call {{foo()}} instead of {{demo.namespace.foo()}}, {{demo.DemoPackage.foo()}}, and catch the exception.
* Option four is to make [Kotlin] put a *throws* list to the {{foo()}}'s signature with the *throws* annotation:
{jet}
{jet}

h3. Runtime Type Information

[Kotlin]'s generics are retained at runtime, this means that upon object creation, we pass the type information as parameter to the constructor:
{code}
// Java
new SomeClassDesclaredInKotlin<Integer>(TypeInfos.Int()) // We are passing a TypeInfo object explicitly
{code}

And the same for generic functions.

h3. Null-safety