Tutorial Gateway

  • C Programming
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • About
    • About Us
    • Contact Us

Java equals Method

18-04-2016 by suresh Leave a Comment

The Java equals method is one of the Java String Method which is used to compare the string with user specified Object data to check whether they both represent the same sequence of characters or not, and based on the result it will return Boolean True or False. In this article we will show you, How to write string equals in Java Programming language with example. Before we get into the example, let us see the basic syntax of the string.equals in Java Programming language is as shown below.

Java equals Method syntax

Following method will accept the Object as the argument and compare the string with this Object to check whether the string is equal to this Object or not

1
2
3
4
public boolean equals(Object Obj); // It will return boolean True or False
 
//In order to use in program
String_Object.equals(Object Obj)

Java equals Example

The Java string equals method is used to check whether the string and user specified object data represents same sequence of characters or not. In this Java program, We are going to find the same.

JAVA CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Java String.equals example
package StringFunctions;
 
public class EqualsMethod {
public static void main(String[] args) {
String str1 = "Java Programming";
String str2 = "Java Programming";
String str3 = new String("Tutorial Gateway");
String str4 = new String("Tutorial Gateway");
String str5 = new String("Java Programming");
 
boolean a = str1.equals(str2);
boolean b = str3.equals(str4);
boolean c = str1.equals(str5);
boolean d = str3.equals(str5);
boolean e = str1.equals(str3);
System.out.println("Does the String Object str1 Equals to str2? = " + a);
System.out.println("Does the String Object str3 Equals to str4? = " + b);
System.out.println("Does the String Object str1 Equals to str5? = " + c);
System.out.println("Does the String Object str3 Equals to str5? = " + d);
System.out.println("Does the String Object str1 Equals to str3? = " + e);
}
}

OUTPUT

Java equals Method 1

ANALYSIS

First we declared two String variables str1, str2 and assigned corresponding values using following statement

1
2
String str1 = "Java Programming";
String str2 = "Java Programming";

Next, we declared three String Objects str3, str4, str5 and assigned corresponding values using following statement

1
2
3
String str3 = new String("Tutorial Gateway");
String str4 = new String("Tutorial Gateway");
String str5 = new String("Java Programming");

Following statement will call the public boolean equals (Object Obj) method to compare the string str1 with str2. From the above screenshot you can observe that, it is returning True because String is an Object.

1
boolean a = str1.equals(str2);

Following statement will compare the string Object str3 with another string object str4.

1
boolean b = str3.equals(str4);

Following statement will compare the string variable str1 with the string object str5.

1
boolean c = str1.equals(str5);

Next we checked for non equivalent values. Here “Tutorial Gateway” is not at all equals to “Java Program”

1
2
boolean d = str3.equals(str5);
boolean e = str1.equals(str3);

Lastly, we used the System.out.println statements will to print the output

Java equals Method Example

In this Java program, We are going to ask the user to enter any word and based the user entered string value we will display the message.

JAVA CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package StringFunctions;
 
import java.util.Scanner;
 
public class EqualsMethodex {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
String str = new String("java programs");
System.out.println("Please Enter any word: ");
String str1 = sc.nextLine();
if (str1.equals(str)) {
System.out.println("Welcome to Java Programming Language");
}
else {
System.out.println("Goodbye to Java Tutorials");
}
}
}

OUTPUT

Java equals Method 2

Let us enter different word

Java equals Method 3

ANALYSIS

First, we declared a String object strand assigned corresponding value using following statement

1
String str = new String("java programs");

Following statement will ask the user to enter any word, and then we are going to assign the user entered value to string variable str.

1
2
System.out.println("Please Enter any word: ");
String str1 = sc.nextLine();

Next, we used the Java If Else Statement to compare the user entered string with the String Object “java programs” and check whether they are equal or not.

  • If the statement inside the If is True, System.out.println(“Welcome to Java Programming Language”); statement will be printed
  • Else System.out.println(“Goodbye to Java Tutorials”); statement will be printed

Thank You for Visiting Our Blog

Placed Under: Java Tutorial

Share your Feedback, or Code!! Cancel reply

Trending

  • SQL Server Integration Services (SSIS)
  • SQL Server Tutorial
  • SQL Server Reporting Services (SSRS)
  • Home
  • C Program to Calculate the Sum and Average of n Number
  • Tableau
  • C Program to Print Prime Numbers from 1 to 100

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • Blogger
  • C Programs
  • Java Programs
  • SQL
  • SSIS
  • SSRS
  • Tableau
  • JavaScript

Copyright © 2018 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy