Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Difference between String variable and String Object ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between String variable and String Object ???" Watch "Difference between String variable and String Object ???" New topic
Author

Difference between String variable and String Object ???

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,

Can any one please tell me what is the difference between :

String str = new String();

and

String str1 = null;

What I have understood that str is the object of String class and str1 is variable of String data type, am I right ?

But Iam confused that functions of both str and str1 are same .Can any one please tell me where is the difference between str and str1.

Thanks & Regards
Bikash
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

try this:


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;


http://home.arcor.de/hirnstrom/bewerbung
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
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
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 ?

If I am wrong can any one please clarify me.

Thanks & Regards
Bikash
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Hi Bikash,

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

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.
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi all,

Lot of thanks for all of yours clarification.

Thanks
Bikash
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between String variable and String Object ???
 
Similar Threads
diffrence between equals() method and ==
Query on String class ?
Query about String ??
Difference between String and StringBuffer.
String vs String Buffer