Syntax

Java Compare Strings Syntax

In Java, compare strings: compares string contents (not references) — a.equals(b)

Quick Answer

Syntax
a.equals(b)
JavaSyntax

Compare Strings

Code example
a.equals(b)

Compares string contents (not references).

Tip: Use equals(), not ==, for strings.

Learn More

How to compare strings in Java

  1. Open your Java file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Tip: Use equals(), not ==, for strings.

Java Compare Strings Syntax

Use a.equals(b).

Related Syntax References

Frequently asked questions

What is the Java syntax for compare strings?

Use this pattern: a.equals(b)

Can I copy this Java code example?

Yes. Use Copy code and adapt the example to your program.

Is this Java syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify