Activity 2.2.1.
Run the following code. Add another string that is the empty string and print out its length.
String
Objects
String
and Graphics
classes.String
class.String
class. In addition to the two String()
constructor methods, which are used to create strings, it lists several useful instance methods that can be used to manipulate strings.String
class also has two instance variables: value
, which contains the string’s characters, such as “Hello98”, and count
, which records the number of characters in the string.String
, named str
and we want to find out how many characters it contains, we can call its length()
method, using the expression str.length()
. If we want to print its length, we can embed this expression in a print statement:System.out.println(str.length()); // Print str's length
methodName()
refers to one of its instance methods.String
methods in a program, we must first create a String
object. To create a String
object, we first declare a String
variable.String str; // Declare a String variable named str
String
object by using the new
keyword in conjunction with one of the String()
constructors and assign the new object to the variable we declared:str = new String("Hello"); // Create a String containing the word "hello"
String
that contains, as its value, the word "Hello" that is passed in by the constructor. The String
object that this creates is shown in Figure 2.2.2.String
object stores a sequence of characters and a count
giving the number of characters.String str2 = new String(); // Create a String
String
object that contains the empty string as its value. The empty string has the literal value "" — that is, a pair of double quotes that contain no characters. Because the empty string has no characters, the count
variable stores a zero (Figure 2.2.3).new
and a constructor to assign an initial value to a String
variable — or to any other type of object variable.int
and boolean
variables. Because primitive types are not objects in Java, we can assign them values with a simple assignment statement:int num = 5;
String
variable (and a variable for any other type of object) stores a reference to an object of that type. (A reference is also called a pointer because it points to the memory address where the object itself is stored.) The object’s constructor creates an object of that type somewhere in memory and supplies a reference to it that is stored in the variable. Figure 2.2.4 illustrates this difference with a simple example.int num = 5;
boolean boo = true;
String s = new String("hello");
int
in Java is always 32 bits. But a String
or a Riddle
object can vary in size.int
variable is boolean
variable is false
.null
, which indicates that it points to nothing. It has no object to point to (Figure 2.2.5).int num; // num has the value 0
boolean boo; // boo has the value false
String s; // s has the value null
String
object, you can use any of the methods shown in Figure 2.2.1 on it. One of the most useful String
method is the concat(String)
method, which can be used to concatenate two strings. This method takes a String
argument. And it returns a String
that combines a String
argument to the String
that the method is called on. For example:String s1 = new String("George ");
String s2 = new String("Washington");
System.out.println(s1.concat(s2));
concat()
method adds the String
s2 to the end of the String
s1. The result will be the String
"George Washington".new String()
when creating a new string object. The following code will also work:String s1 = "George ";
String s2 = "Washington";
String
objects is to use the plus sign (+), which serves as a concatenation operator in Java:System.out.println(s1 + s2);
String
method is the equals()
method. This is a boolean
method, which is used to compare two String
objects. If both have the same characters, in the same order, it will return true. Otherwise it will return false. For example, consider the following code segment:String s1 = "Hello";
String s2 = "Hello";
String s3 = "hello";
String
variable that contains null
. Executing the statements:String s1; // null
String s2 = ""; // empty string
System.out.println(s1.equals(s2)); // System crash
true
, it will cause the program to crash — to terminate abnormally.String
variable, or any other object variable whose value is null
. When the above code is executed, it will report a null pointer exception, one of the most common runtime errors. When you see that error message, it means that you were trying to use a null
— one that does not refer to any object.String
object which just happens to contain zero characters.s
, s1
, and so on, and it instantiates a String
object for each variable to refer to. It then prints out a top-five list using the concatenation operator to combine strings. Can you figure out what it prints without running it?public class StringPuns {
public static void main(String args[]) {
String s = new String("string");
String s1 = s.concat(" puns.");
System.out.println("Here are the top 5 " + s1);
String s2 = "5. Hey baby, wanna ";
String s3 = s + " along with me.";
System.out.println(s2 + s3);
System.out.println("4. I've got the world on a " + s + ".");
String s4 = new String("two");
String s5 = ". You have more class than a ";
System.out.print(s4.length());
System.out.println(s5 + s + " of pearls.");
System.out.print("2. It is ");
System.out.print(s.equals("string"));
System.out.println(" that I am no " + s + " bean.");
String s6 = " quintet.";
System.out.println("1. These puns form a " + s + s6);
} // main()
} // StringPuns class
String s = "ing";
System.out.println("s" + s + s + " k" + s + ".");