This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between two String statement 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 two String statement" Watch "Difference between two String statement" New topic
Author

Difference between two String statement

deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
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: 7552
    
  18

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?
Articles by Winston can be found here
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: 165
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: 165
what should be the answer...?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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: 165
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: 7552
    
  18

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: 165
can i ask you one another question in same thread.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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: 6109
    
    6

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: 6109
    
    6

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: 6109
    
    6

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between two String statement
 
Similar Threads
Difference Between the String s = "String "; and String s = new String("String");
string object
Difference in Creation of String Objects
Want the difference between String and StringBuffer
Difference between the two types of tostring methods?