| Author |
Difference between two String statement
|
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 159
|
|
Hi All
What exactly is the difference between below two statements
String s="something";
AND
String s=new String("something");
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4756
|
|
deepak carter wrote:What exactly is the difference between below two statements
String s="something";
AND
String s=new String("something");
Effectively, nothing. Why do you ask?
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Manoj Kumar Jain
Ranch Hand
Joined: Aug 22, 2008
Posts: 191
|
|
As such I don't think there is a difference. but when you use the String as primitive then compiler might create a single String object for many references (if those are having same value) but if you create object with new keyword then they will initialized in different memory location, look at below code:
[Edit]
below is the output:
false
true
|
Do not wait to strike till the iron is hot; but make it hot by striking....
|
 |
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 159
|
|
I gave an interview yesterday...
He told me that first statement create one object and second statement create two object.
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
|
The first statement may not actually create any new objects. The second statement will always create at least one new object.
|
 |
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 159
|
|
what should be the answer...?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
The first time we see a string literal, a String object is created in the String pool. So, the line
String s="something";
may or may not create an object, depending on whether "something" has already been seen.
Now,
String s=new String("something");
uses the new operator. that mean that no matter what, a new object will be created. It uses the literal "something" as a seed to create the object. If "something" has not yet been seen, then we first have to create it on the string pool, and then create the object as per the new operator.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4756
|
|
deepak carter wrote:what should be the answer...?
The answer should be the correct one.
While we all suspect what this question is about, you haven't told us what you think it's about...and at the end of the day, that's what is most important.
Winston
|
 |
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 159
|
|
Hi
If "something" is not seen before
then
String s="something" will create 1 object
and second statement
String s=new String("something");
will create 2 object....
Am I right???
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4756
|
|
deepak carter wrote:If "something" is not seen before...
Why are you worried? Programs should always do the same (or at least a predictable) thing.
My tip: ALWAYS, ALWAYS, ALWAYS, use equals() with reference variables (and that includes Strings). Only use '==' with primitives.
Winston
|
 |
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 159
|
|
|
can i ask you one another question in same thread.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4756
|
|
deepak carter wrote:can i ask you one another question in same thread.
If it's related to your previous question, sure; if not, better start a new Thread.
Winston
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5888
|
|
deepak carter wrote:Hi
If "something" is not seen before
then
String s="something" will create 1 object
No. Executing that line never creates an object. The String object "something" is put into the constant pool when the class is initialized, if it wasn't already there. Before we can execute that line, the String object already exists. That line just gives us a reference to it.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5888
|
|
Manoj Kumar Jain wrote:when you use the String as primitive
Which you can never do in Java, since Java Strings are not primitives.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5888
|
|
deepak carter wrote:
He told me that first statement create one object and second statement create two object.
First one creates zero objects. Second one creates one String object, and one char[] object. Possibly others, depending on what other members String has--no objects that I know of, but I can't be bothered to look it up.
(I hate it when interviewers give these "clever, tricky technical questions" when they don't even know the right answers themselves.
|
 |
 |
|
|
subject: Difference between two String statement
|
|
|