-
Differance between "==" & string.equals()
What is the differance between "==" & string.equals() explain with example
Question asked by visitor hanumanth
-
Junior Member
Re: Differance between "==" & string.equals()
Hi,
Strings in Java are considered objects and you cant test equality with objects using "==" cause in Java this means that you are testing that both object references are pointing to the same object..
-
Junior Member
Re: Differance between "==" & string.equals()
In Java, "==" is used for comparing the premetive data types, and for objects "equals()" must be used. if u use "==" for objects comparison, it wiil just compare the reference of the objects not the content.
if u declare a string like
String a = "Same"; //1
Actually JVM doesnot consider it as a String object, rather than it will be consider as String literal. Only when u declare like
String a = new String("Same"); //2
will leads to create a object of type String.
When u use type 1 of declaration u can make use of "==". But for the second type u must use "equals()" method.
-
Junior Member
Re: Differance between "==" & string.equals()
It is important to understand that the equals() method and the == operator perform two different operator.
simply i will tell
the equals() method compares the characters inside a string object.
the == operator compares two object references.
example
class compare {
public static void main(String args[]) {
String s1 = "abc";
String s2 = "abc";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
Stirng s3 = new String("def");
String s4 = new String("def");
System.out.println(s3==s4);
System.out.println(s3.equals(s4));
}
}
just this program compile and run it then understand these compare of the == and equals().
-
Junior Member
Re: Differance between "==" & string.equals()
It is important to understand that the equals() method and the == operator perform two different operator.
simply i will tell
the equals() method compares the characters inside a string object.
the == operator compares two object references.
example
class compare {
public static void main(String args[]) {
String s1 = "abc";
String s2 = "abc";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
Stirng s3 = new String("def");
String s4 = new String("def");
System.out.println(s3==s4);
System.out.println(s3.equals(s4));
}
}
just this program compile and run it then understand these compare of the == and equals().
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules