🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
String.equals() method in Java is used to compare two strings for content equality. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
equalsMethod Syntax- Examples
- Comparing Equal Strings
- Comparing Different Strings
- Case Sensitivity
- Handling Null Values
- Conclusion
Introduction
The String.equals() method is a member of the String class in Java. It allows you to compare two strings to determine if they are exactly the same in terms of content. This method is essential for validating string inputs, ensuring data consistency, and performing equality checks.
equals Method Syntax
The syntax for the equals method is as follows:
public boolean equals(Object anObject)
- anObject: The object to be compared with the string. It is typically another string.
Examples
Comparing Equal Strings
The equals method can be used to compare two strings that are exactly the same.
Example
public class EqualsExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "Hello, World!";
boolean areEqual = str1.equals(str2);
System.out.println("Strings are equal: " + areEqual);
}
}
Output:
Strings are equal: true
Comparing Different Strings
The equals method will return false if the strings being compared are not exactly the same.
Example
public class EqualsExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "Hello, Java!";
boolean areEqual = str1.equals(str2);
System.out.println("Strings are equal: " + areEqual);
}
}
Output:
Strings are equal: false
Case Sensitivity
The equals method is case-sensitive, meaning it will distinguish between uppercase and lowercase characters.
Example
public class EqualsExample {
public static void main(String[] args) {
String str1 = "hello, world!";
String str2 = "Hello, World!";
boolean areEqual = str1.equals(str2);
System.out.println("Strings are equal: " + areEqual);
}
}
Output:
Strings are equal: false
Handling Null Values
The equals method can handle comparisons with null values safely. If the string being compared to is null, the method will return false.
Example
public class EqualsExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = null;
boolean areEqual = str1.equals(str2);
System.out.println("Strings are equal: " + areEqual);
}
}
Output:
Strings are equal: false
Conclusion
The String.equals() method in Java is a fundamental tool for comparing strings for content equality. By understanding how to use this method, you can accurately check if two strings are the same, considering case sensitivity and handling potential null values. Whether you are validating user input, ensuring data consistency, or performing equality checks, the equals method provides a reliable solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment