Strings
> Click or hit Control-Enter to run the code above
Thanks, Ben!
Questions About Functions?
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
Announcements
-
MP0 is out and the first checkpoint is due this weekend. Please get started!
-
Yesterday’s lab has been reopened for you to complete today or tomorrow.
-
I’m not going to read these announcements anymore—but please view them and of course also anything we post on the forum in the announcements category.