Do this...
Create a test-case class named
Experiment08Test
.
Textual data is represented in Java as a string of
characters, which is implemented by the
String
data type. A character in Java
is represented by the char
data type.
A String
literal begins and ends with double
quotes, "
, with any number of characters between the
double quotes. A char
literal begins and ends with
single quotes, '
, with exactly one character
between the single quotes.
One important thing to understand is that a char
is
not a String
and similarly a String
is
not a char
.
Do this...
Create a test method in Experiment08Test
named
testTextTypes()
, and add these statement:
assertEquals("a String is not a char", "H", 'H'); assertEquals("a char is not a String", 'H', "H");
Then compile the code.
Question #03.08.01 Write down the
exact error message the compiler gives you for each line
of code.
To be answered in Exercise
Questions/lab03.txt
.
Do this...
Comment out these bad lines of code.
Let's try some declarations. This declares and initializes a
char
variable:
char ch1 = 'H';
Do this...
Create a test method in Experiment08Test
named
testChars()
, and add this declaration. Compile, and run for a green bar. (You aren't
asserting anything yet, so the real test here is to be able to
compile the code.)
Compilers thrive on strict rules, and above we noted that a
char
represents a character. Is this singular
important? What about two characters?
Question #03.08.02 Write down the
exact error message the compiler gives you.
To be answered in Exercise
Questions/lab03.txt
.
Question #03.08.03 What's the chance that we
could put three or more characters in the single quotes?
To be answered in Exercise
Questions/lab03.txt
.
Do this...
Comment out the bad declaration.
Do this...
Add an assertEquals()
statement to the test method to
assert the value of ch1
.
A string is a "string of characters". That's "characters", plural---more than one. If the compiler is as strict as I claim it is, this plural probably is significant.
Do this...
Create a test method in Experiment08Test
named
testStrings()
, and add this declaration:
String str1 = "Hi there!";
Adding more characters seems reasonable, but what about fewer?
How about one character in a String
?
Do this...
Add this declaration to the test method:
String str2 = "H";
Interesting. What about no characters?
Do this...
Add this declaration to the test method:
String str3 = "";
This string literal with no characters in it is known as the empty string. It may seem a little silly, but it is handy in a variety of situations.
So, as it turns out, a String
can have any
number of characters in it.
String
OperatorThere's one String
operator, and you'll use over
and over again. The operator is the concatenation
operator, represented in Java with the plus sign,
+
. So we can concatenate two String
s
together. "Hi" + " " + "there"
creates a new
String
"Hi there"
---it contains all
of the characters from the first, second, and third
String
s in that expression.
If that's really the case, then this should work just fine:
assertEquals("string concatenation", "Hi there", "Hi" + " " + "there");
Actually, Java is very flexible in concatenating
String
s. The operands could even be
char
s:
Do this...
Add this assert statement to the test method:
assertEquals("string concatenation with a char", ???, "Hi" + ' ' + "there");
Replace the ??? with the appropriate value, compile, and run the test-case class for a green bar.
In fact, Java is very, very
flexible in concatenating String
s. You can use
any type of data in a concatenation:
Do this...
Add this assert statement to the test method:
assertEquals("int concatenated to string", ???, 123 + "456");
Replace the ??? with the appropriate value, compile, and run the test-case class for a green bar.
In Java, String
s are more "important" than
int
s, so this is seen as String
concatenation. Consequently, the integer 123 is turned into a
String
consisting of the appropriate characters; that
String
is concatenated with the second
String
.
character, concatenation operator, empty string, string of characters