24 March 2022

Using the Java API Guide: String and Math

Goal You will learn how to use methods and constantss on an API page. You will also learn aboout static items that are properties of the class (and not of any particular instance) as a whole and how to use them. We will study the String and Math classes, as they are very frequently used.

Bookmark this site! This is the encyclopaedia of Java.

picture of Java API window

To learn about a class (let's see String), type it in the search window in the upper right-hand corner.

picture of Java API window showing String search

A menu pops up; select java.lang.String. You will see this.

picture of Java API window showing String search

What are we seeing? There is some useful information here. First of all, the Java Standard Library contains thousands of useful classes that can create object for us. There are two levels of organization.

To see the contents of a module or a package, click on its link. If you click on java.lang, you can scroll down and see the String and Math classes in it.

Now go back to the String API page. Scroll down a little further and you will see this.

picture of Java API window showing first scroll

This section tells you a little bit about strings. They are immutable character sequences, just like they are in Python. However, the interface to them is different. The [] operator cannot be used to slice strings or to get substrings out of them.

There are plenty of things you won't understand right off, but there is plenty of useful information here.

There is Method to my Madness Scroll down to the Method summary. You will see this.

picture of String API Method Summary

This area describes the methods present for strings. Each method name is a link. Click on the charAt method.

picture of charAt method entry

The method's header is

public char charAt(int index)

The public tells you that the method is visible outside of the class. This documentation does not show any private parts of a class.

Next you see char. This is telling you that this method returns a datum of char type.

The name of the method is ... charAt.

Finally, the int index in the parens tells you this function accepts an integer as an argument. In fact, that integer is the index of the character you seek.

This information is displayed in this method's section.

You see a header Throws:. This describes any potential run-time error and what triggers it.

(Show use of charAt)

I will discuss a couple of these to get started. then, let's split the rest up and have you describe them in a similar fashion. Use jshell to create some strings and apply these methods to them.

startsWith

endsWith

indexOf

lastIndexOf

replace

contains

toLowerCase

toUpperCase

strip

stripLeading

stripTrailing

isBlank

String.format (static items can be used via the class name)

Now let's do the same for Math. Every method is static so you can call them with the prefix Math.

abs

acos

asin

atan

atan2

cbrt

exp

floor

hypot

log

log10

max

min

pow

sqrt

toDegrees

toRadians

What is a Class

A class is a blueprint for making objects Both Python and Java allow you to create custom data types via the means of a class.

To begin, we will make a very simple class for points in the plane with integer coördinates. We will create identical classes in Python and Java.