集册 Java实例教程 比较字符值

比较字符值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

606
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
比较字符值

public class CharacterDemo {

    public static void main(String args[]) {

        Character a = new Character('a');

        Character a2 = new Character('a');//来 自 n o w j a v a . c o m - 时  代  Java

        Character b = new Character('b');


        int difference = a.compareTo(b);


        if (difference == 0) {

            System.out.println("a is equal to b.");

        } else if (difference < 0) {

            System.out.println("a is less than b.");

        } else if (difference > 0) {

            System.out.println("a is greater than b.");

        }


        System.out.println("a is "

                + ((a.equals(a2)) ? "equal" : "not equal") + " to a2.");


        System.out

                .println("The character "

                        + a.toString()/* 来自 时   代     Java  公  众  号 - nowjava.com*/

                        + " is "

                        + (Character.isUpperCase(a.charValue()) ? "upper"

                                : "lower") + "case.");

    }

}


展开阅读全文