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).
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.
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.
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.
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.
Figure6.14.3.Figure 2: Object variable of type String with a reference to a String object which has a reference to the String class which has a reference to the Object class.
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.
Try the following code. Add another variable for a lastname that is “Hernandez”. Use += or + to add the lastname variable after name to the result. Use += or + to add 2 more exclamation points (!) to the end of the happy birthday greeting in result.
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.
What do you think the following will print? Guess before you hit run. If you want the addition to take place before the numbers are turned into a string what should you do? Try to modify the code so that it adds 4 + 3 before appending the value to the string. Hint: you used this to do addition before multiplication in arithmetic expressions.
Since the same operators are processed from left to right this will print 1243. First 4 will be turned into a string and appended to 12 and then 3 will be turned into a string and appended to 124. 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 is an active code sample that creates two greeting strings: one using a string literal and the other using new and the String constructor. Change the code to add 2 new strings called firstname and lastname, one using a string literal and the other using new, and print them out with the greetings.