Program Logic

Unit 4 Index

Library Methods

Library Methods

There are two basic types of methods...methods you create and methods that are built into all modern programming languages. The built-in methods are sometimes called library methods. Library methods are very useful because they perform common tasks such as number manipulation, data type conversion, and string manipulation. You do not write built-in methods, you just need to learn how to use them. All high-level programming languages have a set of library methods. I highly advise you to get familiar with library methods. After all, why re-invent the wheel by developing your own versions of the built-in methods when you can save time and potential errors by using a tried and true built-in library method.

What are Library Methods?

Here is the thing about library methods, they are like a black box. You do not need to know the intimate details of what is inside the box, you just need to know how to use it. With library methods, you send in the data that the box is expecting and you get back manipulated data. In other words, you give the library method an input, something magical happens inside the black box, and you get back the output.

Using Library Methods

Remember, you do not create library methods, instead, you just use them. The basic pattern is, give the library method what it wants and you get back what you need. In order to use some of Java's built-in library methods, you may need to import specific libraries. By default, the Java language gives you the java.lang package, meaning this package is available without importing. Inside of the package are many class libraries. Each library has one or more methods within it. The implied code is java.lang.* which means give me all the class libraries within the java.lang package.

In code, library method names are preceded with a dot. Their naming convention is camel case and there is always a set of parentheses after the method name. Depending on the method, these parentheses may or may not contain data. Lastly, remember to terminate your statement with a semicolon.

Java Library Methods

Although library methods are built into high-level languages, the specific syntax may differ from language to language. Furthermore, a library method available in one language may not be available in a different language. This unit focuses on common library methods within the Java programming language. Get familiar with what each method does and then when you branch out into different languages find its library method equivalent. Library methods should be firmly planted into your programming bag of tricks. Please note, Java includes many more library methods than the ones listed in this unit.

Most of the library method in this unit are part of Java's core package, java.lang. That means you do not need to import the package, you just start using its contents. One thing to keep in mind is that all methods have parentheses after the method name when you use them.

The Dot Operator

Throughout this unit and others, you will see a dot ( . ) between a class name or object variable and a method or property. Technically the name of the method does not include this dot. I include the dot so that you get used to the idea that it must be there. In Java, the dot operator is sometimes called a member operator or dot notation. Please keep this in mind when you see syntax such as sb.append() or Math.round(). Both append() and round() are built-in library methods; they always include the parentheses.

What's next?

The next chapter in this unit is: Common String Methods