
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Differentiate String == operator and equals() method
In this article, we will understand how to differentiate == operator and equals() method in Java. The == (equal to) operator checks if the values of two operands are equal or not, if yes then condition becomes true.
The equals() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Below is a demonstration of the same −
Suppose our input is −
The first string : abcde The second string: 12345
The desired output would be −
Using == operator to compare the two strings: false Using equals() to compare the two strings: false
Algorithm
Step 1 – START Step 2 - Declare two strings namely input_string_1, input_string_2 and two boolean values namely result_1, result_2. Step 3 - Define the values. Step 4 - Compare the two strings using == operator and assign the result to result_1. Step 5 - Compare the two strings using equals() function and assign the result to result_2. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class compare { public static void main(String[] args) { String input_string_1 = new String("abcde"); System.out.println("The first string is defined as: " +input_string_1); String input_string_2 = new String("12345"); System.out.println("The second string is defined as: " +input_string_2); boolean result_1 = (input_string_1 == input_string_2); System.out.println("\nUsing == operator to compare the two strings: " + result_1); boolean result_2 = input_string_1.equals(input_string_2); System.out.println("Using equals() to compare the two strings: " + result_2); } }
Output
The first string is defined as: abcde The second string is defined as: 12345 Using == operator to compare the two strings: false Using equals() to compare the two strings: false
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class Demo { static void compare(String input_string_1, String input_string_2){ boolean result_1 = (input_string_1 == input_string_2); System.out.println("\nUsing == operator to compare the two strings: " + result_1); boolean result_2 = input_string_1.equals(input_string_2); System.out.println("Using equals() to compare the two strings: " + result_2); } public static void main(String[] args) { String input_string_1 = new String("abcde"); System.out.println("The first string is defined as: " +input_string_1); String input_string_2 = new String("12345"); System.out.println("The second string is defined as: " +input_string_2); compare(input_string_1, input_string_2); } }
Output
The first string is defined as: abcde The second string is defined as: 12345 Using == operator to compare the two strings: false Using equals() to compare the two strings: false
- Related Articles
- Golang Program to Differentiate String == operator and equals() method
- Python Program to Differentiate String == operator and__eq__() method
- Java String equals() method.
- Java String equals() method example.
- What is the difference between equals() method and == operator in java?
- String compare by equals() method in Java
- Java string concat() method vs "+" operator
- Equals(String, String) Method in C#
- The equals and == operator for Enum data type in Java
- Difference between == and equals() method in Java.
- IntBuffer equals() method in Java
- FloatBuffer equals() method in Java
- DoubleBuffer equals() method in Java
- ShortBuffer equals() method in Java
- ByteBuffer equals() method in Java

Advertisements