search
HomeJavaJavagetting StartedJava compares strings for equality

Java compares strings for equality

Nov 14, 2019 am 10:46 AM
java

Java compares strings for equality

Comparison of strings in java: ==

We often habitually write if(str1==str2) , this way of writing may cause problems in java

example1: (Recommended learning: java course)

String a="abc";
String b="abc"

Then a== b will return true. Because the value of a string in Java is immutable, only one copy of the same string will be stored in the memory, so a and b point to the same object;

example2:

String a=new String("abc"); 
String b=new String("abc");

Then a==b will return false. At this time, a and b point to different objects.

2. The equals method is used to compare whether the contents of the strings are the same.

example:

String a=new String("abc"); 
String b=new String("abc");
 a.equals(b);

will return true.

The equals comparison content of the String class has the same idea as follows:

1, First determine whether the addresses are equal, and return true if they are equal

2. Compare whether the types are the same or not, return false

3. Convert the incoming object to String and compare the lengths. The lengths are not equal. Return false

4. The lengths are equal, and the elements of the character array are compared in a loop. When there is an element with different contents, false is returned immediately

5. Two character arrays are looped. Compare all elements, if there are no elements with different contents, return true

The above is the detailed content of Java compares strings for equality. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment