Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 2
String型同士の大小比較 | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 17
String型同士の大小比較
Stringオブジェクト同士の大小比較にはcompareToメソッドを使用します。
compareToメソッドは

public int compareTo(Object o)
public int compareTo(String anotherString)

の二つがありますが、String型同士の比較は後者のメソッドを使用します。
この時compareToメソッドの戻り値はAPIには、

引数文字列がこの文字列に等しい場合は、値 0。
この文字列が文字列引数より辞書式に小さい場合は、0 より小さい値。
この文字列が文字列引数より辞書式に大きい場合は、0 より大きい値
とあります。ですので、String型同士の比較は必ずint型の0と比較します。

検証
public class StringTest {

    public static void main(String[] args) {
        String a = "test1";
        String b = "test2";
        if(a.compareTo(b) == 0){
            System.out.println("正しい");
        }else{
            System.out.println("正しくない");
        }
    }
}
出力結果は以下のようになります。

正しくない

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 94

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 94

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 103

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/string/compareto/compareto.html on line 103