Home > Java > JavaBase > body text

Java method to determine whether strings are not equal

Release: 2019-12-21 14:50:40
Original
6786 people have browsed it

Java method to determine whether strings are not equal

1. Determine whether the addresses are equal. Use: ==

The Object class is the super class of all classes, and the equals method of the Object class directly compares addresses. Source code As follows:

public boolean equals(Object obj)  
   {  
       return this == obj;  
   }
Copy after login

2. Determine whether the values ​​are equal. Use: equals method

equals() method is used to compare a string with a specified object.

Syntax

public boolean equals(Object anObject)
Copy after login

Parameters

anObject -- The object to compare with the string.

Return value

Returns true if the given object is equal to the string; otherwise returns false.

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("runoob");
        String Str2 = Str1;
        String Str3 = new String("runoob");
        boolean retVal;
        retVal = Str1.equals( Str2 );
        System.out.println("返回值 = " + retVal );
        retVal = Str1.equals( Str3 );
        System.out.println("返回值 = " + retVal );
    }
}
Copy after login

The execution result of the above program is:

Return value = true

Return value = true

For more java knowledge please Pay attention to the java basic tutorial column.

The above is the detailed content of Java method to determine whether strings are not equal. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!