KotlinCS 124 LogoJava

Imports and Libraries

import java.util.Random;
import java.util.Date;
import java.util.Arrays;
Date date = new Date();
Random random = new Random();
if (random.nextBoolean()) {
System.out.println(date.getTime());
} else {
boolean[] values = new boolean[8];
Arrays.fill(values, false);
System.out.println(Arrays.toString(values));
}

This may be the most important lesson of the entire semester! All kinds of other people are creating and freely sharing useful Java code! 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 Java objects has focused on Strings. We’ve seen how to create them using new:

String s = new String("test");

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

String s = new String("test@illinois.edu");
String[] parts = s.split("@");
System.out.println(parts[0]);

Strings are built in to Java, meaning that we don’t need to use import statements to access them. But Java 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.

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!

First, 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

Example Libraries
Example Libraries

Now let’s look at a few useful parts of the Java 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!

java.util.Random
java.util.Random

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

// java.util.Random

java.util.Arrays
java.util.Arrays

Arrays are useful, but they seem to be missing a few features. Let’s explore where they are hiding out!

// java.util.Arrays

java.math
java.math

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

// java.util.Arrays

Collections
Collections

One of the most important part of the Java Standard Library is the collections framework. It provides a variety of different ways of store things.

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

More Practice

Need more practice? Head over to the practice page.