Blog literacki, portal erotyczny - seks i humor nie z tej ziemi
Program 5.1: A Test for String Equality
You use ints pretty much as you expect.
<, >, <= and >= can only be used with numbers and characters. They cannot be used with Strings, booleans, arrays or other compound types since there's no well-defined notion of order for these objects. Is true greater than false? Is "My only regret is that I have but one life to give for my country" greater than "I have a dream""
Equality is a little easier to test however. true is equal to true and true is not equal to false. Similarly "My only regret is that I have but one life to give for my country" is not equal to "I have a dream." However you might be surprised if you ran Program 5.1.
class JackandJill {
public static void main(String args[]) {
String s1 = "Jack went up the hill.";
String s2 = "Jack went up the hill.";
if (s1 == s2) {
System.out.println(
"The strings are the same.");
}
else if (s1 != s2) {
System.out.println(
"The strings are different.");
}
}
}
The result is
The strings are different.
That's not what you expected.
Copyright 1996