Defining Strings
Student: So I guess we could also make a definition of a
String and use it to swap out colors, right?Prof: Absolutely, you try!
Student: OK, so I’d want an example where we make a few shapes, and they all have the same variable where we would pass in a color. Like new Circle and then it takes 10 or whatever size and then the name.
Prof: Good so far.
Student: And then another one, like a Rect maybe, that takes the width and height and then the same variable for its color… Something like this:
Student: Huh. That was an error?
Prof: Yeah, you ran into an interesting detail here. The type
int is spelled lowercase, but the type String is uppercase.Student: Why?
Prof: Why questions are great. The Java-specific reason is unfortunately jargon-y in this case: because
int is a primitive type and String is a reference type, and primitive types have lowercase keywords and reference types are usually written uppercase. See? Lots of jargon.Student: I like jargon! I told you, I’m an expert in many things. Ready for anything. But honestly I don’t know what you just said.
Prof: On this point, that’s fine. Stay tuned. For now it’s enough to remember that
String and int are spelled that way. You can use String and int as types along with Pict.Prof: Oh and I want to leave you with a reminder about a tricky case here. Say my program prints out
age = 17 after I run it. What do I know about that definition?Student: Well, it isn’t necessarily
int age = 17;, you could have done, like int age = 9 + 8. But it’s a number, so it’s got to be that it’s type is int, right?Prof: Run this and see for yourself:
Student: They look exactly the same. So
age is a number and label is text, but I can’t see that from the output.Prof: Right. The quotes in
"17" are how you make a string literal in the program. But that’s just part of the program. A lot of places in Java (and many programming languages) will print out the string value without the quotes. When you want to know whether a name holds a number or a string, look at the type in its definition, not at what gets printed.Student: Does it matter? They print the same, so maybe Java treats them the same.
Prof: It matters as soon as you do something with them. For instance, what would
age + 1 be? And what about label + 1? Hold onto that question for the next step.Practice
Here is the program from the top of this step with String spelled correctly, plus a definition for the radius. What happens when it runs? (You can use the editor above to experiment!))
String myColor = "red";
String size = "10";
Pict c = new Circle(size, myColor);
Pict r = new Rect(30, 40, myColor);- A red circle with radius 10 and a red rectangle.
"10"is a string, andCircletakes anintradius. Java does not read the text inside the quotes to see if it looks like a number. - An error:
Circletakes anintradius, andsizeis aString. - An error, fixed by changing the definition to
int size = "10";.Changing the type alone does not help."10"is a string literal wherever it appears, soint size = "10";is an error too. The fix isint size = 10;, with no quotes. - An error:
myColoris used twice, and a definition can only be used once.myColoris fine. It is aString, and both constructors take aStringcolor. The problem issize.