Java Reference
In-Depth Information
" so strings are equal ignoring
case.");
} else {
System.out.println("string1.equalsIgnoreCase(string3) is false"
+
" so strings are different.");
}
}
}
MatchStrings2.java
Of course, if you don't want to create another source file, leave the class name as it was before, as
MatchStrings . If you run this example, you should get the following output:
Test 1
string3 is now: Too many cooks
string1 is now: Too many cooks
string1.equals(string3) is true. so strings are equal.
Test 2
string3 is now: TOO many cooks
string1 is now: Too many cooks
string1.equals(string3) is false so strings are not equal.
string1.equalsIgnoreCase(string3) is true so strings are equal
ignoring case.
How It Works
In the if expression, you've called the equals() method for the object string1 to test for equality with
string3 . This is the syntax you've been using to call the method println() in the object out . In gener-
al, to call a method belonging to an object, you write the object name, then a period, and then the name
of the method. The parentheses following the method name enclose the information to be passed to the
method, which is string3 in this case. The general form for calling a method for an object is shown in
Figure 4-10 .
FIGURE 4-10
 
 
Search WWH ::




Custom Search