Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Difference between String variable and String Object ???

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Lot of thanks for all of yours clarification.

Thanks
Bikash
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!