Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

equals()

Example

Determine if two strings are equal by comparing them.

String myStr1 = "Hello";
String
myStr2 = "Hello";
String
myStr3 = "Another String";
System
.out.println(myStr1.equals(myStr2)); // Returns true because they are equal
System
.out.println(myStr1.equals(myStr3)); // false

Definition and Usage

The equals() method compares two strings, returning true if they are equal and false otherwise.

Tip: Utilize the compareTo() method to compare two strings lexicographically.

Syntax

public boolean equals(Object anotherObject)

Parameter Values

Parameter

description

anotherobject

An object representing the other string to be compared.

Technical Details

Returns:

A boolean value:

 

  •            true: if the strings are equal
  •           false: if the strings are not equal

Overrides:

equals in class Object