Skip to content

Instantly share code, notes, and snippets.

@shah-smit
Last active April 26, 2017 16:24
Show Gist options
  • Save shah-smit/4397d62270daf14819843490a706649b to your computer and use it in GitHub Desktop.
Save shah-smit/4397d62270daf14819843490a706649b to your computer and use it in GitHub Desktop.
String (String literal, StringBuffer, StringBuilder

String

== references to memory location, i.e if the both the objects share the same memory
.equals(...) references to the value of the object i.e if the both the objects has the exactly the same value.

Case 1: Considered String a liternal IMMUTABLE

Example: String name = "Smit"; In the memory | name || Smit | So whenever a new literal is created, Java(JVM) will go a check for its existance in the mrmory and it there is no, it will create new. name = "Smit Shah"; In the memory |name || Smit| | name || Smit Shah| so everytime program makes changes to the String variable, it will create a new instance in the memory and it will cause HEAP size to increase

String n = "Smit";
String m = n;
is(m == n) //yes, because they both are the same location
is(m.equals(n) //yes, they both have value "Smit"

m = m + " Shah";
is(m == n) //no, because they both are referencing at different locations
is(m.equals(n) //no, they both have different value "Smit" and "Smit Shah"

String x = "Smit";
is(x == n) //yes, because JVM will search for the value in memory, if found it will reference x to that memory location and if not it will create new.
is(x.equals(n)) //yes, they both have same value "Smit"

Case 2: Considered new String, creating a new object of type String IMMUTABLE

Example: String fname = new String("Smit"); In the memory |name || Smit| | fname || Smit| it will create a new location, and will not check if the value exists. String lname = fanme; is lname == fname? yes, why because it is sharing the same memory. but as soon as you make change to any(lname, fname) it will not be == anymore.

    String s = "s";
		String sn = new String(s);
		String snw = sn;
		
		if(s == sn) //false, why because they both are new instance in the location.
		if(sn == snw) //true, because they both share the same location in the memory
    
    sn += "s";
    if(sn == snw) //false, as there was a change in sn, thus sn has a new location in memory.

Case 3: Consider using new StringBuffer, creating a Object of type StringBuffer MUTABLE

One of its biggest strength Immutability is also biggest problem of Java String if not used correctly. Many times we create a String and then perform a lot of operation on them e.g. converting a string into uppercase, lowercase , getting substring out of it , concatenating with other string etc. Since String is an immutable class every time a new String is created and the older one is discarded, which creates lots of temporary garbage in the heap. insert and append are kind of method added to assit in updating the object

StringBuffer sb = new StringBuffer("Smit");
StringBuffer sb1 = new StringBuffer("Smit");
is(sb == sb1) //false, because they both have created a new instance of the object in the memory
is(sb.equals(sb1)) //false, MIND IT, you are comparing StringBuffer not the value of sb and sb1
is(sb.toString().equals(sb1.toString)) //true, as both have same value "Smit"

StringBuffer nsb = sb;
is(nsb == sb) //true, same memory location. 
nsb.append("s");
is(nsb == sb) //true, because StringBuffer are mutable, they work as "PASS BY REFERENCE" so the value of nsb and sb will be "Smits"
is(nsb.equals(sb)) //true, because of PASS BY REFERENCE, the vaues of sb abd nsb are appended.

Case 4: Consider using new StringBuilder, it is a copy of StringBuffer but just that this is not syncnorised MUTABLE

StringBuilder are copy of StringBuffer they have exactly the same behaviour except one thing StringBuffer is very good with mutable String but it has one disadvantage all its public methods are synchronized which makes it thread-safe but same time slow. In JDK 5 they provided a similar class called StringBuilder in Java which is a copy of StringBuffer but without synchronization. Try to use StringBuilder whenever possible it performs better in most of the cases than StringBuffer class.

You may read more about this topic here

String pig = "catch-22";
String newPig = new String("catch-22");
String dog = "catch-"+22;
String newDog = new String("catch-"+22);
is pig == dog ? true
is pig.equals(dog) ? true
is newPig == dog ? false
is newPig.equals(dog) ? true
is newPig.equals(newDog) ? false
Animals are equal will concatinate with pig and then compare with dog. thus it will give: false
(pig == dog) As this in bracket it will compare and result will contacatinated with Animals are equal : Animals are equal true
public class StringBufferBuilder {
public static void main(String[] args) {
// TODO Auto-generated method stub
String name = "Smit";
String fname = "Smit";
if(name.equals(fname)) System.out.println("name.equals(fname) "+true);
if(name == fname) System.out.println("name == fname "+true);
StringBuffer sb = new StringBuffer("Smit");
StringBuffer sb1 = new StringBuffer("Smit");
System.out.print("name == sb.toString() "); if(name==sb.toString()) System.out.println(true); else System.out.println(false);
System.out.print("sb.toString() == sb1.toString() "); if(sb.toString()==sb1.toString()) System.out.println(true); else System.out.println(false);
System.out.print("sb == sb1 "); if(sb == sb1) System.out.println(true); else System.out.println(false);
StringBuffer sb2 = sb1; //assigning sb1 to sb2
if(sb1 == sb2) System.out.println("sb1 == sb2 "+true); //will result true because sb1 and sb2, are referring to the same location.
sb1.append("s"); //appending sb1 by adding s to the sb1 value. sb1 and sb2 are looking at the same memory location so both the values will be changed.
System.out.println(sb1);
System.out.println(sb2);
if(sb1 == sb2) System.out.println("sb1 == sb2 "+true); //will result true because sb1 and sb2, are referring to the same location.
sb2.append("s"); //appending sb2 by adding s to the sb2 value. sb1 and sb2 are looking at the same memory location so both the values will be changed.
System.out.println(sb1);
System.out.println(sb2);
if(sb1 == sb2) System.out.println("sb1 == sb2 "+true); //will result true because sb1 and sb2, are referring to the same location.
}
}
name.equals(fname) true
name == fname true
name == sb.toString() false
sb.toString()==sb1.toString() false
sb ==sb1 false
sb1 == sb2 true
Smits
Smits
sb1 == sb2 true
Smitss
Smitss
sb1 == sb2 true
package com.tutorial.strings;
public class Strings {
public static void main(String[] args) {
// String data-type - non-Primitive, As it is immutable it will function as pass by value.
// == will check for the reference of the object in the memory, if they are same, it will return true otherwise false
// .equals() will check for the value of the object, if they are same, it will return true otherwise false
String pig = "catch-22"; //will check if any string has the same reference, and will reference pig to it
String newPig = new String("catch-22"); //new allocation in memory
String dog = "catch-"+22; //will check if any string has the same reference, and will reference dog to it. thus pig and dog will reference same object
String newDog = new String("catch-"+22); //new allocation in memory, doesn't matter if the same string exists
System.out.println("String pig = \"catch-22\";");
System.out.println("String newPig = new String(\"catch-22\");");
System.out.println("String dog = \"catch-\"+22;");
System.out.println("String newDog = new String(\"catch-\"+22);");
System.out.println();
System.out.print("is pig == dog ? ");
System.out.println(pig == dog); //true Reason: as pig and dog are sharing the same reference
System.out.println();
System.out.print("is pig.equals(dog) ? ");
System.out.println(pig.equals(dog));//true Reason: pig and dog as same value
System.out.println();
System.out.print("is newPig == dog ? ");
System.out.println(newPig == dog); //false Reason: as pig1 is a new allocation i.e. new reference in memory. thus pig1 and dog has difference
System.out.println();
System.out.print("is newPig.equals(dog) ? ");
System.out.println(newPig.equals(dog));//true Reason: pig and dog as same value
System.out.println();
System.out.print("is newPig.equals(newDog) ? ");
System.out.println(newPig == newDog); //false
System.out.println();
System.out.print("Animals are equal will concatinate with pig and then compare with dog. thus it will give: ");
System.out.println("Animals are equal "+pig == dog); //false
System.out.println();
System.out.print("(pig == dog) As this in bracket it will compare and result will contacatinated with Animals are equal : ");
System.out.println("Animals are equal "+(pig == dog)); //Animals are equal true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment