• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between two String statement

 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

What exactly is the difference between below two statements

String s="something";

AND

String s=new String("something");


 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave an interview yesterday...

He told me that first statement create one object and second statement create two object.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what should be the answer...?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i ask you one another question in same thread.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic