Introduction to Software Development
GWU Computer Science
By the end of this module, for simple programs, you will be able to:
If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:
Before we move forward, let's catch up (complete any remaining work from the previous module)
In this case, make sure we've got:
As mentioned in the previous class, Strings have an API (Application Programming Interface), which describe the set of rules for using Strings in the programming of an application.
but why do we need to know the rules for using Strings?
Because Strings are different than the other types we've seen, like int, double, float, boolean, and char.
Before we move forward, let's look at how memory works in a computer and how we will imagine it for the duration of the class:
The following are called "Primitive Types": int, float, double, boolean, and char.
They are called "Primitive" because the function they perform is extremely simple:
They are locations in memory inside of which you save the actual value you need.
Look at the program and the abstraction of how we store the variables.
Note:
Reference types:
As we will see later in the semester, we will be creating objects that contain so much information that it does not make sense to save them inside the box or space assigned to a variable.
Instead, we use a clever mechanism: We leave a forwarding number!, or in other words, the address of where we are keeping the object's information.
When we make a variable of a Reference type, like String, inside the variable, instead of saving the String value, we save the address of the starting location where we are going to keep more information about the value or values of that type.
Look at the example shown below:
Note:
The String API is the list of methods that each of these objects provide.
Today, we'll show you how to find and use some of the typical String methods.
First, open this page in a different tab: Java 11 - String API.
You'll notice several things:
So far we have written methods that do not need any input nor return any output.
However, we have been using methods that do both.
In this section, we will use String methods that
1 2 3 4 5 6 7 8 9 10 | public class StringMethods { public static void main(String args[]) { String s1 = "The future ain’t what it used to be."; // Takes no parameters and returns 36 int s1Length = s1.length(); // s1.length() resolves into 36 System.out.println ("The length of s1 is: " + s1Length); } } |
Now, examine the following block of code:
9 10 11 12 13 14 15 | String s2 = ""; // Takes no parameters and returns 0 int s2Length = s2.length(); // s2.length() resolves into 0 System.out.println ("The length of s2 is: " + s2Length); // we can call String methods inside the print! System.out.println ("Is s2 empty? " + s2.isEmpty());// s2.isEmpty() resolves into true |
Each character in a string has its own "location" or "index".
The following is a visualization of the indices for the string:
I like Squirrels!
16 17 18 19 20 21 22 23 | String s3 = "I like Squirrels!"; // Takes no parameters and returns 26 int s3Length = s3.length(); // s3.length() resolves into 26 System.out.println ("The length of s3 is: " + s3Length); // Let's see what character is at index 7. 7 is an explicit integer input char c7 = s3.charAt(7);// s3.charAt(7) resolves into 'S' System.out.println ("Character at index 7 in s3 is " + c7); |
We can do the inverse search!.
What is the index for some character:
24 25 26 27 | // what is the index for the letter 'q'. 'q' is an explicit charater input. int qIndex = s3.indexOf('q');// s3.indexOf('q') resolves into 8 System.out.println ("Index for character 'q' in s3 is " + qIndex); |
We can extract parts of a string.
Observe the following piece of code:
28 29 30 31 32 | int beginIndex = 7; // Extract everything after index beginIndex. Input is now a variable. String sub1 = s3.substring(beginIndex);// s3.substring(beginIndex) resolves into "Squirrels!" System.out.println ("The substring from index " + beginIndex + " to the end in s3 is " + sub1); |
We can also indicate a final index for the substring.
Observe the following piece of code:
33 34 35 36 37 38 39 | int endIndex = 11; // This method takes two inputs // below, s3.substring(beginIndex, endIndex) resolves into "Squi" String sub2 = s3.substring(beginIndex, endIndex); System.out.println ("The substring from index " + beginIndex + // continues below " to index " + endIndex + " in s3 is " + sub2); |
We can actually call String methods on explicit strings, like in the example show below:
1 2 3 4 5 6 7 8 9 | public class ExplicitStrings { public static void main(String args[]) { System.out.println("explicit string".length()); System.out.println("endogamy".substring(2,5)); } } |
Try it out!!
The only difference is that were operating on the explicit string instead of a String in a saved variable.