Java applications, packages, API specs, programming with Strings
The goals of this we page are for you to learn:
- How to run Java applications in Eclipse;
- What a package is and how you view Java specifications on the web ---the Java API (Application Programmer Interface) specs;
- How to write programs that manipulate strings (sequences of characters).
There is nothing deep in all this! You have already used an IDE (Integrated Development Environment) to write programs that manipulate strings in some language. It's just a matter of learning the mechanics of programming using Eclipse, learning a little bit about classes and objects, and seeing how strings are manipulated in Java. There's a lot of detail, and it may be frustrating at first, but stick with it and soon this will all appear simple.
1. Running a Java application to print "hello" in several languages.
In the tutorial on Eclipse, you already learned how to to write and run a Java application. If you don't remember, please visit this page and watch the first video on it. Then, in Eclipse, create a new project, create a method main in it ---you can type any simple string in the println statement, for example, "hello"--- and run it.
2. Packages and the package and import statements
A package is a collection of related Java classes —and perhaps other (sub) packages. This pdf file gives full details. It also tells you about the package statement and the import statement.
3. The Java API documentation
We give two videos. Use the first if you are using Java version 8. Use the second if you are using Java version 11 (or later). Java version 11 has modules; the documentation also does not use frames, so the way one uses the API documentation is slightly different.
Java version 8 has predefined classes for all sorts of things from dealing with strings of characters to building GUIS to reading web pages. Because there are so many classes, they are grouped into packages. The documentation for all these classes appears on the web as the "Java API Specification". This video shows you how to visit the Java API documentation in a browser and how to read it. (2:49 minutes) Read it here: 01API.pdf
Java version 11 has predefined classes for all sorts of things from dealing with strings of characters to building GUIS to reading web pages. They are grouped into modules, and each module contains packages, which contain the classes. The documentation appears on the web as the "Java API Specification". This video shows you how to visit the Java API documentation in a browser and how to read it. (4:50 minutes) Read it here: 01API-11.pdf
When you view an API documentation page for a class, like String or JFrame, scroll down to see documentation for the following kinds of components of the class:
- Field summary. This gives a short intro to each field. Click on a field name to get to the Field Detail, where the complete documentation is given.
- Constructor Summary. This gives a short intro to each constructor. Click on a constructor name to get to the Constructor Detail, where the complete documentation is given.
- Method Summary. This gives a short intro to each method. Click on a method name to get to the Method Detail, where the complete documentation is given.
4. Primitive type char
Read this pdf file to learn about primitive type char. There's more than you need to remember. Important points to remember are:
(1) a character is enclosed in single quotes, e.g. '$'.
(2) Special chars require an escape char \, e.g. '\n' is the new-line character and '\\' is the backslash character.
(3) A character is represented internally by an int, and one can cast between a char and its int representation. Such casts happen automatically when necessary. Example: The value of (char) 65 is 'A'. Example: the value of (int) 'A' is 65.
(4) Most characters are represented in Unicode, a 16-bit representation. See www.unicode.org/
5. Class String
In this video, we talk about strings ---sequences of characters--- and show you what the Java API documention looks like for class String. We introduce basic string functions and show you how to write calls on them. We end by developing a function from its specification, showing you how we intersperse English statements with their implementations in Java. (5:30 minutes) Read it here: String.pdf.
Below are the frequently used String functions. The *starred ones were introduced in Java 11.
s.length() | s.charAt(int) | s.substring(int) | s.substring(int, int) |
s.indexOf(String) | s.indexOf(code point) | s.lastIndexOf(String) | s.lastIndexOf(code point) |
s.startsWith(String) | s.endsWith(String) | s.contains(String) | *s.isBlank() |
s.compareTo(String) | s.equals(Object) | *s.lines() | |
s.trim() | *s.strip() | *s.stripLeading() | *s.stripTrailing() |
s.toLowerCase() | s.toUpperCase() | s.replace(String) | *s.repeat(int) |
We suggest that you look through the API documentation for class String. Here are the webpages:
Java 8: docs.oracle.com/javase/8/docs/api/java/lang/String.html
Java 11: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html