What the question 'Object of class' vs. 'Variable of data type' concerns: In my language I use it interchangable, but the language-lawyers might tell me I'm wrong.
A String of length null (s2), which is not 'null' looks a bit confusing, but is very helpfull, if you call replace ("foo", "") or String sNum = "" + 7;
One thing you should always remember is that Strings are always objects in Java.People usually get confused about the difference between Objects and references.
Here are some differences: 1) A reference is located in the stack memory, while an object is in the heap. 2) A reference is NOT an object. 3) We use references to refer to objects 4) Objects are eligible for garbage collected, when they loose their reference. 5) Objects are created using the new keyword.
Now, back to your question...
String str = new String(); str --> (String)
In this case you have a reference that is named 'str' which is referring to a string object.
String str1 = null; str1 --> null
Here on the other hand, you have a reference called 'str1' that is supposed to refer to an object of type string, but it is not, it is referring to null (Which mean that there is no object).
Since both str and str1 have the same referene, then they will have the same methods. [ May 16, 2004: Message edited by: Vicken Karaoghlanian ]
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Bikash Paul
Ranch Hand
Joined: Dec 04, 2001
Posts: 342
posted
0
Hi,
First of all thanks for the reply.
Now another question is
String s1 = new String (); String s2 = "";
when we say
s1.equals(s2)
then we are comparing two reference of String class. am I right ?
and when say
s1 == s2
then we are comparing the value of two reference of String class. am I right ?
When we write s1.equals(s2) then we are comparing the values of two string objects and when we write s1==s2 then we are comparing references.
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
posted
0
when we say
s1.equals(s2)
then we are comparing two reference of String class. am I right ?
Actually, no. Here you are comparing the values of the String (Comparing objects)
s1 == s2
then we are comparing the value of two reference of String class. am I right ?
Here you are comparing the references.
Let me explain it more with an example:
String s1 = new String("10"); String s2 = new String("10");
s1.equals(s2) --> returns TRUE, because the value of both string object are the same. s1 == s1 --> returns FALSE, because you are comparing references of both strings which definitely are not referring to the same object.
Remember, the only purpose of a reference is to allow us to access an object, nothing more, nothing less.
When you ask s1=s2, you are asking the computer whether s1 and s2 occupy the same physical space in the computer's memory, whether they are literally, physically one and the same object called by two different names.
Generally when you wonder whether two strings are equal you mean whether they contain the same word. This is in fact a different question and that's why it must be phrased differently.