Java String equals() Method
What is String?
In Java, an string class is a series of characters that are interpreted literally by a script. An object called a string represents a string of characters. Java.lang is used. A string object is made using the String class.
- Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.
To know more about String Class in java read the complete article.
String Equals() Method:
The Java String equals() method of the String class compares the contents of the two strings. It returns false if any character is not found to match. It returns true if all characters match.
This string is compared to the given object. The argument must not be null and must be a String object that represents the same set of characters as this object in order for the result to be true.
- The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.
Syntax:
public boolean equals(Object anObject)
Parameters:
anObject - The object to compare this String against
Let’s look at a string-related Java program where the Java String Equals() method is used to perform an operation on the given string.
Examples : Concatenating two given strings difference
public class Main { public static void main(String[] args) { String myStr1 = "Hello"; String myStr2 = "Hello"; String myStr3 = "Hello PrepInsta"; System.out.println(myStr1.equals(myStr2)); System.out.println(myStr1.equals(myStr3)); } }
Output
true false
Examples 2 : Java String Concat() Method
public class Main { public static void main(String[] args) { String myStr1 = "PrepInsta"; String myStr2 = "Prime"; String myStr3 = "PrepInsta"; System.out.println(myStr1.equals(myStr2)); System.out.println(myStr1.equals(myStr3)); } }
Output
false true
Examples 3: Concatenating two given strings ignoring lower and upper case.
public class Main { public static void main(String[] args) { String myStr1 = "PrepInsta"; String myStr2 = "Prime"; String myStr3 = "Prep"; System.out.println(myStr1.equals(myStr2)); System.out.println(myStr1.equals(myStr3)); } }
Output
false false
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others