Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JAVA»How to Compare Two Strings in Java

    How to Compare Two Strings in Java

    RahulBy RahulOctober 9, 20152 Mins ReadUpdated:August 17, 2019

    Question – How do I compare two strings in the Java programming language?

    Java provides two options for string compare. First is == operator and second is .equals() function for Java string compare. This Java tutorial will help you to understand string comparison under Java and difference between == and .equals() for string comparison in Java.

    • Operator == checks for the object reference and matches true if both string belongs to same object.
    • Function .equals() checks for the value of string, it doesn’t matter to which object they belongs.

    Compare two Simple Strings

    If both strings are simple strings. Java will consider them the same object. So both (== & equals()) will result as true.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public class StringCompare{
     
       public static void main(String args[]){
          String str1 = "hello world";
          String str2 = "hello world";
     
          System.out.println( str1 == str2 );       // Return True
          System.out.println( str1.equals(str2) );  // Return True
       }
    }

    Compare two String Objects

    If both strings are object strings. Java will consider them different-2 objects. So == operator will return false and equals() function will result in true;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public class StringCompare{
     
       public static void main(String args[]){
          String str1 = new String("hello world");
          String str2 = new String("hello world");
     
          System.out.println( str1 == str2 );        // Return False
          System.out.println( str1.equals(str2) );   // Return True
       }
    }

    Compare one Simple String with one Object

    If one is a simple string and other is an object string. Java will consider them different-2 objects. So == operator will return false and equals() function will result in true;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public class StringCompare{
     
       public static void main(String args[]){
          String str1 = "hello world";
          String str2 = new String("hello world");
     
          System.out.println( str1 == str2 );        // Return False
          System.out.println( str1.equals(str2) );   // Return True
       }
    }

    Compare with NULL String

    But in case of comparison with null string == will return false but .equals() function will throw an exception. To avoid null exception we can use Objects.equals(str1, str2) function like following example.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import java.util.Objects;
    public class StringCompare{
     
       public static void main(String args[]){
          String str1 = null;
          String str2 = new String("hello world");
     
          System.out.println(str1.equals(str2)); // Throw an Exception
          System.out.println(Objects.equals(str1, str2)); // Return False
     
       }
    }

    code example Java programming string tutorial
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Setup Forward Only DNS Server on Ubuntu & Debian
    Next Article How to Backup and Restore SVN Repository in Linux

    Related Posts

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    How to Install JAVA on Ubuntu 22.04

    Updated:May 16, 20224 Mins Read

    Java HashMap – How to Get Value from Key

    Updated:April 6, 20222 Mins Read

    Java HashMap – How to Get Key from Value

    Updated:April 6, 20222 Mins Read

    5 Methods to Print an Array in Java

    Updated:April 21, 20222 Mins Read

    How To Install Java on Debian 11

    4 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.