×

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

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Java Polymorphism

The process of representing one form in multiple forms is known as Polymorphism. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means...

5 minutes read.

Polygonal Number in Java

What does a Java Polygonal Number Mean? In mathematics, a polygonal number refers to a number that is expressed through dots or pebbles arranged in a regular polygonal pattern. Alphas are...

4 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Lazy loading in Java

Lazy loading is the idea of waiting to load an object until you need it. In other words, it is the practice of postponing class instantiation until it is necessary....

5 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.