×

Java String equals() method

equals() method compares two string based on the their content.

Syntax

public boolean equals(Objects anObject)

parameter

anObject: Object to be compared with the current String.

Returns

It returns true when the current String is equivalent to the given String Otherwise, it returns false.

Java String equals() method Example1:

public class JavaStringEqualsEx1
{ 
   public static void main(String args[])
  { 
   String str1,str2,str3;
   str1="welcome to java tutorial";
   str2=str1.concat(" and example"); 
   str3="welcome to java tutorial and example";
   System.out.println(str2.equals("ample"));
   System.out.println(str1.equals("al"));
   System.out.println(str1.endsWith("example"));
   System.out.println(str2.equals(str3));
   }
}

Output:

false
false
false
true

Java String equals() method Example 2:

public class JavaStringEqualsEx2
    { 
        public static void main(String[] args)
        { 
            String str = "tutorialandexample";
            System.out.println(str.equals("and")); //returns false
            if(str.equals("tutorialandexample")) {// returns true 
                String str2=str.concat(".com");
                System.out.println(str2);
            }else
               System.out.println("something else"); 
        } 
    }

Output:

 false
 tutorialandexample.com

Java String equals() method Example 3:

   public class JavaStringEqualsEx3


    { 
        public static void main(String[] args)
        {
            String[] s1 = {"welcome","to","java","tutorial"};
            System.out.println(s1[0]);
            System.out.println(s1[1]);
          //retriveing the array strings
            for (String element:s1){
            if ( element.equals("java")) {
               System.out.println("java");
              }else if( element.equals("tutorial")){
               System.out.println("tutorial"); 
              }
            }
           System.out.println(s1.equals("welcome to java tutorial"));  
      }
}

Output:

welcome
to
java
tutorial
false

Related Topics

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Knapsack problem in Java

We have a collection of items in the knapsack problem. Every object has a weight and a value. These things should go in a knapsack. But there is a weight...

3 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.