KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

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…

Practice: 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.

Homework: Array All Multiples

Created By: Geoffrey Challen
/ Version: 2022.8.0

Write a method named arrayAllMultiples that, given an array of int values greater than 0, returns whether the values are all integer multiples of one of the values in the array, which we'll call the base. For example, given the array {4, 2, 8} you would return true, since 4 and 8 are integer multiples of the base 2. Given the array {2, 2, 5} you would return false, since there is no value that all others are integer multiples of. (If the array contains 1, you should always return true, but there's no need to handle this case specifically.)

You should approach this problem in two steps. First, identify the base—the value in the array that you are going to check whether others are multiples of. This may sound complicated, but it's straightforward. You do not need to and should not check whether every value is base! Your solution should not contain a nested loop.

Next, check all the values in the array and look for a counterexample—a value that is not a multiple of the base.

The passed array may be null or empty. In those cases you should return false. Valid arrays will contain only integers strictly greater than 0.

ACM Student Chapter
ACM Student Chapter

Hello! We are ACM@UIUC, the largest CS RSO on campus! We represent 16+ special interest groups that focus on all areas of computing—from security, to AI, to policy. Each special interest group hosts weekly meetings on their topics of choice. Some are educational, others are experimental, and most are just plain fun. We also have 7 committees that maintain our internal state and host activities; among them are HackIllinois and R|P, the largest student-run hackathon and student-led tech conference in the Midwest, respectively.

You can learn more about our ACM chapter here.

More Practice

Need more practice? Head over to the practice page.