• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between two String statement

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what should be the answer...?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i ask you one another question in same thread.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic