String Handling in Java - Difference between .equals() and '=='

In this particular blog we will dive into a very useful concept i.e. "String Handling in Java". In simple words a string can be defined as a sequence of characters, but in Java it a String is more than that of just a sequence of characters it covers a very wide area. There are a lot of operations and implementations that can be performed on String. In Java String is an immutable object, we will look into 'what immutable means exactly ?' in a few seconds. In today's discussion we will try to cover all concepts and operations that can be performed on String objects.
In the last part of the tutorial we will cover "what is StringBuffer and StringBuilder class ?" and how these two classes differs from String in aspect of operations and performance.

String in Java
Firstly lets us discuss about string and its basic operations. String class itself consists of 13 constructors hence a number of ways to obtain an String object. However in most of the cases when we talk about String in Java it is always an object that is merely obtained by one of the two ways described below. More information in String constructors can be obtained Here

How to create a String

In Java a String can be created in two ways,
1) By String literal
2) Using new keyword

1) By String literal
In this method we creates a String type object and assign a sequence of characters to it enclosed within " ".
String s = "javaguys";
Whenever a new string is created using String literal a object is created in String constant pool, String constant pool resides under heap memory. If a string with same content is already available in the pool than new String  type variable points to the object that is  already there in the memory and new object does not created.
      String s = "javaguys";
      String s1 = "javaguys";
As shown in above example, two code lines are creating a string having same content i.e. "javaguys". Therefore two reference variables will point to same object as shown in the figure below.

Difference between "==" and .equals(). (With String literal)

As we already knows that while comparing Strings, "==" checks for references and ".equals()" checks for content. In case of 's' and 's1' both will return a true. because the two variables are pointing to same object and having same content as well.
      s.equals(s1) // true --having same conten
      s==s1 // true -- pointing to same object

2) Using new keyword
An String object can be obtained using 'new' keyword as shown below:
String str = new String("javaguys");
In this statement two objects and a reference variable is created, One object of String class is created in simple non pool memory and second object is created by "javaguys" value. This time the reference variable points to the object in non pool memory and not to the literal object.

Difference between "==" and .equals(). (With String new keyword)
Every time we created a String object using a new keyword, the reference variable point to different objects no matters if their content is same or not.
      String str1 = new String("javaguys");
      String str2 = new String("javaguys");
      str1.equals(str2) // true --having same content
      str1==str2 // false -- pointing to different object

Immutable nature of String
Now lets discuss, why we called 'String' object immutable in nature. The answer to this question is that every time we performs an operation on a string object it reflects to a new object and older object remains unchanged. Let us see an example
      String s1 = "being";
      s1.equals(s1.concat(javaguys)) //false
Although concat operation is performed on S1 object but the changes are not reflected to s1, s1 is still having unchanged value i.e. "being". Here a new object with value "beingjavaguys" is created but reference variable s1 still points to "being".

compareTo() method of string

compareTo() method of String compares two String objects on the basis of their contents and return an integer value according to similarities of content of comparing objects.See the example below:
      String s = new String("being");
      String s1 = new String("beingjava");
      String s2 = new String("beingjavaguys");
      s.compareTo(s1) // -4
      s1.compareTo(s2) // -4
      s2.compareTo(s) // 8

Here we are done with a quick introduction of String in java, we came to know how to create a String in Java and difference between them, we had a look at difference between '==' and '.equals()' methods. In upcoming blogs we will see basic operations of String and difference between String and StringBuffer class.