Strings
> Click or hit Control-Enter to run the code above
Storing Data
So far we know how to store a few different types of data using Java’s 8 primitive types:
-
Integers:
byte
,short
,int
,long
-
Floating point numbers:
float
,double
-
Character:
char
-
True or false:
boolean
What’s Missing?
Strings
Much of the interesting data in our human world is in text form.
As a computer scientist, you call text a string.
(I realize that I cannot remember not knowing that term. I guess I’m old, or just have really become a computer scientist.)
Java Strings
Unsurprisingly, Java has a String
type.
We can declare, assign, and pass them to functions like primitive types.
String maybe;
maybe = "Challen"; // No, bad
maybe = "Geoff"; // Better, thanks
callMe(maybe); // You have my number?
Strings Seem Familiar, But Behave Differently
> Click or hit Control-Enter to run the code above
Our First Object
In Java a String
is our first 1
example of an object.
Objects combine state (like variables) and behavior (like functions).
-
Each
String
has state: the array of characters in that string -
Each
String
also has behavior: functions that we can call on it that operate on its state
What does it store, and what does it do?
Objects v. Primitives
Primitives:
-
Store something that can be represented as a single number
-
Have type names that start with a lowercase letter:
int
,char
, etc.
Objects:
-
Can be made up of multiple other objects or primitive types
-
Have type names that start with an uppercase letter:
String
Objects
You will get a lot of practice working with and designing your own objects.
For now we’re going to use strings as a gentle introduction to using objects.
Strings
Normally to initialize an object in Java you use the new
keyword.
String myString = new String("ABC");
String anotherString = new String("DEF");
However, strings are so common that Java provides a shorthand:
String myString = "ABC";
String anotherString = "DEF";
(In practice there are minor differences between the two code snippets above, but we’re not going to worry about them.)
String Literal
In Java, the String
object is so important that is (almost
2)
the only object in Java that supports literals.
int first = 3; // 3 is a numeric literal
boolean isHot = true; // true is a boolean literal
char one = 'a'; // one is a char literal
String test = "Test Me"; // "Test Me" is a String literal
Arrays Are Also Objects
Do you remember? We’ve seen new
before…
int[] array = new int[8];
Java arrays are also objects—which is why we create them with new
and
can access their length property as array.length
.
Combining Strings
We can combine strings using the concatenation operator: +
.
String first = "Geoffrey";
String last = "Challen";
String full = first + " " + last;
Creating Strings
> Click or hit Control-Enter to run the code above
Dot Notation
We access an object’s state and methods using dot notation.
String example = "test";
System.out.println(example.length());
System.out.println(example.replace('t', 'b'));
System.out.println(example.toUpperCase());
Dot Notation
> Click or hit Control-Enter to run the code above
Fun With Strings
> Click or hit Control-Enter to run the code above
Questions About Strings?
Consecutive Identical Values
Given an array of chars a String, find all cases where consecutive characters are the same.
First, what is our algorithm?
-
Examine each character in the string
-
Compare it with the next value—but how do we get at that?
-
Print it out if they are the same
> Click or hit Control-Enter to run the code above
String Manipulation
Given a phone number formatted like "111.222.3333", convert it to "(111) 222-3333".
First, what is our algorithm?
-
Divide the input at each "."
-
Combine the values appropriately to create the new string
> Click or hit Control-Enter to run the code above
Preparing for CBTF Quizzes
Our quizzes get more challenging from this point forward. Here’s how to prepare.
-
Review the lecture slides
-
Go over the HW125 practice problems
-
Go over the HW125 practice problems
-
Go over the HW125 practice problems
-
Go over the HW125 practice problems
Preparing for CBTF Quizzes
int earnGradeOnQuiz() {
int currentGrade = 0;
currentGrade += reviewSlides();
while (workOnHomeworkProblems()) {
currentGrade++;
}
return currentGrade;
}
Announcements
-
MP0 is out and due next Monday. Please get started!
-
Quiz 2 has been rescheduled to extend through tomorrow. Please sign up to take it!
-
No course activities today because it’s cold!
-
But homework continues and staff are available for help on the forum.
-
Please fill out the initial student survey. 1% extra credit for anyone who does by Sunday 02/03/2019.