Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • 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

    null

    String test = null;
    System.out.println(test.length());

    This lesson is all about nothing! It turns out, that in Java, nothing causes all kinds of problems. We have to be very careful with nothing. In fact, nothing may be the biggest flaw with the Java programming language! Let’s find out why, and also continue to practice writing String algorithms.

    Java’s Nothing That Is (A Big Problem)
    Java’s Nothing That Is (A Big Problem)

    Java has a special value that is used to indicate that an object is uninitialized: null. Here’s an example using Strings, the one object that we have been working with:

    String s = null;

    null is a literal that can be used to initialize any object. Note that variables of primitive types cannot be set to null:

    int i = null;

    However, arrays of primitive types are objects and so can be null:

    int[] values = null;

    The Billion Dollar Mistake
    The Billion Dollar Mistake

    null seems harmless and maybe even kind of cute! But it has left a trail of damage and tears in its wake. In fact, famous computer scientist Tony Hoare, who is credited for inventing null as part of the programming language ALGOL, refers to it as his “billion dollar mistake”. Let’s look at why:

    String s = null;

    null has caused so many problems over the years, that newer variants of Java have made avoiding these problems a core design goal.

    Safely Working with null
    Safely Working with null

    From this point forward we’re going to try and keep null in the back of our minds. Always. Whenever we have a variable that could be null, we need to make sure that it isn’t null before we do anything with it!

    Fortunately there is a fairly straightforward pattern to this. Let’s check it out:

    int countCharacters(String input, char toCount) {
    }

    Practice: Array Max (1D)

    Created By: Geoffrey Challen
    / Version: 2020.9.0

    Let's bring together our understanding of functions and our understanding of arrays. Previously you determined the maximum of three values. Some of you solved this problem using an array. Now we'll write a function that can determine the maximum of any number of double values stored in an array.

    Declare and implement a function called arrayMax. It should accept an array of doubles as its single argument, and return the maximum value stored in the array. If the array is empty or null, you should return 0.0.

    More Practice With Strings
    More Practice With Strings

    Now let’s continue developing our algorithmic and String manipulation capabilities. Let’s apply our skills to determining whether two Strings are anagrams. An anagram is created by rearrange the letters from one word to form another:

    For our implementation we will not ignore whitespace and capitalization. Some anagrams do: for example, “New York Times” and “monkeys write” are anagrams, but the first string has two spaces while the second has only one. To us those would not be anagrams. When we are done, we can discuss how to make our approach more flexible.

    // Design and implement an anagram method

    Note that there are better ways to implement this algorithm. Perhaps we’ll return to it later and experiment with one or even two alternate approaches. That’s part of what makes computer science so exciting! There is always more than one way to solve any problem…

    Homework: Array Count Greater Than 1D

    Created By: Geoffrey Challen
    / Version: 2021.8.0

    Write a method named arrayCountGreaterThan. It should accept an int array as its first argument and an int as its second, and return a count of how many values in the array are strictly greater than the second parameter. assert that the passed array is not null.

    More Practice

    Need more practice? Head over to the practice page.