×

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

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.