Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • Lists and Type Parameters : 20

    • Imports and Libraries : 19

    • Multidimensional Arrays : 18

    • Practice with Strings : 17

    • null : 16

    • Algorithms and Strings : 15

    • Strings : 14

    • Functions and Algorithms : 13

    • Practice with Functions : 12

    • More About Functions : 11

    • Errors and Debugging : 10

    • Functions : 9

    • Practice with Loops and Algorithms : 8

    • Algorithms I : 7

    • Loops : 6

    • Arrays : 5

    • Compound Conditionals : 4

    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    Imports and Libraries

    import kotlin.random.Random
    import kotlin.math.exp
    import java.util.Date
    var date = Date()
    if (Random.nextBoolean()) {
    println(date.time)
    } else {
    println(exp(8.0))
    }

    This may be the most important lesson of the entire course! All kinds of other people are creating and freely sharing useful code that you can use. And now you’ll learn how to build on top of those contributions. Don’t try to do it yourself! You are not alone…

    Objects Beyond String
    Objects Beyond String

    So far our understanding of Kotlin objects has focused on Strings. We’ve seen how to create them:

    var s = "test"

    And how to access useful String methods, like length, substring, and split. And we’ve used these methods to solve problems:

    var s = "test@illinois.edu"
    var parts = s.split("@")
    println(parts[0])

    Strings are built in to Kotlin, meaning that we don’t need to use import statements to access them. But Kotlin allows you to access lots of other kinds of objects that might be useful for solving other kinds of problems! This lesson looks at how to access these other objects in your own code and provides some examples of useful objects and libraries.

    Java Interoperability
    Java Interoperability

    While Kotlin is a relatively new language, it interoperates with a language called Java that was released in 1996. What does that mean? It means that you can use Java code as a Kotlin programmer! (It also means that you can create libraries for Java programmers to use, but we’re not going to discuss that just yet.)

    This is a real benefit of Kotlin. Rather than starting from scratch, Kotlin can already use decades worth of mature, well-tested, and useful code written in Java. Awesome! Let’s find out how.

    import and Libraries
    import and Libraries

    Computer science is a remarkably collaborative field. In no other pursuit are millions of people all across the world so freely willing to share their creations!

    Because of this, changing the world through your code has never been easier. Let’s see how!

    As a Kotlin programmer, you can use both the Kotlin standard library and the Java standard library. Let’s look at the Kotlin standard library first.

    Next, let’s see how we find interesting and useful Java code that is part of the Java standard library.

    Next, let’s see how to actually load those library classes into our project.

    // import

    Practice: Format Date Method

    Created By: Geoffrey Challen
    / Version: 2020.10.0

    Complete the method below formatDate. It should accept a positive Long representing milliseconds since 1970 and return a String containing the ISO-8601 representation of this time. Here is one example for a recent timestamp: given 1602106609897 your function should return "2020-10-07T21:36:49.897Z".

    Do not overthink this. The solution we are after is a single line of code. We suggest that you explore the various built-in Java libraries for working with dates and times.

    We have provided starter code so that you can tell where the import statements should go. Ignore the class Question stuff, since we haven't covered that yet.

    Example Libraries
    Example Libraries

    Now let’s look at a few useful parts of the Kotlin Standard Library. No claim that these are the most useful parts! They’re just a few examples of code that is already out there for you to use!

    kotlin.random
    kotlin.random

    Looking to make your Kotlin programs more interesting? Try introducing some randomess! Let’s see how.

    // kotlin.random

    kotlin.math
    kotlin.math

    Computers can do math, right? That might be useful! Let’s get past +, -, *, and /.

    // kotlin.math

    Collections
    Collections

    One of the most important part of the Kotlin Standard Library is the collections framework. It provides a variety of different ways of store any kind of Kotlin object.

    We certainly couldn’t do it justice here. But we’ll be covering several of these incredibly useful classes over the next few lessons!

    Homework: Compare Date Strings

    Created By: Geoffrey Challen
    / Version: 2021.9.0

    Complete the method below called compareDates that, given two Strings containing datetimes in ISO-8601 format, returns -1 if the first time is before the second, 1 if the second is before the first, and 0 if they are equal.

    When you are done, here is how your method should work:

    You will want to approach this in two steps. First, convert each String into some kind of datetime representation. We suggest that you explore the various built-in Java libraries for working with dates and times. Don't attempt to do this yourself!

    Next, use the resulting object to compare the two datetimes. We might suggest that you explore the java.time.Instant class and its parse and other methods. We have provided some starter code so that you can identify where the import statements should go. Ignore the class Question stuff, since we haven't covered that yet.

    More Practice

    Need more practice? Head over to the practice page.