Wednesday, December 2, 2015

Difference between String Buffer and String














Code Examples


package com.javaninja.stringexamples;

public class StringExample4 {
   //Difference between String Buffer and String
public static void main (String args[])
{
String str1 = "Java";
System.out.println("str1 : "+str1);// Prints Java

str1.toUpperCase();
    System.out.println("str1 after uppercase: "+str1);// Prints Java
   
    StringBuffer sb = new StringBuffer("Java");
    System.out.println("sb : "+sb);// Prints Java
   
    sb.append(" Hello");
    System.out.println("sb after append: "+sb);// Prints Java Hello
}

}

What are immutable objects and Why is String Immutable

















Code Examples

package com.javaninja.stringexamples;

import java.util.HashMap;

public class StringExample3 {
   //What are immutable objects and Why is String Immutable
public static void main (String args[])
{
String str1 = new String("Java");
System.out.println("str1 : "+str1);// Prints Java

str1.toUpperCase();
    System.out.println("str1 : "+str1);// Prints Java

    String str2= str1.toUpperCase();
    System.out.println("str2 : "+str2);// Prints JAVA
   
   
    Employee emp1 =  new Employee(101, "Vatsal");
    System.out.println("emp1 : "+emp1.getEmployeeCode()); // Prints 101
   
    emp1.setEmployeeCode(102);
    System.out.println("emp1 : "+emp1.getEmployeeCode()); // Prints 102
   
    HashMap hm = new HashMap();
    hm.put(str2, 1);
    str2=str2.concat(" Ninja");
    System.out.println(hm.get(str2));
   
}
}




package com.javaninja.stringexamples;

public class Employee {

private int employeeCode;
private String name;
Employee(int employeeCode,String name)
{
this.employeeCode = employeeCode;
this.name = name;
}
public int getEmployeeCode() {
return employeeCode;
}
public void setEmployeeCode(int employeeCode) {
this.employeeCode = employeeCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}





Difference between String "equals method" and "== operator"


















package com.javaninja.stringexamples;

public class StringExample2 {
   //Difference between String "equals method" and "== operator"
public static void main (String args[])
{
String str1 = "Java";
String str2 = "Java";

System.out.println("Is str1==str2 ? " +( str1==str2)); //Prints true
System.out.println("Is str1 equals str2 ? " +( str1.equals(str2))); //Prints true

String str3 = new String("Java");
System.out.println();
System.out.println("Is str1==str3 ? " +( str1==str3)); //Prints false
System.out.println("Is str1 equals str3 ? " +( str1.equals(str3))); //Prints true

}
}

What is the difference between "String Literal" and "String Object"





package com.javaninja.stringexamples;

public class StringExample1 {
   //Difference between "String Literal" and "String Object"
public static void main (String args[])
{
String str1 = "Java";
String str2 = "Java";

System.out.println("Is str1==str2 ? " +( str1==str2)); //Prints true

String str3 = new String("Java");

System.out.println("Is str1==str3 ? " +( str1==str3)); //Prints false

}
}