Compares two Strings, returning true if they are equal. - Java java.lang

Java examples for java.lang:String Compare

Description

Compares two Strings, returning true if they are equal.

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str1 = "java2s.com";
        String str2 = "java2s.com";
        System.out.println(equals(str1, str2));
    }//from  w w w  . j  av a 2 s. c om

    /**
     * <p>
     * Compares two Strings, returning <code>true</code> if they are equal.
     * </p>
     * 
     * <p>
     * <code>null</code>s are handled without exceptions. Two <code>null</code>
     * references are considered to be equal. The comparison is case sensitive.
     * </p>
     * 
     * <pre>
     * StringUtils.equals(null, null)   = true
     * StringUtils.equals(null, "abc")  = false
     * StringUtils.equals("abc", null)  = false
     * StringUtils.equals("abc", "abc") = true
     * StringUtils.equals("abc", "ABC") = false
     * </pre>
     * 
     * @param str1
     *            the first String, may be null
     * @param str2
     *            the second String, may be null
     * @return <code>true</code> if the Strings are equal, case sensitive, or
     *         both <code>null</code>
     * @see java.lang.String#equals(Object)
     */
    public static boolean equals(final String str1, final String str2) {
        return str1 == null ? str2 == null : str1.equals(str2);
    }
}

Related Tutorials