• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings  RSS feed

 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this piece of code:

public static void main(String[] args){
String s1 = new String("abc");
String s2 = new String("bde");
if(s1==s2){
System.out.println("Both s1 and s2 are==");
}
else
System.out.println("Both Strings are not ==");
}

}
}

I expected the result as : Both s1 and s2 are ==, since both the String references refer to String object.
But the actual result i got was: Both Strings are not ==

Can anyone please tell me the reason for such behavior?
Thanks in advance
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to read twice for understanding your problem

The == checks if the reference variable refers to the same object and not to the same objecttype. For that you can use the instanceof operator.

The following code has the behavior that you want

 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand this you need to more practice on strings my friend.

why because if i am telling directly you may not understand the concept.

i will come to the point .

we can use == operator for to check the content of two primitive data types .as well as to check for the object references.

when two objects are pointing to the same object then == operator gives
true. otherwise it will give false.

for example
case(1)
Sample s1 = new Sample();
Sample s2 = s1;
here s1 and s2 are pointing to the same object. so it will give true when you apply == operator.

case(2)
Sample s1 = new Sample();
Sample s2 = new Sample();

s1 and s2 are two different objects. so you will get false.

i hope you got the concept
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == checks if the reference variable refers to the same object and not to the same objecttype. For that you can use the instanceof operator.


The problem with using the instanceof operator is that you need to know what type you want to confirm. If you want to test if any two objects are of the same class type, then you could do something like this...



Henry
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

I admit your method is better

I've read that your class object has some interesting properties and i think that I could use your code in my applications, because it would make the code more flexible.
 
Yash Sivan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, s1 stores the reference pointing to the content "abc" and s2 stores the reference pointing to the content "bde", which are not equal. You can check this by printing the variable s1 and s2.
System.out.println("s1 = " + s1 + "s2 = " + s2);

Adding to the replies, to check if the objects are of the same class and contents of the objects are equal, we can use the equals method of the Class Object.

if (s1.equals(s2))
{....

Me too a beginner, please correct, if any of the above is incorrect.
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code was posted. It checks to see if one object reference variable has the same class or is a subclass of the other class:



The following code tests to see if the classes are exactly of the same type:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kannan VS, please check your private messages. You can see them by clicking "My Profile" at the top right of the page.
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks a lot for the clarification..
s1==s2 -> checks if references are equal,
s1.getClass().isInstance(s2)-> checks if both are of same class
------------------------------------------------------------------
One more clarification required..what is equals() method used for?
s1.equals(s2) -> checks if both Strings have same value or not?
please correct me if i am wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!