UO.6: STRING OBJECTS: CONCATENATION, LITERALS, AND MORE
OBJECTIVES
I can:
create String objects for a String class
concatenate Strings using operators and escape sequences
Strings in Java are objects of the String class that hold sequences of characters (a, b, c, &, etc.).
Remember that a class (or classification) in Java defines the data that all objects of the class have (the fields) and the behaviors, the things that objects know how to do (the methods)
You can declare a variable to be of type String.
Class names in Java, like String, begin with a capital letter.
All primitive types: int, double, and boolean, begin with a lowercase letter.
This is one easy way to tell the difference between primitive types and class types.
public class Test1
{
public static void main(String[] args)
{
String greeting = null;
System.out.println(greeting);
}
}
The code above declares an object variable named greeting and sets the value of greeting to the Java keyword null to show that it doesn't refer to any object yet.
So System.out.println(greeting); will print null.
Object variables refer to objects in memory.
A reference is a way to find the actual object, like adding a contact to your phone lets you reach someone without knowing exactly where they are.
The value of greeting is null since the string object has not been created yet.
In Java there are two ways to create an object of the String class.
You can use the new keyword followed by a space and then the class constructor and then in parentheses you can include values used to initialize the fields of the object.
This is the standard way to create a new object of a class in Java.
String greeting = new String("Hello");
In Java you can also use just a string literal, which is a set of characters enclosed in double quotes (""), to create a String object.
String greeting = "Hello";
In both cases an object of the String class will be created in memory and the value of the variable greeting will be set to an object reference, a way to find that object.
public class Test2
{
public static void main(String[] args)
{
String greeting = "Hello";
Class currClass = greeting.getClass();
System.out.println(currClass);
Class parentClass = currClass.getSuperclass();
System.out.println(parentClass);
}
}
The code above will first print class java.lang.String since greeting was created by the String class.
The full name for the String class is java.lang.String.
The java.lang part is the package name.
Every class in the Java language is in a package and the standard classes like String are in the java.lang package.
Every object in Java knows the class that created it.
Also, every class knows its parent class.
Yes, a class can have a parent class, just as people have parents.
But, in Java a class can only have one parent.
A class can inherit object fields and methods from a parent class, just like you might inherit musical ability from a parent.
The fourth line will print class java.lang.Object because the parent class (superclass) of the String class is the Object class.
All classes in Java inherit from the Object class at some point in their ancestry.
STRING OPERATORS - CONCATENATION
Strings can be appended to each other to create a new string using the + or += operator.
This is also called concatenation.
Note that spaces are not added between strings automatically.
If you want a space between two strings then add one using + ” ” +.
If you forget to add spaces, you will get smushed output like “HiJose” instead of “Hi Jose”.
And remember that variables are never put inside the quotes (“”) since this would print the variable name out letter by letter instead of its value.
You can even add other items to a string using the + operator.
The other item will be converted to a string using the toString operator if it is an object and then appended to the current string.
All objects inherit a toString method that returns a string representation of the object.
If you are appending a number to a string it will be converted to a string first before being appended.
public class Test2
{
public static void main(String[] args)
{
String message = "12" + 4 + 3;
message += "!";
System.out.println(message);
}
}
Since the same operators are processed from left to right this will print 1 2 4 3!
First 4 will be turned into a string an appended to 12 and then 3 will be turned into a string and appended to 124.
Notice that the += operator could also be used to concatenate something to the end of the String object.
If you want to do addition instead, try using parentheses!
What if you wanted to print out a double quote ” character?
Since the double quote ” is a special character with meaning in Java, we put in a backslash in front of the quote to signal that we want just the character.
This is called a backslash escape sequence.
And if you wanted to print out a backslash, you would have to backslash it too in order to escape its special meaning.
Another useful backslashed character is backslash \n which will put in a newline.
Here are the escape sequences that may be used in the AP course:
public class TestEscape
{
public static void main(String[] args)
{
String message = "Here is a backslash quote \" " +
" and a backslashed backslash (\\) " +
"Backslash n \n prints out a new line.";
System.out.println(message);
}
}
SUMMARY
Strings in Java are objects of the String class that hold sequences of characters.
String objects can be created by using string literals: String s = "hi"; or by calling the String class constructor: String t = new String("bye");.
new is used to create a new object of a class.
null is used to indicate that an object reference doesn't refer to any object yet.
String objects can be concatenated using the + or += operator, resulting in a new String object.
Primitive values can be concatenated (combined) with a String object. This causes implicit conversion of the values to String objects.
Escape sequences start with a backslash \ and have special meaning in Java. Escape sequences used in this course include \", \\, and \n to print out a quote, backslash, and a new line.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) Have you ever played Mad Libs? In this game, you first choose a bunch of words without looking at the story then those words are filled into the story and it ends up being very weird! For this lesson, you will create a Mad Libs program;it could be a silly song, poem, story, instructions, memo, or whatever you want!
Your story should get input from the user using the Scanner class and store their input in 5 - 7 String variables.
When finished, bring your device up to show Mr. C your Mad Lib in action.