when you use new String("...") you get a new string object.
In this example both string literals refer the same object:
String a = "abc";
String b = "abc";
System.out.println(a == b); // True
Here two different objects are created and they have different references:
String c = new String("abc");
String d = new String("abc");
System.out.println(c == d); // False
In general you should use the string literal notation when possible. It is easier to read and it gives the compiler a chance to optimize your code.
Reference:
http://stackoverflow.com/questions/3297867/difference-between-string-object-and-string-literal
- the String class creates a pool of Strings
when you create a String by using the new operator or by using the + and += operators (the string is computed at runtime) you are implicitly telling the compiler to create a new String object
when you create a String by assigning a string literal the compiler searches the existing string pool for an exact match. If it finds one, a new string is NOT created. Instead the variable is assigned a reference to the existing pooled string.
http://www.janeg.ca/scjp/oper/string.html
public class compareStrings {
public static void main(String[] args) {
/*
* string literal example
* when you create a String by assigning a string literal the compiler searches the existing string pool for an exact match.
* If it finds one, a new string is NOT created. Instead the variable is assigned a reference to the existing pooled string
*/
String str1="abc";
String str2="abc";
if (str1==str2)
{
System.out.println("str1=str2");
}
else
{
System.out.println("str1 not str2");
}
//to actually compare the contents of String objects use the String method equals()
if (str1.equals(str2)) {
System.out.println("str1=str2");
} else {
System.out.println("str1 not str2");
}
}
}
output:
str1=str2
str1=str2
public class compareStrings {
public static void main(String[] args) {
/*
* Here two different string objects are created and they have different references
* when you use new String("...") you get a new string object
* when you create a String by using the new operator or by using the + and += operators (the string is computed at runtime)
* you are implicitly telling the compiler to create a new String object
*/
String str1=new String("abc");
String str2=new String("abc");
if (str1==str2)
{
System.out.println("str1=str2");
}
else
{
System.out.println("str1 not str2");
}
//to actually compare the contents of String objects use the String method equals()
if (str1.equals(str2)) {
System.out.println("str1=str2");
} else {
System.out.println("str1 not str2");
}
}
}
output:
str1 not str2
str1=str2
- Comparison of Objects with equals
== relational operator is used to compare primitive data types
int a = 5;
int b = 5;
if (a == b)
System.out.println("equal");
else
System.out.println("not equal");
Output is "equal".
"==" operator works only with primitive data types, does not work if we use wrapper classes of primitive data types
Integer a = new Integer(5);
Integer b = new Integer(5);
if (a == b)
System.out.println("equal");
else
System.out.println("not equal");
Output is "not equal".
"==" operator is not applicable for objects because it compares references of objects not contents of objects
comparison of contents of objects there is "equals" method in each object
wrapper class example
Integer a = new Integer(5);
Integer b = new Integer(5);
if (a.equals(b))
System.out.println("equal");
else
System.out.println("not equal");
Output is "equal".
This "equals" work fine for wrapper classes but does not run correctly for custom classes because it compares two object references by default just like "==" operator.
To change behaviour of "equals" we will have to override it in our classes
http://javainnovations.blogspot.com/2008/05/comparison-of-objects-with-equals.html
- equals method is overriden in all wrapper classes and String class as well.
the StringBuffer class doesnt overrides it.
http://www.coderanch.com/t/241896/java-programmer-SCJP/certification/equals-Wrapper-Classes
public class TestWrapper {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer i=new Integer("1");
Integer j=new Integer("1");
String s=new String("1");
String s1="1";
StringBuffer sb=new StringBuffer("1");
StringBuffer sb1=new StringBuffer("1");
System.out.println(i==j); //false
System.out.println(i.equals(j)); //true
System.out.println(s==s1); //false
System.out.println(s.equals(s1)); //true
System.out.println(sb==sb1); //false
System.out.println(sb.equals(sb1)); //false
System.out.println(sb==sb); //true
System.out.println(sb.equals(sb)); //true
System.out.println(sb1==sb1); //true
System.out.println(sb1.equals(sb1)); //true
}
}
- String and wrapper classes have override the equals method in Object class
Primitive values in Java are not objects.
In order to manipulate these values as objects, the java.lang package provides a wrapper class for each of the primitive data types
All wrapper classes are final.
The objects of all wrapper classes that can be instantiated are immutable,
Wrapper Comparison, Equality, and Hashcode
Each wrapper class (except Boolean) defines the following method:
int compareTo(WrapperType obj2)
Each wrapper class (except Boolean) also implements the Comparable interface, which defines the following method:
int compareTo(Object obj2)
This method is equivalent to the compareTo(WrapperType) method when the current object and the object denoted by the argument obj2 have the same WrapperType.
Each wrapper class overrides the equals() method from the Object class.
The overriding method compares two wrapper objects for object value equality.
boolean equals(Object obj2)
Each wrapper class overrides the hashCode() method in the Object class.
The overriding method returns a hash value based on the primitive value in the wrapper object.
int hashCode()
http://etutorials.org/cert/java+certification/Chapter+10.+Fundamental+Classes/10.3+The+Wrapper+Classes/
No comments:
Post a Comment